-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
LuYanFCP
wants to merge
1
commit into
python:main
Choose a base branch
from
LuYanFCP:fix-issue-84579
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fixed a deadlock issue in the bufferedIO module when using fork in Py…
…thon
- Loading branch information
commit f8e27c03eb64df30c6838841c14ff66cfc4d88af
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -737,6 +737,8 @@ static PyObject * | |
_bufferedreader_read_generic(buffered *self, Py_ssize_t); | ||
static Py_ssize_t | ||
_bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len); | ||
static int | ||
_buffered_init(buffered *self); | ||
|
||
/* | ||
* Helpers | ||
|
@@ -825,6 +827,60 @@ _buffered_raw_seek(buffered *self, Py_off_t target, int whence) | |
return n; | ||
} | ||
|
||
#ifdef HAVE_FORK | ||
|
||
static PyObject* | ||
buffered_after_fork_child_impl(PyObject *self, PyObject *Py_UNUSED(ignored)) | ||
{ | ||
buffered *buf = (buffered *)self; | ||
if (_buffered_init(buf) < 0) { | ||
PyErr_SetString(PyExc_RuntimeError, "Failed to initialize buffer after fork"); | ||
return NULL; | ||
} | ||
_bufferedreader_reset_buf(buf); | ||
Py_RETURN_NONE; | ||
} | ||
|
||
static PyMethodDef buffered_fork_methods[] = { | ||
{"_after_fork_child", buffered_after_fork_child_impl, METH_NOARGS, NULL}, | ||
{NULL, NULL} | ||
}; | ||
|
||
static int | ||
buffered_register_at_fork(buffered *self) | ||
{ | ||
PyInterpreterState *interp = PyThreadState_Get()->interp; | ||
if (!interp) { | ||
PyErr_SetString(PyExc_RuntimeError, "Failed to get interpreter state"); | ||
return -1; | ||
} | ||
|
||
// Only register the fork handlers once | ||
if (!interp->after_forkers_child) { | ||
interp->after_forkers_child = PyList_New(0); | ||
if (!interp->after_forkers_child) { | ||
return -1; | ||
} | ||
} | ||
|
||
/* Create method objects */ | ||
PyObject *after_child = PyCFunction_New(&buffered_fork_methods[0], (PyObject *)self); | ||
if (!after_child) { | ||
return -1; | ||
} | ||
|
||
/* Append callbacks to the lists */ | ||
int status = 0; | ||
if (PyList_Append(interp->after_forkers_child, after_child) < 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm correct, I think the length of |
||
status = -1; | ||
} | ||
|
||
Py_DECREF(after_child); | ||
return status; | ||
} | ||
|
||
#endif /* HAVE_FORK */ | ||
|
||
static int | ||
_buffered_init(buffered *self) | ||
{ | ||
|
@@ -859,6 +915,16 @@ _buffered_init(buffered *self) | |
self->buffer_mask = 0; | ||
if (_buffered_raw_tell(self) == -1) | ||
PyErr_Clear(); | ||
|
||
#ifdef HAVE_FORK | ||
/* Register fork handlers */ | ||
if (buffered_register_at_fork(self) < 0) { | ||
PyThread_free_lock(self->lock); | ||
self->lock = NULL; | ||
return -1; | ||
} | ||
#endif /* HAVE_FORK */ | ||
|
||
return 0; | ||
} | ||
|
||
|
Oops, something went wrong.
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.
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.
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.