fix: guard yaml/flock/TOCTOU/atomic writes across small surfaces (#28018)#28593
Merged
Conversation
1. trajectory_compressor.py: yaml.safe_load() returns None on empty
files, crashing with TypeError on `if 'tokenizer' in data`. Fix by
adding `or {}` fallback. (HIGH — blocks startup with empty config)
2. 6 files with fcntl.flock(LOCK_UN) in finally blocks without
try/except: cron/scheduler.py, hermes_cli/auth.py,
agent/shell_hooks.py, tools/skill_usage.py,
tools/environments/file_sync.py, tools/memory_tool.py. If unlock
raises OSError, fd.close() is skipped and the lock is held forever.
The msvcrt branches already had try/except; the fcntl branches did
not. Fix by wrapping in try/except (OSError, IOError): pass.
3. agent/copilot_acp_client.py line 639: TOCTOU race — path.exists()
followed by path.read_text() with no try/except. If file is deleted
between the check and the read, FileNotFoundError propagates. Fix
by using try/except FileNotFoundError.
4. gateway/sticker_cache.py: non-atomic write via Path.write_text()
can leave truncated JSON on crash, causing JSONDecodeError on next
load. Fix by writing to tempfile + fsync + os.replace (atomic).
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Salvage of #28018 by @vanthinh6886.
What: Several small defensive gaps across multiple modules that all share the same pattern (race condition between check + use, or no exception handler around a syscall that can fail under load/edge cases):
agent/copilot_acp_client.py:path.exists()→path.read_text()is a TOCTOU race; replace withtry/except FileNotFoundError.agent/shell_hooks.py,cron/scheduler.py,hermes_cli/auth.py:fcntl.flock(.., LOCK_UN)in finally blocks could raise OSError if the lock fd was already closed; wrap in try/except.gateway/sticker_cache.py: cache save wasCACHE_PATH.write_text(...)(non-atomic, partial-write corruption risk); switch totempfile.mkstemp+os.fsync+os.replace.Why: None of these are bugs hit in the happy path — they're hardening against partial reads/writes, lock fd reuse, and races on heavily-loaded systems.
Original PR: #28018