GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-151707: fix race conditions in `FileIO` by KowalskiThomas · Pull Request #151708 · python/cpython

Conversation

@KowalskiThomas

@KowalskiThomas KowalskiThomas commented Jun 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

What is this PR?

Fixes #151707.

Note that the PR itself does a bit more than just fix the race condition. It introduces new helper functions around the self->fd to avoid race conditions altogether, in the same fashion as the socketmodule.c file (and updates every relevant usage site to call into those helpers).

cpython/Modules/socketmodule.c

Lines 566 to 592 in 46b5e3e

static inline void
set_sock_fd(PySocketSockObject *s, SOCKET_T fd)
{
#if SIZEOF_SOCKET_T == SIZEOF_INT
_Py_atomic_store_int_relaxed((int *)&s->sock_fd, (int)fd);
#elif SIZEOF_SOCKET_T == SIZEOF_LONG
_Py_atomic_store_long_relaxed((long *)&s->sock_fd, (long)fd);
#elif SIZEOF_SOCKET_T == SIZEOF_LONG_LONG
_Py_atomic_store_llong_relaxed((long long *)&s->sock_fd, (long long)fd);
#else
#error "Unsupported SIZEOF_SOCKET_T"
#endif
}
static inline SOCKET_T
get_sock_fd(PySocketSockObject *s)
{
#if SIZEOF_SOCKET_T == SIZEOF_INT
return (SOCKET_T)_Py_atomic_load_int_relaxed((int *)&s->sock_fd);
#elif SIZEOF_SOCKET_T == SIZEOF_LONG
return (SOCKET_T)_Py_atomic_load_long_relaxed((long *)&s->sock_fd);
#elif SIZEOF_SOCKET_T == SIZEOF_LONG_LONG
return (SOCKET_T)_Py_atomic_load_llong_relaxed((long long *)&s->sock_fd);
#else
#error "Unsupported SIZEOF_SOCKET_T"
#endif
}

  • Issue: Race condition in FileIO under free-threading #151707

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
KowalskiThomas added 2 commits June 19, 2026 10:55
@KowalskiThomas
fix: race conditions in FileIO
3a68a17
@KowalskiThomas
test: add regression test
14ed8f1
@bedevere-app bedevere-app Bot mentioned this pull request Jun 19, 2026
Race condition in FileIO under free-threading #151707
Open
vstinner
vstinner reviewed Jun 19, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

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.

I'm not sure that it's the right approach. While the change makes sure that int fd integer is not modified in parallel, it doesn't ensure that the file descriptor is not closed before/while we are using it.

For example, FileIO.isatty() gets int fd, check that it's >= 0, and then call isatty().

If another thread calls close(fd) in parallel, after >= 0 check and before isatty() call, isatty() fails with EBADF.

It's even worse if the file descriptor is recycled for a new unrelated file.

Maybe it would be safer to the whole method in a critical section. It would prevent calling FileIO.close() in parallel: the call would be serialized.

@cmaloney: What do you think?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

KowalskiThomas reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@vstinner

vstinner commented Jun 19, 2026

Copy link
Copy Markdown
Member

Oh, the TSan tests failed. Example:

WARNING: ThreadSanitizer: data race (pid=20851)
  Write of size 8 at 0x72b000000060 by thread T9189:
    #0 close <null> (python+0x1044c7) (BuildId: ac94b2b86fdb18d0b904ed36fb1fd97b68fe5a64)
    #1 internal_close /home/runner/work/cpython/cpython/./Modules/_io/fileio.c:155:15 (python+0x731b66) (BuildId: ac94b2b86fdb18d0b904ed36fb1fd97b68fe5a64)
    #2 _io_FileIO_close_impl /home/runner/work/cpython/cpython/./Modules/_io/fileio.c:210:10 (python+0x730fbb) (BuildId: ac94b2b86fdb18d0b904ed36fb1fd97b68fe5a64)
    #3 _io_FileIO_close /home/runner/work/cpython/cpython/./Modules/_io/clinic/fileio.c.h:34:12 (python+0x730fbb)
...
KowalskiThomas reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@KowalskiThomas

KowalskiThomas commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

I'm not sure that it's the right approach.

Oh, the TSan tests failed.

I guess we have an answer 😁

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Sep 23, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 23, 2026
@scasella scasella mentioned this pull request Sep 26, 2026
Free-threading: concurrent FileIO close/truncate can double-free stat_atopen #158196
Open
@BHUVANSH855 BHUVANSH855 mentioned this pull request Oct 10, 2026
gh-158196: Fix double free of FileIO stat_atopen #159153
Open
@KowalskiThomas

KowalskiThomas commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

I'm going to close this PR as I'm working (in an issue I'll post soon) on a better way to make FileIO thread-safe.

vstinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@KowalskiThomas KowalskiThomas closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@cmaloney cmaloney Awaiting requested review from cmaloney cmaloney will be requested when the pull request is marked ready for review cmaloney is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Race condition in FileIO under free-threading

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@KowalskiThomas @vstinner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.