python / python/cpython

`OSError` when creating a `multiprocessing.Queue` with `maxsize > 32767`

Offen
#119,534 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

While exploring multiprocessing.Queue, I encountered an exception which was confusing and I couldn't reconcile from Googling. Briefly, it seems that the Queue object allows providing a maxsize which exceeds from .synchronize import SEM_VALUE_MAX. If that occurs, the exception below is raised.

I suspect but haven't confirmed that this issue exists in main as the maxsize does not carry an upper bound.

If this is acknowledged as a bug, or at least a source of improvement for the user experience, I would be happy to provide a pull request that (1) sets an upper bound as SEM_VALUE_MAX and (2) provides a Warning that the upper bound was exceeded.

$ cat queue-test.py 
import multiprocessing as mp
import sys

class mw:
    def __init__(self, q):
        self.q = q

    def writer(self):
        for i in range(5):
            self.q.put(i)
            print(f"writer placed {i}", flush=True)
        self.q.put(None)

    def reader(self):
        while True:
            i = self.q.get()
            if i is None:
                break
            print(f"reader got {i}", flush=True)

if __name__ == '__main__':
    maxsize = int(sys.argv[1])
    ctx = mp.get_context('spawn')
    qq = ctx.Queue(maxsize)

    f = mw(qq)
    wp = ctx.Process(target=f.writer)
    wp.start()
    rp = ctx.Process(target=f.reader)
    rp.start()
    wp.join()
    rp.join()
$ python queue-test.py 32767
writer placed 0
writer placed 1
writer placed 2
writer placed 3
writer placed 4
reader got 0
reader got 1
reader got 2
reader got 3
reader got 4
$ python queue-test.py 32768
Traceback (most recent call last):
  File "/Users/dtmcdonald/queue-test.py", line 24, in <module>
    qq = ctx.Queue(maxsize)
         ^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/context.py", line 103, in Queue
    return Queue(maxsize, ctx=self.get_context())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/queues.py", line 49, in __init__
    self._sem = ctx.BoundedSemaphore(maxsize)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/context.py", line 88, in BoundedSemaphore
    return BoundedSemaphore(value, ctx=self.get_context())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/synchronize.py", line 152, in __init__
    SemLock.__init__(self, SEMAPHORE, value, value, ctx=ctx)
  File "/Users/dtmcdonald/miniconda3/envs/duckdb/lib/python3.11/multiprocessing/synchronize.py", line 57, in __init__
    sl = self._semlock = _multiprocessing.SemLock(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument
$ python -c "from multiprocessing.synchronize import SEM_VALUE_MAX; print(SEM_VALUE_MAX)"
32767
CPython versions tested on:

3.11, 3.12

Operating systems tested on:

macOS

Linked PRs
  • gh-119542

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften gh-119542 und untersuche anschließend Lib/multiprocessing/queues.py sowie Lib/multiprocessing/synchronize.py im Zusammenhang mit der Initialisierung von Queue und SEM_VALUE_MAX. Reproduziere das Verhalten mit queue-test.py unter Verwendung der maxsize-Werte 32767 und 32768; done sollte den Fehler wegen eines ungültigen Arguments für Werte oberhalb des unterstützten Semaphore-Limits beheben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
operating-systems
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.