Skip to content

Fix OSError race condition in file polling between fileExists and getLastModificationTime#15

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-last-modification-time-error
Draft

Fix OSError race condition in file polling between fileExists and getLastModificationTime#15
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-last-modification-time-error

Conversation

Copy link
Copy Markdown

Copilot AI commented Jan 10, 2026

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)


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

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix OSError when polling last modification time Fix OSError race condition in file polling between fileExists and getLastModificationTime Jan 10, 2026
Copilot AI requested a review from ringabout January 10, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getLastModificationTime can fail and raise OSError

2 participants