python / python/cpython

Connecting a socket in timeout mode to a server with full listen backlog raises BlockingIOError

Abierto
#117,208 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

extension-modules topic-socket 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:

I'm working on a Debian bookworm system. Please consider this executable script ms:

#!/usr/bin/python3 -i

from pathlib import Path
import socket

SOCK = 'stream.sock'
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
Path(SOCK).unlink(missing_ok=True)
s.bind(SOCK)
s.listen(0)

and this one mc:

#!/usr/bin/python3

import socket

SOCK = 'stream.sock'
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.settimeout(3)
s.connect(SOCK)

Let's start ./ms, and once it reaches the Python prompt, run ./mc twice in another terminal:

$ ./mc
$ ./mc
Traceback (most recent call last):
  File "/home/wferi/block/./mc", line 8, in <module>
    s.connect(SOCK)
BlockingIOError: [Errno 11] Resource temporarily unavailable

The first[^1] invocation runs to completion all right, but all succeeding ones immediately fail with BlockingIOError, as if the connecting socket was in non-blocking mode, not in timeout mode. I understand that the documentation[^2] notes

at the operating system level, sockets in timeout mode are internally set in non-blocking mode

so that must be where this comes from, but such an implementation detail shouldn't leak out of the abstraction, as the documentation also states that

A socket object can be in one of three modes: blocking, non-blocking, or timeout.

and

exception BlockingIOError: Raised when an operation would block on an object (e.g. socket) set for non-blocking operation. [^3]

[^1]: In general the first backlog+1, but I set the listen backlog to 0 in the code of ms above.
[^2]: https://docs.python.org/3/library/socket.html#socket-timeouts
[^3]: https://docs.python.org/3/library/exceptions.html

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs
  • gh-134688

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

Reproduce el comportamiento con los scripts ms y mc del issue, usando un socket Unix con listen(0) y conexiones temporizadas repetidas. Lee la documentación enlazada sobre timeouts y excepciones de sockets y, después, inspecciona la implementación relevante de sockets de CPython y el PR enlazado gh-134688. Se considera terminado cuando se conserva la abstracción de timeout documentada cuando la cola de escucha está llena, con cobertura de regresión para el caso reproducido.

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

Evaluación

Stack tecnológico
python
Área
networking
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.