Skip to content

fix(sandbox): accumulate successive writes in SandboxFileIO write mode - #126

Open
pcbeingused333 wants to merge 1 commit into
koyeb:mainfrom
pcbeingused333:fix/fileio-write-accumulate
Open

pcbeingused333 wants to merge 1 commit into
koyeb:mainfrom
pcbeingused333:fix/fileio-write-accumulate

Conversation

@pcbeingused333

Copy link
Copy Markdown

Fixes the loss of earlier blocks when a file opened with fs.open(path, "w") is written more than once.

Root cause

SandboxFileIO.write / AsyncSandboxFileIO.write call write_file with only the new block in w mode, and write_file replaces the whole remote file. After write("first\n"); write("second\n") the file contains only second\n. Append mode already reads the existing content, so it was not affected.

Fix

The handle keeps what it has written so far and sends the accumulated content on each write() (sync and async). Mixing str and bytes in one handle raises TypeError, as append mode already does.

Reproduction

with fs.open("/tmp/output.txt", "w") as f:
    f.write("first\n")
    f.write("second\n")
# remote file: "second\n" (expected "first\nsecond\n")

Testing

  • pytest koyeb/sandbox/test_filesystem.py -k "accumulates or retry_after": before the change the two accumulates tests (sync and async) fail; after it all four pass. The two retry_after tests guard the new accumulated state: a failed write_file does not record the content, so retrying the same write does not duplicate it.
  • Full koyeb/sandbox/test_filesystem.py: 147 passed.

AI assistance was used to prepare this change and its tests.

write_file replaces the whole file, so each write() in 'w' mode dropped
earlier blocks. Keep the accumulated content and only update it after
write_file succeeds, so a failed write that is retried is not duplicated.
Covers sync and async.
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.

1 participant