python / python/cpython

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

Open
#117,208 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules topic-socket type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

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

Reproduce the behavior with the ms and mc scripts in the issue, using a Unix socket with listen(0) and repeated timed connections. Read the linked socket timeout and exception documentation, then inspect the relevant CPython socket implementation and linked PR gh-134688. Done means the documented timeout abstraction is preserved when the listen backlog is full, with regression coverage for the reproduced case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.