fsnotify icon indicating copy to clipboard operation
fsnotify copied to clipboard

Fix OSError race condition in file polling between fileExists and getLastModificationTime

Open Copilot opened this issue 1 month ago • 0 comments

Race condition in file polling: fileExists() can return true, but file gets deleted before getLastModificationTime() executes, raising unhandled OSError.

Changes

Wrapped getLastModificationTime() calls in try-except blocks at race-prone locations in src/fsnotify/filepoll.nim:

  • initFileEventData: Catches OSError during file initialization, silently skips initialization if file vanishes
  • filecb modification check: Catches OSError when checking existing file for changes, defers to next poll cycle
  • filecb rename detection: Catches OSError when checking renamed files, marks file as non-existent and continues search
  • filecb recreation: Catches OSError when reinitializing previously-removed files that reappear
# Before
if fileExists(data.name):
  let now = getLastModificationTime(data.name)  # Can raise OSError
  
# After  
if fileExists(data.name):
  try:
    let now = getLastModificationTime(data.name)
  except OSError:
    discard  # File vanished, handled on next poll

Affects Windows and macOS file monitoring (both use polling). Linux unaffected (uses inotify).

Original prompt

This section details on the original issue you should resolve

<issue_title>getLastModificationTime can fail and raise OSError</issue_title> <issue_description>Very rarely when polling for a file, os.getLastModificationTime can fail to find the file (as it was just removed) after os.fileExists returns true and thus raises OSError.</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes planety/fsnotify#4

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Jan 10 '26 07:01 Copilot