Skip to content

[Python Fork] Use os.register_at_fork instead of pthread_atfork.#32935

Merged
XuanWang-Amos merged 2 commits intogrpc:masterfrom
XuanWang-Amos:PythonForkFix
Apr 25, 2023
Merged

[Python Fork] Use os.register_at_fork instead of pthread_atfork.#32935
XuanWang-Amos merged 2 commits intogrpc:masterfrom
XuanWang-Amos:PythonForkFix

Conversation

@XuanWang-Amos
Copy link
Copy Markdown
Contributor

Fix: #18075

From comments in #18075, CPython reinitialize the GIL after pthread_atfork child handler, thus we shouldn't use any GIL related functions in child handler which is what we're currently doing, this PR uses os.register_at_fork to replace pthread_atfork to prevent any undesired bevahior.

This also seems to fixes a thread hanging issue cased by changes in core: #32869

Testing:

@XuanWang-Amos XuanWang-Amos added release notes: no Indicates if PR should not be in release notes python Pull requests that update Python code labels Apr 25, 2023
@XuanWang-Amos XuanWang-Amos marked this pull request as ready for review April 25, 2023 17:26
Copy link
Copy Markdown
Member

@drfloob drfloob left a comment

Choose a reason for hiding this comment

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

LGTM, pending Python tests passing. I can confirm that this fix removes the fork-hang problem I was able to reproduce in my work. I'm comfortable landing this, but @gnossen please also sanity check this before the next release at the end of May, just to be safe.

@XuanWang-Amos XuanWang-Amos changed the title [Python Fork] Use os.register_at_fork instead of pthread_atfork [Python Fork] Use os.register_at_fork instead of pthread_atfork. Apr 25, 2023
@XuanWang-Amos XuanWang-Amos merged commit bcb9701 into grpc:master Apr 25, 2023
@copybara-service copybara-service bot added the imported Specifies if the PR has been imported to the internal repository label Apr 26, 2023
XuanWang-Amos added a commit to XuanWang-Amos/grpc that referenced this pull request May 1, 2023
…c#32935)

Fix: grpc#18075

From comments in grpc#18075, `CPython`
reinitialize the `GIL` after `pthread_atfork` child handler, thus we
shouldn't use any `GIL` related functions in child handler which is what
we're currently doing, this PR uses `os.register_at_fork` to replace
`pthread_atfork` to prevent any undesired bevahior.

This also seems to fixes a thread hanging issue cased by changes in
core: grpc#32869

### Testing:
* Passed existing fork tests. (Note that due to some issues in `Bazel`,
this change was not verified by `Bazel runs_per_test`).
* Tested by patch the core PR, was able to fix Python fork tests:
grpc#32933
paulosjca pushed a commit to paulosjca/grpc that referenced this pull request May 4, 2023
…c#32935)

Fix: grpc#18075

From comments in grpc#18075, `CPython`
reinitialize the `GIL` after `pthread_atfork` child handler, thus we
shouldn't use any `GIL` related functions in child handler which is what
we're currently doing, this PR uses `os.register_at_fork` to replace
`pthread_atfork` to prevent any undesired bevahior.

This also seems to fixes a thread hanging issue cased by changes in
core: grpc#32869

### Testing:
* Passed existing fork tests. (Note that due to some issues in `Bazel`,
this change was not verified by `Bazel runs_per_test`).
* Tested by patch the core PR, was able to fix Python fork tests:
grpc#32933
wanlin31 pushed a commit that referenced this pull request May 18, 2023
)

Fix: #18075

From comments in #18075, `CPython`
reinitialize the `GIL` after `pthread_atfork` child handler, thus we
shouldn't use any `GIL` related functions in child handler which is what
we're currently doing, this PR uses `os.register_at_fork` to replace
`pthread_atfork` to prevent any undesired bevahior.

This also seems to fixes a thread hanging issue cased by changes in
core: #32869

### Testing:
* Passed existing fork tests. (Note that due to some issues in `Bazel`,
this change was not verified by `Bazel runs_per_test`).
* Tested by patch the core PR, was able to fix Python fork tests:
#32933
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

imported Specifies if the PR has been imported to the internal repository lang/Python python Pull requests that update Python code release notes: no Indicates if PR should not be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client-side Python fork support can hang on Python 3.7

2 participants