diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index c0ec8808ae..18105763c2 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -1490,7 +1490,16 @@ def _read_request_body(self, length): if left <= 0: raise TimeoutError("request body read timed out") self.connection.settimeout(left) - chunk = self.rfile.read(min(remaining, _CRAWL_READ_CHUNK_BYTES)) + # read1(), not read(): the buffered read() keeps + # recv-ing until the full count arrives, and a client + # dribbling bytes faster than the socket timeout keeps + # every recv alive, so read() outlives the absolute + # deadline (#8906; the _fetch_crawl pattern above). + # read1() returns after a single raw read, so the loop + # rechecks the deadline between chunks. + chunk = self.rfile.read1(min(remaining, _CRAWL_READ_CHUNK_BYTES)) + if time.monotonic() >= deadline: + raise TimeoutError("request body read timed out") if not chunk: raise ValueError("request body ended before Content-Length bytes arrived") chunks.append(chunk) diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 5194580880..f33c70592c 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -2238,6 +2238,56 @@ def test_stalled_in_cap_body_times_out_and_server_recovers(self): self.assertEqual(status, 408) self.assert_get_still_answers() + def test_trickling_in_cap_body_is_cut_off_at_the_deadline(self): + # #8906 (Codex P1 on PR #8705): a client dribbling bytes at + # intervals below the socket timeout must still be cut off at + # the absolute deadline. The buffered read() this guards + # against keeps recv-ing until the declared length arrives - + # every recv succeeds, so read() never returns and the + # deadline is never rechecked; read1() returns after one raw + # read, so the loop rechecks the deadline between chunks. + # Oracle: the server answers 408 at ~the deadline (1s patched) + # while the drip would need ~20s to satisfy Content-Length. + orig = app._CRAWL_REQUEST_READ_TIMEOUT_SECONDS + app._CRAWL_REQUEST_READ_TIMEOUT_SECONDS = 1 + stop = threading.Event() + + def drip(sock): + try: + while not stop.is_set(): + sock.sendall(b"x") + time.sleep(0.2) + except OSError: + pass + + try: + with socket.create_connection( + ("127.0.0.1", self.port), timeout=8.0 + ) as sock: + sock.sendall(self.post_head(b"100")) + sender = threading.Thread(target=drip, args=(sock,), daemon=True) + sender.start() + started = time.monotonic() + chunks = [] + try: + while True: + chunk = sock.recv(65536) + if not chunk: + break + chunks.append(chunk) + except socket.timeout: + raise AssertionError( + "server did not cut off the trickling body promptly" + ) + elapsed = time.monotonic() - started + finally: + stop.set() + app._CRAWL_REQUEST_READ_TIMEOUT_SECONDS = orig + status = int(b"".join(chunks).split(b" ", 2)[1]) + self.assertEqual(status, 408) + self.assertLess(elapsed, 5.0) + self.assert_get_still_answers() + def test_well_formed_control_still_200(self): body = json.dumps({"url": "http://example.com/"}).encode() status, raw = self.raw_request(