Skip to content

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

Merged
encukou merged 6 commits into
python:mainfrom
encukou:baseproxy-serial
Nov 13, 2024
Merged

gh-71936: Fix race condition in multiprocessing.Pool#124973
encukou merged 6 commits into
python:mainfrom
encukou:baseproxy-serial

Conversation

@encukou

@encukou encukou commented Oct 4, 2024

Copy link
Copy Markdown
Member

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>

@gpshead gpshead left a comment

Copy link
Copy Markdown
Member

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.

Comment thread Lib/multiprocessing/managers.py Outdated
Comment thread Lib/multiprocessing/managers.py Outdated
@encukou encukou merged commit ba088c8 into python:main Nov 13, 2024
@encukou encukou deleted the baseproxy-serial branch November 13, 2024 09:25
@basnijholt

basnijholt commented Nov 15, 2024

Copy link
Copy Markdown

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

Will this be back ported?

@encukou

encukou commented Nov 15, 2024

Copy link
Copy Markdown
Member Author

Will this be back ported?

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

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

Copy link
Copy Markdown

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

@miss-islington-app

Copy link
Copy Markdown

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

bedevere-app Bot commented Nov 15, 2024

Copy link
Copy Markdown

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

bedevere-app Bot commented Nov 15, 2024

Copy link
Copy Markdown

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 only 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>
picnixz pushed a commit to picnixz/cpython that referenced this pull request Dec 8, 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.

Co-Authored-By: Akinori Hattori <hattya@gmail.com>
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…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.

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