python / python/cpython

multiprocessing.sharedctypes: SynchronizedBase calls get_context(force=True), which raises TypeError

Open
#156,062 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-multiprocessing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

multiprocessing.sharedctypes.SynchronizedBase.__init__ calls get_context(force=True) when it is given neither a lock nor a ctx:

https://github.com/python/cpython/blob/20e6c2fc7c9fe5a8d62aa8f1b66a1bd72a24cf74/Lib/multiprocessing/sharedctypes.py#L187-L193

But get_context() has never accepted a force argument (set_start_method() does), so constructing one of the synchronized wrapper classes directly, without a lock or a context, fails:

>>> from multiprocessing.sharedctypes import RawValue, Synchronized
>>> Synchronized(RawValue('i', 7))
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    Synchronized(RawValue('i', 7))
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File ".../multiprocessing/sharedctypes.py", line 192, in __init__
    ctx = ctx or get_context(force=True)
TypeError: DefaultContext.get_context() got an unexpected keyword argument 'force'

The same applies to SynchronizedArray and SynchronizedString. The module-level helpers (Value(), Array(), synchronized()) are not affected because they always resolve a context themselves and pass it down, which is why this has gone unnoticed: the line dates from the commit that introduced contexts (b1694cf588, bpo-18999, 2013).

The fix is to call get_context() without the bogus argument, which is what synchronized() does a few lines above. Found by running pylint over the standard library (unexpected-keyword-arg). I have a PR with a regression test ready.

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-156063

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review Lib/multiprocessing/sharedctypes.py, especially SynchronizedBase.init and the direct Synchronized, SynchronizedArray, and SynchronizedString construction paths. Reproduce the reported TypeError, then inspect the regression test mentioned in the issue and verify that direct construction succeeds without a lock or context.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.