Handle short writes when creating memory files - #1849
Open
sylvesterkaczmarek wants to merge 1 commit into
Open
Conversation
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
Ensure the local filesystem memory tool writes complete file contents when
os.write()performs a legal short write.Both
BetaLocalFilesystemMemoryTool.create()andBetaAsyncLocalFilesystemMemoryTool.create()currently callos.write()once and ignore its return value before reporting success. POSIX writes are allowed to consume fewer bytes than requested, so a short write can leave a newly created memory file truncated while the tool still returnsFile created successfully.The existing atomic edit/insert paths already loop until the complete buffer has been written, so create currently has weaker data-integrity semantics than later updates to the same memory file.
Fix
Introduce a small shared
_write_all()helper that retries writes until the complete byte buffer has been persisted and treats a zero-byte write as an error.Use the helper consistently in synchronous and asynchronous memory creation and in the existing sync/async atomic replace paths.
Regression coverage
Adds deterministic sync and async tests that force
os.write()to accept only three bytes per call. The tests use UTF-8 content containing a multibyte character and verify that multiple writes occur and the final file exactly matches the requested content.The production change is confined to Anthropic's hand-maintained local filesystem memory tool.