Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use a tempfile to write files in filestorage. #300

Merged
merged 2 commits into from
Aug 4, 2024

Conversation

elee1766
Copy link
Contributor

@elee1766 elee1766 commented Jul 6, 2024

discovered in #296

there is a currently a race between file deletion and rewrite which can cause an empty read.

copying the method used by containerd, https://github.com/containerd/containerd/blob/main/pkg%2Fatomicfile%2Ffile.go the problem can be solved via a flushing a temporary file

@mholt
Copy link
Member

mholt commented Jul 10, 2024

Thanks for the PR!

I don't understand much about this (you have been very helpful explaining it in Slack so far, which I appreciate) -- is this change ready to go? Does it work for all the platforms it is supposed to work on (and it doesn't break on others)?

I will give it a closer look ASAP :)

@elee1766
Copy link
Contributor Author

yes this is ready. it should work fine and shouldn't break anything on windows.

Comment on lines +91 to +94
_, err = fp.Write(value)
if err != nil {
// cancel the write
fp.Cancel()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't the fp automatically cancel the write when there's an error?

Copy link
Contributor Author

@elee1766 elee1766 Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cancel is a special call which deletes the temp file.

the fp to the tempfile can't know to delete itself when it errors, so we tell it to

@mholt
Copy link
Member

mholt commented Jul 15, 2024

One more quick question -- how do you think this will work with NFS mounts? We have some people using it with NFS, though I don't recommend it because I know NFS already has some issues related to O_EXCL and possibly other things; do you think this will make the problem worse or better? (Or neither)

@elee1766
Copy link
Contributor Author

One more quick question -- how do you think this will work with NFS mounts? We have some people using it with NFS, though I don't recommend it because I know NFS already has some issues related to O_EXCL and possibly other things; do you think this will make the problem worse or better? (Or neither)

it should be better or the same.

NFS mounts have more lag, they are maybe more likely to run into the race condition that this fixes.

but also, NFS has so many issues for stuff like this that I can't say for sure it will anything better. I don't know if this will fix issues with o_excl. I don't use it and it's fundamentally broken on NFS so I doubt it will.

@@ -0,0 +1,3 @@
# testutil

some testing functions copied out of github.com/stretchr/testify, which were originally used by atomicfile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine -- but it's MIT licensed code so we should probably copy and paste the license (which contains the copyright) into this readme file, just in case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

mholt
mholt previously approved these changes Aug 2, 2024
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give this a shot. Thanks for being willing to help maintain this!

@mholt mholt merged commit 1a2275d into caddyserver:master Aug 4, 2024
6 checks passed
This pull request was closed.
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.

2 participants