Repository navigation
[AI-4108] A PDF read past its deadline takes no worker; deterministic deadline test - #2576
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
kanat
left a comment
There was a problem hiding this comment.
VERDICT: GO
Security review, round 1, head d4919db.
Boundaries: Extractor.Extract -> open (pdf.go) is reached by the ingest job on an uploaded file. The PR adds one early ctx.Err() check and takes no worker for a context that is done. It adds no entry point and no new reader of caller data.
- Error text: the new error says only that the open stopped at the deadline, the same text the existing post-open path returns. It is not a
Reasonerror, so no refusal reaches the uploader from it. Noerr.Error()or%vof a cause in the diff. - Guard mutation: removing the check fails
TestReadingStopsAtTheDeadline(engine started). 1 of 1 killed. - The kill-on-deadline path is code the PR does not change. Nothing new becomes reachable through the check.
No findings.
kanat
left a comment
There was a problem hiding this comment.
VERDICT: NO-GO
Reviewed d4919dbf merged onto c351776d (the base already contained). CI on this draft: go and integration are skipped (they run at gh pr ready). The change itself is sound: a PDF read with a live context takes the same path as base (the new ctx.Err() is nil), and only an already-done context is refused before the engine starts. On the head, TestReadingStopsAtTheDeadline passes at -count=100 (default cpu), -cpu 1 -count=100, 4 copies in parallel, and -race -count=20; go test -race ./internal/knowledge/... passes.
On whether a read that finishes after the deadline needs an error: base returns the text then, in every format, and a check after the read would change a live-context result. The check at the start is enough for the promise this test makes.
[Should fix] R1.1 pdf.go:89: a mutation survives. Moving the check from open into readPDF (so renderPDF is no longer covered) leaves the whole extract suite green. Add a test: a fresh Extractor, a document from Extract("manual.pdf"), extractor.Close(), then document.Render(pastDeadlineCtx, 1) must satisfy ErrorIs(err, context.DeadlineExceeded). Checked in a scratch copy: passes on the head, fails under the mutation (the error is "the extractor is closed") and on base.
[Nit] R1.2 extract_test.go:220: TestAReadCancelledBeforeItStartsKillsItsWorkerBeforeClosingIt can no longer reach a worker, since a cancelled context is now refused before the pool. It checks nothing about a kill racing a close, and its comment is stale ("a read can also finish before it and return no error"). Delete it or make the cancel race the open. Coverage is not lost: the mutation "stop() does not wait for the kill" is still caught by TestClosingAReadWaitsForAKillThatBeganAfterTheDocumentOpened.
[Question] R1.3 extract_test.go:290: TestReadingEveryFormatStopsAtTheDeadline has the same cause and flakes: a 1 ns timeout, and ctx.Err() turns non-nil only once the timer goroutine runs. On the head test binary, -count=300: -cpu 1 24 failures (docx, odt, xlsx, doc), -cpu 2 5, -cpu 4 8, -cpu 10 3; rtf did not fail. The readers check at docx.go:232, odt.go:120, doc.go:168, rtf.go:207, xlsx.go:33. Same fix as here (a context whose deadline is already past). xlsx belongs to #2550, so this goes to a ticket, not into this PR.
Mutations: 7 checked on the new check and its neighbours, 1 survived (R1.1).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ady passed Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Round 1 follow-ups, head b339141:
|
kanat
left a comment
There was a problem hiding this comment.
VERDICT: GO
Reviewed b339141 merged onto origin/main 25b4f15 (main moved one commit that touches no knowledge/ file). The delta since d4919db touches only acceleration/internal/knowledge/extract/extract_test.go. The XLSX code and tests are untouched.
Prior findings
- R1.1 fixed (adb9b28). With the open's ctx check moved into readPDF only, TestDrawingAPageStopsAtTheDeadline fails ("the extractor is closed"). It passes on head.
- R1.2 fixed (df0d653). The test that could not reach a worker is deleted. The stop-without-waiting-for-the-kill mutant still fails TestClosingAReadWaitsForAKillThatBeganAfterTheDocumentOpened under -race (data race reports, and its half-minute guard).
- R1.3 fixed (b339141). TestReadingEveryFormatStopsAtTheDeadline uses a deadline that has already passed.
-count=300 -cpu 1passes 300 of 300; round 1 had 24 failures. Removing any one reader's ctx check (docx, odt, rtf, doc, xlsx) makes the test fail on that format.
New findings: none.
Checks
go test -race -count=1 ./internal/knowledge/extract/: ok.go test -count=1 ./internal/knowledge/...on the head merged onto origin/main: ok.- gofmt and go vet (with and without the integration tag) on the package: clean.
- Mutants: 7 checked, 0 survived.
- CI: the draft runs go and integration as skipped,
Analyze (go)was pending at check time. Not a failure.
Cause
TestReadingStopsAtTheDeadlinereadmanual.pdfwith a 1 ns timeout. A done context does not stop the worker pool from handing out a worker, and the kill runs in its own goroutine, so a short read can finish before it and return no error.Fix
openchecks the context before it starts the engine or takes a worker.readPDFandrenderPDFboth go through it.The test now passes a context whose deadline is already past, so the answer no longer depends on timing. It also asserts that no engine was started.
TestReadingEveryFormatStopsAtTheDeadlinehad the same cause (a 1 ns timer, 24 of 300 failures with-cpu 1). It now reads every format with a context whose deadline has passed.TestDrawingAPageStopsAtTheDeadlinepins the same check on drawing a page.TestAReadCancelledBeforeItStartsKillsItsWorkerBeforeClosingItis removed: with the check inopenit can no longer reach a worker, andTestClosingAReadWaitsForAKillThatBeganAfterTheDocumentOpenedstill catches astop()that does not wait for the kill.Failures of
TestReadingStopsAtTheDeadline, same commands on both sides-count=60-count=40 -cpu 1-count=30eachMutations
TestReadingStopsAtTheDeadline(engine started)%wTestReadingStopsAtTheDeadline(ErrorIs)TestReadingStopsAtTheDeadline(engine started)openintoreadPDFonlyTestDrawingAPageStopsAtTheDeadlineTestReadingEveryFormatStopsAtTheDeadlineTicket: AI-4108
🤖 Generated with Claude Code