os._exit(0) followed at once the end of the drawings, so the request
threads never sent their error (a stream's error event, a 5xx without a
stream): the client saw a bare cut. Past the flag, the main thread now
ends the drawings in progress in a loop, the list re-read each turn,
until no place is taken any more or STOP_S at most, then exits.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
subprocess.Popen (a fork/exec) ran under Drawing.live_lock, so every
drawing's start, every end and the stop waited behind it. Popen now
runs before the lock; under the lock, if the stop is in progress the
process just launched is ended at once and the drawing refused, else
it is added to the drawings in progress.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The handler ran on the main thread and took Drawing.live_lock, which the
main thread also takes while stopping: a second SIGTERM (a stop sent
twice) then waited on itself until the SIGKILL. The handler is now
on_sigterm(srv): it sets the stopping flag and starts the server's
shutdown, and takes no lock.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The drain of a body a refusal leaves unread had only the BODY_S
inactivity delay per read, no total bound: a client sending a byte every
BODY_S - 1 held a thread for ever (ThreadingHTTPServer has no cap on
threads). The drain now reads with read1 and stops at the deadline, and
the connection is kept only if the whole announced body was read.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 404 path read the Content-Length bytes even when a Transfer-Encoding
was also present, then kept the connection: the tail of the chunked body
was parsed as the next request. The 411 now comes first, before any
read, with Connection: close and the connection closed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
_drain marked the body read whole after reading only MAX_BODY of an
announced larger body, so the 413 kept the connection open and the rest
of the body was parsed as the next request. The 413 is now refused
before any read, and _read_whole is true only when the whole announced
body (at most MAX_BODY) was read.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
At SIGTERM the list of the drawings in progress was copied once after serve_forever ended: a thread that had already taken a place could still create a Drawing after it, that nobody ended; and the interpreter exit then waited for the ThreadPoolExecutor join, the reads in flight up to ENGINE_S, so TimeoutStopSec ended it a SIGKILL. A stopping flag is now set at SIGTERM and checked before a drawing is created, under the lock the drawing registers itself with, so no drawing can appear past the copy; the request is refused cleanly (a 503 with a Retry-After). The drawings in progress are ended, then the front exits at once, os._exit(0) after the cleanup, without waiting for the reads in flight.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A document read without stream had no total bound at the front (the proxy's own bound says nothing about the front: a page's read at the engine goes up to ENGINE_S = 600 s). Past NONSTREAM_S (7200 s, 2 h: 1000 pages at the 0.22 page/s measured beside the 27B under load take 4545 s) the front now stops the document, its reads and its drawing container as when the client leaves, and answers a 504 whose message says to send "stream": true for a long document, without a Retry-After. A streamed document has no such bound, its events come as they are read.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The place was taken before rfile.read(n) and the socket had no delay: a client that announced a Content-Length and then went silent, or sent drop by drop, held one of the two places for ever. The body's read now runs under a socket delay of BODY_S (60 s) without a byte; past it the front answers a 408, gives the place back and closes the connection. Past the body, the socket waits as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A 429 or a 503 answered while the announced body is still unread makes the close send a RST: the proxy, which still sends up to 90 MiB, fails to write and answers a 503 try again, and the 429 with its Retry-After is lost. The body (bounded as the request is, MAX_BODY at most) is now read and thrown away in chunks of 1 MiB, never kept, before the refusal is answered; a fully drained refusal keeps the connection usable. A 429 still takes no place, so it costs no body memory.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The front's SIGTERM handler was os._exit(0): Drawing.end() never ran, and the drawing containers (3 GiB and four cores each, up to 1000 pages) kept drawing after the lane stopped, even when the cockpit stopped it to free memory before a text lane's Load. The handler now stops the server, the front ends every drawing in progress before exiting 0, and ExecStartPre and ExecStopPost remove any qwen38-ocr-pages-* container left (docker by its path, a - in front), for a front that ended hard.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A refusal was answered while the place was still held (the finally ran after the response was written), so a client refused once could be answered 429 on its very next request. The place is now given back in the finally, before the refusal's response is sent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The drawing thread submitted every page to the engine as soon as it was drawn, and the pool's queue held them all: a document of 1000 pages of 1 to 3 MiB each piled up in the front's memory beside the lane that serves. The drawing now takes a place (a semaphore of the read parallelism, OCR_PARALLEL) before reading a page's pictures from the container, and the place is given back when the page is read. The 60 s no-line delay only runs while the pipe is read, so waiting for a place never triggers it, and ocr_pages.py clears its alarm before writing a page, so a container waiting to write is never cut.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It was a pipe read only at the end of the container's stdout: more than 64 KiB of warnings (Pillow, PDFium, a broken TIFF's pages) blocked the container on a full pipe, which was then stopped for having given no line in 60 s. A thread now drains it while the container runs, keeping only the tail for the error message.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The two documents' slots were taken only after the whole body was read (up to 86 MiB), parsed and base64-decoded, so each 429 cost that memory beside the lane that serves. The place is now taken after the header checks and given back in a finally, so a refusal never reads the body.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/ocr-lane.md says a page that could not be read is named in errors, with its index and why, and the others come back. The front put it in pages too, as an empty Markdown with an error key, and counted it in usage_info.pages_processed. It is now only in errors (still sent as a page event with its error in a stream), and pages_processed counts the pages that came back.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- a POST /v1/ocr holds a slot while its document is read (DOCS = 2, as the cockpit's relay already bounds it): a third request is a 429 with Retry-After, in the front's error JSON, and the slot is given back however the document ends (ended, failed, client gone)
- frames() reads the drawing container with a delay of DRAW_S = 2 x PAGE_S, PAGE_S read from ocr_pages.py: no line within it and the container is stopped the way work()'s finally stops it, and the document ends with an error saying a page was not drawn in that many seconds
- tests: a third document gets a 429 (200 before), a container that gives nothing gets an answer within DRAW_S (a client timeout before), and the busy count is back to 0 after each kind of end
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>