Skip to content

Write files to disk atomically so they are never empty#126

Merged
airhorns merged 1 commit into
mainfrom
fix-atomic-write
Sep 26, 2024
Merged

Write files to disk atomically so they are never empty#126
airhorns merged 1 commit into
mainfrom
fix-atomic-write

Conversation

@airhorns

@airhorns airhorns commented Sep 26, 2024

Copy link
Copy Markdown
Contributor

This fixes a bug where if multiple parallel worker threads in a wds project start around the same time, one will trigger a build of the whole project and another will soon after trigger a build of just one file. Both builds are processed in parallel for speed, but if the timing is just right, both try to write to the same destination file around the same time. If one of the project threads is unlucky enough to read the file at around when it is being written, it can read an empty or half written file. Whoops!

write-file-atomic solves this problem by using a tmpfile and serializing writes made from within the same process.

I ran a stress test in the Gadget repo that boots many worker threads in parallel that almost always failed after one or two iterations before this change, and didn't fail after 10 mins of continuous booting locally!

COMPUTERS

[no-changelog-required]

This fixes a bug where if multiple parallel worker threads in a wds project start around the same time, one will trigger a build of the whole project and another will soon after trigger a build of just one file. Both builds are processed in parallel for speed, but if the timing is just right, both try to write to the same destination file around the same time. If one of the project threads is unlucky enough to read the file at around when it is being written, it can read an empty or half written file. Whoops!

`write-file-atomic` solves this problem by using a tmpfile and serializing writes made from within the same process.

COMPUTERS

[no-changelog-required]
@airhorns airhorns marked this pull request as ready for review September 26, 2024 15:31
@airhorns airhorns merged commit ed7a7e0 into main Sep 26, 2024
@airhorns airhorns deleted the fix-atomic-write branch September 26, 2024 15:32
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