Skip to content

gh-84579: Fixed a deadlock issue in the bufferedIO module when using fork in Py… #128591

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy