X Tutup
The Wayback Machine - https://web.archive.org/web/20260117202126/https://github.com/python/cpython/pull/23712
Skip to content

Conversation

@izbyshev
Copy link
Contributor

@izbyshev izbyshev commented Dec 9, 2020

On POSIX-conforming systems, O_APPEND flag for open() must ensure
that no intervening file modification occurs between changing the file
offset and the write operation[1]. In effect, two processes that
independently opened the same file with O_APPEND can't overwrite
each other's data. On Windows, however, the Microsoft C runtime
implements O_APPEND as an lseek(fd, 0, SEEK_END) followed by write(),
which obviously doesn't provide the above guarantee. This affects
both os.open() and the builtin open() Python functions, which rely on
_wopen() from MSVCRT.

One way to achieve the desired behavior is to ensure that the Windows
handle backing the MSVCRT file descriptor has FILE_APPEND_DATA access
right, but doesn't have FILE_WRITE_DATA. This makes the Windows kernel
enforce the described semantics.

To avoid full reimplementation of _wopen(), implement the above by
duplicating the MSVCRT-created handle with fixed up access rights and
wrapping it with a new file descriptor. Note that handles created by
functions like os.dup() will still have the correct access rights (and
thus atomic behavior) because MSVCRT duplicates them with
DUPLICATE_SAME_ACCESS flag.

[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html

https://bugs.python.org/issue42606

On POSIX-conforming systems, O_APPEND flag for open() must ensure
that no intervening file modification occurs between changing the file
offset and the write operation[1]. In effect, two processes that
independently opened the same file with O_APPEND can't overwrite
each other's data. On Windows, however, the Microsoft C runtime
implements O_APPEND as an lseek(fd, 0, SEEK_END) followed by write(),
which obviously doesn't provide the above guarantee. This affects
both os.open() and the builtin open() Python functions, which rely on
_wopen() from MSVCRT.

One way to achieve the desired behavior is to ensure that the Windows
handle backing the MSVCRT file descriptor has FILE_APPEND_DATA access
right, but doesn't have FILE_WRITE_DATA. This makes the Windows kernel
enforce the described semantics.

To avoid full reimplementation of _wopen(), implement the above by
duplicating the MSVCRT-created handle with fixed up access rights and
wrapping it with a new file descriptor. Note that handles created by
functions like os.dup() will still have the correct access rights (and
thus atomic behavior) because MSVCRT duplicates them with
DUPLICATE_SAME_ACCESS flag.

[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
@github-actions
Copy link

github-actions bot commented Jan 9, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jan 9, 2021
@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Aug 2, 2022
@xhyrom
Copy link

xhyrom commented Mar 11, 2024

Why this has not been review since 2020?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

X Tutup