Skip to content

Conversation

@LuYanFCP
Copy link

@LuYanFCP LuYanFCP commented Jan 7, 2025

Fix #84579

[BUG] Fix deadlock in BufferedIO after fork in child process

The current implementation of BufferedIO can lead to a deadlock situation in child processes after fork() due to inherited lock states from the parent process's buffer protection mechanism.

Problem
When a Python process forks, the child process inherits the lock state of BufferedIO's internal buffer protection from its parent. This inherited lock state can cause deadlocks in the child process when attempting to perform I/O operations.

Solution

  • Added a new function buffered_after_fork_child_impl() to properly initialize buffer and lock states in child processes
  • Integrated the new function into PyOS_AfterFork_Child() to ensure proper buffer state reset after fork
  • This ensures that the child process starts with a clean lock state, preventing potential deadlocks

Target Issues

This fix is particularly important for applications that rely on process forking and perform I/O operations in child processes.

Note: This change only affects Unix-like systems where fork() is available.

@ghost
Copy link

ghost commented Jan 7, 2025

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jan 7, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@LuYanFCP LuYanFCP marked this pull request as draft January 7, 2025 16:49
}

/* Create method objects */
PyObject *after_child = PyCFunction_New(&buffered_fork_methods[0], (PyObject *)self);
Copy link
Contributor

Choose a reason for hiding this comment

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

For me, this is not true here.

If you try to register a callback for every buffer object. I think there would be memory leak here.


/* Append callbacks to the lists */
int status = 0;
if (PyList_Append(interp->after_forkers_child, after_child) < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm correct, I think the length of after_forkers_child will raising unlimited?

@bedevere-app
Copy link

bedevere-app bot commented Jul 21, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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.

IO streams locking can be broken after fork() with threads

2 participants