`BaseHTTPRequestHandler` hides `TimeoutError` from `handle_error()`
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- python
- 領域
- backend, networking
調査の方向性
BaseHTTPRequestHandler と issue に記載されたリクエスト処理経路から始め、続いて提供されたテストケースを再現して、どの TimeoutError 例外が handle_error() に到達するかを確認します。ハンドラーが発生させたタイムアウトが handle_error() を通じて報告され、既存のリクエストソケットのタイムアウト動作が失われなければ完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug report
If anything in your method handlers (e.g. do_GET()) ends up throwing a TimeoutError, then that unhandled exception is never forwarded to handle_error(), and hence you do not get a chance to do whatever reporting you have set up there.
The cause of this is an undocumented timeout handling that was added in bpo-6267. It blindly assumes that any TimeoutError must come from the request socket and that it was requested and hence expected. That is not the case for any mildly complex request handler that might communicate with other services.
The timeline for this issue is:
?.? to 2.6: No exceptions are hidden from handle_error()
2.7 to 3.2: recv() and send() timeouts are hidden. Other timeouts, e.g. connect(), are not.
3.3 to 3.9: Same situation, but by pure luck since socket.timeout was not converted to TimeoutError yet
3.10: All timeouts are hidden, not just for recv() and send()
Your environment
- CPython versions tested on: 2.7.18, 3.2.3, 3.5.10, 3.10.7
- Operating system and architecture: Debian 7 x86_64 and Fedora 35 x86_64
Test case
This test case should give the output:
Got error!
Got error!
But instead gives:
127.0.0.1 - - [25/Nov/2022 13:48:49] Request timed out: TimeoutError(110, 'Connection timed out')
127.0.0.1 - - [25/Nov/2022 13:48:50] Request timed out: TimeoutError('timed out')
For 3.9 or older you get a mix.
Code: (change simulate if you want to have the actual socket code throw the exceptions)
#!/usr/bin/python3
import os
import errno
import socket
import threading
from time import sleep
try:
from http.server import HTTPServer, BaseHTTPRequestHandler
except ImportError:
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
simulate = True
class ConnectRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if simulate:
raise socket.error(errno.ETIMEDOUT, "Timeout")
else:
socket.create_connection(("1.2.3.4", 9000))
class RecvRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if simulate:
raise socket.timeout(errno.ETIMEDOUT, "Timeout")
else:
(sock, dummy) = socket.socketpair()
sock.settimeout(1)
while sock.recv(1024):
pass
class Server(HTTPServer):
def handle_error(self, request, client_address):
print("Got error!")
def run_connect_server():
s = Server(("127.0.0.1", 8998),
ConnectRequestHandler)
s.handle_request()
def run_recv_server():
s = Server(("127.0.0.1", 8999),
RecvRequestHandler)
s.handle_request()
threading.Thread(target=run_connect_server).start()
threading.Thread(target=run_recv_server).start()
sleep(0.5)
s = socket.create_connection(("127.0.0.1", 8998))
s.send(b'GET / HTTP/1.0\r\n\r\n')
while s.recv(1024):
pass
s.close()
s = socket.create_connection(("127.0.0.1", 8999))
s.send(b'GET / HTTP/1.0\r\n\r\n')
while s.recv(1024):
pass
s.close()
sleep(0.5)
Linked PRs
- gh-99806
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 558
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
docs pending
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
build type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
stdlib topic-email type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
似ている issue
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
bancolombia/sentinel#23 ·
-
test md オープンCI
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100