The File-Watching Race Condition That Only Shows Up in Production
说明与用途
问题:文件监视器在启动前创建的文件会被忽略,导致日志缺失和确认失败。解决方案:并行启动监视器和文件写入,确保监视器先监听事件,从而消除竞争条件。
问题
File watchers miss files if the file is created before the watcher starts, causing missing logs and silent failures in production.
结果
By starting the watcher and writer simultaneously, the system reliably captures log files, preventing missing confirmations and silent failures.
工作流
- Start file watcher (watch_log_creation) and file write (write_order_log) in parallel
- Configure watcher to monitor /logs/orders for *.log entries
- Set watchDuration to 20000 ms to detect missing files
- Process log file once watcher signals creation
证据
The watcher has to already be listening before the file lands. Get that backwards, even by a few milliseconds, and the file just slips through.
The fix is to kick off "start watching" and "write the file" at the same moment, as two things happening in parallel instead of one after the other.
- 记录日期: 2026-09-22T10:45:48.201Z