-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
In open(f, ...), call flush before calling close
#35303
Conversation
In #35217, I noticed that if we call `close` on an IOStream, but the device that has the underlying file is full, we silently truncate the file without error. To remidy that, introduce an explicit flush call before closing the file. This should either succeed and reflect the changes on disk, or fail and throw an appropriate error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
[just stating the obvious, all CI failed because of errors in package loading] |
While this is a good idea and should work, I fear that there are many kinds of IO objects where this will not work. I've seen it break things before. In general, I fear we seem to be in a state where the whole flush/close dance is very, very brittle. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@StefanKarpinski This change should be fine. It's just implicit-return that got broken by mistake.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
julia> open("VERSION") do f; close(f); flush(f); end
julia> open(`cat`, "w") do f; close(f); flush(f); end
ERROR: IOError: stream is closed or unusable
^ Do we have any tests like these?
If it works on all built-in IO types, I'll be happy (a little surprised, but happy), but I've definitely seen external IO types that I suspect would break with this change, e.g. TranscodingStreams. |
I'm also fine with this in theory but I'm worried about breakage. Why not add a call to |
Ah, in fact ios_close already calls ios_flush internally, it just doesn't report the error. |
Right, so the question is should close throw an error instead. And when we talked about it, the sense was that The point is at some point, we should throw an error here, because otherwise subsequent code assumes that the file operation was complete, which may not be the case. |
Co-Authored-By: Jameson Nash <[email protected]>
It seems strange to me to only throw the error if you use the |
|
|
Agree. If write stuff and close a file and it didn't get successfully written to the file, I want an error. |
If |
Agree: close is better. If there's an error, you can't really assume the resulting file is in any kind of recoverable state anyway. |
In #35217, I noticed that if we call
close
on an IOStream, but the devicethat has the underlying file is full, we silently truncate the file without
error. To remidy that, introduce an explicit flush call before closing the
file. This should either succeed and reflect the changes on disk, or fail
and throw an appropriate error.