Skip to content
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

gh-71936: Fix race condition in multiprocessing.Pool #124973

Merged
merged 6 commits into from
Nov 13, 2024

Conversation

encukou
Copy link
Member

@encukou encukou commented Oct 4, 2024

This is based on @hattya's gh-98274.
As far as I can tell, the race condition is caused by using id() as a unique identifier, assuming that it can't be reused.

@hattya's PR supplements the remote object's id with the id of the proxy. But since _decref is called after the proxy is deleted, that id could be reused as well. Note that this is a theoretical concern, I wasn't able to reproduce it.

This PR uses a counter (with locked store&increment) to get unique values for _idset.

Unfortunately, I have no idea how to test this :/

Proxes of shared objects register a Finalizer in BaseProxy._incref(), and it
will call BaseProxy._decref() when it is GCed. This may cause a race condition
with Pool(maxtasksperchild=None) on Windows.

A connection would be closed and raised TypeError when a GC occurs between
_ConnectionBase._check_writable() and _ConnectionBase._send_bytes() in
_ConnectionBase.send() in the second or later task, and a new object
is allocated that shares the id() of a previously deleted one.

Instead of using the id() of the token (or the proxy), use a unique,
non-reusable number.

Co-Authored-By: Akinori Hattori <hattya@gmail.com>
Copy link
Member

@gpshead gpshead left a comment

Choose a reason for hiding this comment

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

A couple possible things to improve, but good regardless.

I'm not worried about a regression test for this. Hard, and clearly better than what came before.

Lib/multiprocessing/managers.py Outdated Show resolved Hide resolved
Lib/multiprocessing/managers.py Outdated Show resolved Hide resolved
@encukou encukou merged commit ba088c8 into python:main Nov 13, 2024
36 checks passed
@encukou encukou deleted the baseproxy-serial branch November 13, 2024 09:25
@basnijholt
Copy link

basnijholt commented Nov 15, 2024

As I confirmed in #71936 (comment), this solves the problem for me.

Will this be back ported?

@encukou
Copy link
Member Author

encukou commented Nov 15, 2024

Will this be back ported?

I see no issues in our post-merge testing, so, yes :)

@encukou encukou added needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels Nov 15, 2024
@miss-islington-app
Copy link

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 15, 2024
…124973)

* pythongh-71936: Fix race condition in multiprocessing.Pool

Proxes of shared objects register a Finalizer in BaseProxy._incref(), and it
will call BaseProxy._decref() when it is GCed. This may cause a race condition
with Pool(maxtasksperchild=None) on Windows.

A connection would be closed and raised TypeError when a GC occurs between
_ConnectionBase._check_writable() and _ConnectionBase._send_bytes() in
_ConnectionBase.send() in the second or later task, and a new object
is allocated that shares the id() of a previously deleted one.

Instead of using the id() of the token (or the proxy), use a unique,
non-reusable number.

(cherry picked from commit ba088c8)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-Authored-By: Akinori Hattori <hattya@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Nov 15, 2024

GH-126869 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 15, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 15, 2024
…124973)

* pythongh-71936: Fix race condition in multiprocessing.Pool

Proxes of shared objects register a Finalizer in BaseProxy._incref(), and it
will call BaseProxy._decref() when it is GCed. This may cause a race condition
with Pool(maxtasksperchild=None) on Windows.

A connection would be closed and raised TypeError when a GC occurs between
_ConnectionBase._check_writable() and _ConnectionBase._send_bytes() in
_ConnectionBase.send() in the second or later task, and a new object
is allocated that shares the id() of a previously deleted one.

Instead of using the id() of the token (or the proxy), use a unique,
non-reusable number.

(cherry picked from commit ba088c8)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-Authored-By: Akinori Hattori <hattya@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Nov 15, 2024

GH-126870 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Nov 15, 2024
encukou added a commit that referenced this pull request Nov 15, 2024
… (GH-126870)

Proxes of shared objects register a Finalizer in BaseProxy._incref(), and it
will call BaseProxy._decref() when it is GCed. This may cause a race condition
with Pool(maxtasksperchild=None) on Windows.

A connection would be closed and raised TypeError when a GC occurs between
_ConnectionBase._check_writable() and _ConnectionBase._send_bytes() in
_ConnectionBase.send() in the second or later task, and a new object
is allocated that shares the id() of a previously deleted one.

Instead of using the id() of the token (or the proxy), use a unique,
non-reusable number.

(cherry picked from commit ba088c8)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Akinori Hattori <hattya@gmail.com>
encukou added a commit that referenced this pull request Nov 15, 2024
… (GH-126869)

Proxes of shared objects register a Finalizer in BaseProxy._incref(), and it
will call BaseProxy._decref() when it is GCed. This may cause a race condition
with Pool(maxtasksperchild=None) on Windows.

A connection would be closed and raised TypeError when a GC occurs between
_ConnectionBase._check_writable() and _ConnectionBase._send_bytes() in
_ConnectionBase.send() in the second or later task, and a new object
is allocated that shares the id() of a previously deleted one.

Instead of using the id() of the token (or the proxy), use a unique,
non-reusable number.

(cherry picked from commit ba088c8)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Akinori Hattori <hattya@gmail.com>
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.

3 participants