Repository navigation
Conversation
mkstemp() returns a raw fd which is then passed to open(fd, "w"). If open() succeeds but the write fails, the with-block closes the fd, then the except handler calls os.close(fd) again. On systems where fd numbers are quickly reused, this can close an unrelated file descriptor. Simplify by closing the mkstemp fd immediately and reopening by path. The temp file already exists on disk so there is no TOCTOU race. Made-with: Cursor
There was a problem hiding this comment.
Pull request overview
This PR adjusts the atomic checkpoint writer in researchclaw.pipeline.runner to avoid ambiguous ownership of a raw file descriptor returned by tempfile.mkstemp(), preventing a potential double-close scenario during error handling.
Changes:
- Close the
mkstemp()file descriptor immediately after creation. - Reopen the temp file by path for JSON writing, then atomically replace
checkpoint.json. - Simplify exception cleanup to only remove the temp file.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
84
to
86
| try: | ||
| with open(fd, "w", encoding="utf-8") as fh: | ||
| with open(tmp_path, "w", encoding="utf-8") as fh: | ||
| fh.write(json.dumps(checkpoint, indent=2)) |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2 tasks done
3 tasks done
Author
|
Consolidated into #102 as requested. |
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.
Summary
_write_checkpointusestempfile.mkstemp()which returns a raw fd, then passes it toopen(fd, "w")open()succeeds butwrite()fails, thewithblock closes the fd via__exit__, then the except handler callsos.close(fd)againFix: Close the mkstemp fd immediately and reopen by path. The temp file already exists on disk, so there is no TOCTOU race. This eliminates fd ownership ambiguity entirely.
Test plan
Path.replace()still works correctly