python / python/cpython

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

Aperta
#117,208 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

extension-modules topic-socket type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il comportamento con gli script ms e mc nell’issue, usando un socket Unix con listen(0) e connessioni temporizzate ripetute. Leggi la documentazione collegata sui timeout e sulle eccezioni dei socket, quindi esamina l’implementazione pertinente dei socket di CPython e la PR collegata gh-134688. Il lavoro è completo quando l’astrazione di timeout documentata viene preservata quando il backlog di ascolto è pieno, con copertura di regressione per il caso riprodotto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
networking
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.