You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python breaks OS' append guarantee on file writes #59928
In any case, Python 2 uses fwrite() not write(), so it may be the explanation. Do you observe the same behaviour when using io.open() instead of open()?
(io.open() is the Python 3 IO stack backported to Python 2)
bsdphk commented on Aug 18, 2012
bsdphkmannequin
MannequinAuthor
More actions
Yes, it does:
If the O_APPEND flag of the file status flags is set, the file offset shall be set to the end of the file prior to each write and no intervening file modification operation shall occur between changing the file offset and the write operation.
Ah, sorry. I was stupidly looking for "atomic" and only found the pipe-specific remarks.
(the other points remain, though :-))
bsdphk commented on Aug 18, 2012
bsdphkmannequin
MannequinAuthor
More actions
I have not tried io.open(), nor would I suspect most users would realize that they needed to do so, in order to get the canonical behaviour from an operation called "write" on a file opened in "append" mode.
IMO: If pythons file.write() does not give the guarantee POLA would indicate, it's either a bug or a doc-issue, no matter how many workarounds might exist.
But I have neither a clue to the aspirational goals of python, nor to what it might take to fix this, so it's entirely your call.
I have not tried io.open(), nor would I suspect most users would
realize that they needed to do so, in order to get the canonical
behaviour from an operation called "write" on a file opened in
"append" mode.
The reason I'm asking is that open() is the same as io.open() in Python
3.x, which is currently the main development line. That said, I can find
the results myself.
Python 2 is in bugfix mode, so it's impossible to rewrite the I/O
routines to use unbuffered I/O instead of C buffered I/O.
IMO: If pythons file.write() does not give the guarantee POLA would
indicate, it's either a bug or a doc-issue, no matter how many
workarounds might exist.
What do you call POLA?
But I have neither a clue to the aspirational goals of python, nor to
what it might take to fix this, so it's entirely your call.
Well as I said, Python 2 will be pretty much impossible to fix (we call
fwrite() with the argument, not write()). Python 3 is a different story
since we use our own buffering layer and then C's unbuffered API.
As a sidenote, do you know if writev() has the same guarantee as
write()? POSIX doesn't seem to say so.
bsdphk commented on Aug 18, 2012
bsdphkmannequin
MannequinAuthor
More actions
POLA=PrincipleOfLeastAstonishment
We use that a lot in architectural decision in FreeBSD :-)
As I said: You deal with this as you see fit. If all python2 gets is a doc- or errata-notice, that's perfectly fine with me.
I interpret "The writev() function shall be equivalent to write(), except as described below." as writev() giving the same atomic append guarantee.
In FreeBSD, write() is implemented using writev() and I expect that is the obvious and thus common way it is done.
(You seem to be right with respect to the 1024: That is indeed still the BUFSIZ on FreeBSD, I'll work on getting that changed.)
Even if we write in chunks, if we are calling the OS write function and O_APPEND is set, wouldn't be satisfying the condition? Or, rather, the OS would be. That is, I don't really see a guarantee of an *atomic* write in the quoted description.
Even if we write in chunks, if we are calling the OS write function
and O_APPEND is set, wouldn't be satisfying the condition? Or,
rather, the OS would be. That is, I don't really see a guarantee of
an *atomic* write in the quoted description.
I'm not sure it's guaranteed to be atomic at the hardware level, but as
AFAIU the updates should be atomic as seen from other processes on the
same machine (i.e. filesystem cache coherency).
As a side-note, I've just tested under Linux with the following script:
on 2.7, the write buffers get sliced up (the glibc's fwrite() doesn't
care about atomicity):
write(3, "abcdxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 4096) = 4096
write(3, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 1044480) = 1044480
on 3.2 and 3.3, our home-grown buffering respects the original
buffers:
write(3, "abcd", 4) = 4
write(3, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 1048576) = 1048576
(but that's purely out of luck, since we didn't design it with that
goal :-))
neologix commented on Aug 24, 2012
neologixmannequin
Mannequin
More actions
I wouldn't rely on O_APPEND too much:
it won't work on NFS, and probably other non-local filesystems
it doesn't actually guarantee atomicity, because even though the the file offset and the write is done with locking, there is still the possibility of partial write
Activity
bsdphk commentedon Aug 18, 2012
When a file is opened in append mode, the operating system guarantees that all write(2) system calls atomically appended their payload to the file.
At least on FreeBSD, Python breaks this guarantee, by chopping up large writes into multiple write(2) syscalls to the OS.
Try running this program using ktrace/truss/strace or a similar system-call tracing facility:
Instead of one single megabyte write, I see 1024 kilobyte writes.
(BTW: Why only one kilobyte ? That is an incredible pessimisation these days...)
I leave it to the python community to decide if this should be fixed, or merely pointed out in documentation (os.write() is a workaround)
pitrou commentedon Aug 18, 2012
Does it? I don't see such strong guarantees in http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
In any case, Python 2 uses fwrite() not write(), so it may be the explanation. Do you observe the same behaviour when using io.open() instead of open()?
(io.open() is the Python 3 IO stack backported to Python 2)
bsdphk commentedon Aug 18, 2012
Yes, it does:
If the O_APPEND flag of the file status flags is set, the file offset shall be set to the end of the file prior to each write and no intervening file modification operation shall occur between changing the file offset and the write operation.
pitrou commentedon Aug 18, 2012
Ah, sorry. I was stupidly looking for "atomic" and only found the pipe-specific remarks.
(the other points remain, though :-))
bsdphk commentedon Aug 18, 2012
I have not tried io.open(), nor would I suspect most users would realize that they needed to do so, in order to get the canonical behaviour from an operation called "write" on a file opened in "append" mode.
IMO: If pythons file.write() does not give the guarantee POLA would indicate, it's either a bug or a doc-issue, no matter how many workarounds might exist.
But I have neither a clue to the aspirational goals of python, nor to what it might take to fix this, so it's entirely your call.
pitrou commentedon Aug 18, 2012
The reason I'm asking is that open() is the same as io.open() in Python
3.x, which is currently the main development line. That said, I can find
the results myself.
Python 2 is in bugfix mode, so it's impossible to rewrite the I/O
routines to use unbuffered I/O instead of C buffered I/O.
What do you call POLA?
Well as I said, Python 2 will be pretty much impossible to fix (we call
fwrite() with the argument, not write()). Python 3 is a different story
since we use our own buffering layer and then C's unbuffered API.
As a sidenote, do you know if writev() has the same guarantee as
write()? POSIX doesn't seem to say so.
bsdphk commentedon Aug 18, 2012
We use that a lot in architectural decision in FreeBSD :-)
As I said: You deal with this as you see fit. If all python2 gets is a doc- or errata-notice, that's perfectly fine with me.
I interpret "The writev() function shall be equivalent to write(), except as described below." as writev() giving the same atomic append guarantee.
In FreeBSD, write() is implemented using writev() and I expect that is the obvious and thus common way it is done.
(You seem to be right with respect to the 1024: That is indeed still the BUFSIZ on FreeBSD, I'll work on getting that changed.)
bitdancer commentedon Aug 18, 2012
Even if we write in chunks, if we are calling the OS write function and O_APPEND is set, wouldn't be satisfying the condition? Or, rather, the OS would be. That is, I don't really see a guarantee of an *atomic* write in the quoted description.
pitrou commentedon Aug 18, 2012
I'm not sure it's guaranteed to be atomic at the hardware level, but as
AFAIU the updates should be atomic as seen from other processes on the
same machine (i.e. filesystem cache coherency).
As a side-note, I've just tested under Linux with the following script:
Results:
on 2.7, the write buffers get sliced up (the glibc's fwrite() doesn't
care about atomicity):
write(3, "abcdxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 4096) = 4096
write(3, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 1044480) = 1044480
on 3.2 and 3.3, our home-grown buffering respects the original
buffers:
write(3, "abcd", 4) = 4
write(3, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"..., 1048576) = 1048576
(but that's purely out of luck, since we didn't design it with that
goal :-))
neologix commentedon Aug 24, 2012
I wouldn't rely on O_APPEND too much: