python / python/cpython

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

Abierto
#119,534 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el gh-119542 enlazado y, después, inspecciona Lib/multiprocessing/queues.py y Lib/multiprocessing/synchronize.py en torno a la inicialización de Queue y SEM_VALUE_MAX. Reproduce el comportamiento con queue-test.py usando los valores de maxsize 32767 y 32768; done debería solucionar el fallo de argumento no válido para valores superiores al límite de semaphore compatible.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
operating-systems
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.