Skip to content

HTTPSever.handle_request not respecting timeout as documented between first and second request #102156

@eXhumer

Description

@eXhumer

Bug report

The HTTPServer times out as expected before receiving the first request and after receiving the second request. However, HTTPServer doesn't timeout as expected between the first and second request, blocking until the second request is received by HTTPServer.

Your environment

  • CPython versions tested on: 3.11.1
  • Operating system and architecture: Windows 11 22H2 x64

Example code demonstrating the issue

from http.server import BaseHTTPRequestHandler, HTTPServer


class HelloWorldHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.end_headers()
        self.wfile.write("Hello World!".encode("utf8"))


if __name__ == "__main__":
    httpd = HTTPServer(("localhost", 65012), HelloWorldHTTPRequestHandler)
    httpd.timeout = 1

    while True:
        try:
            print("handle_request")
            httpd.handle_request()

        except KeyboardInterrupt:
            break

Example code output

handle_request
handle_request
handle_request
handle_request
127.0.0.1 - - [22/Feb/2023 14:49:14] "GET / HTTP/1.1" 200 -
handle_request
127.0.0.1 - - [22/Feb/2023 14:49:29] "GET / HTTP/1.1" 200 -
handle_request
handle_request
handle_request
handle_request
handle_request
handle_request
handle_request
handle_request
handle_request
KeyboardInterrupt

Linked PRs

Metadata

Metadata

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions