python / python/cpython

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

未關閉
#117,208 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

extension-modules topic-socket type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

使用 issue 中的 msmc 指令碼重現該行為,使用帶有 listen(0) 的 Unix socket,並重複建立定時連線。閱讀連結的 socket timeout 與 exception 文件,然後檢查相關的 CPython socket 實作以及連結的 PR gh-134688。當 listen backlog 已滿時,文件所述的 timeout abstraction 仍獲得保留,並且針對重現案例具有回歸測試涵蓋,即視為完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
networking
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。