Skip to content

[AI-4108] A PDF read past its deadline takes no worker; deterministic deadline test - #2576

Merged
kanat merged 4 commits into
mainfrom
kanat/ai-4108-pdf-deadline-flake
Nov 26, 2026
Merged

kanat merged 4 commits into
mainfrom
kanat/ai-4108-pdf-deadline-flake

Conversation

@kanat

@kanat kanat commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Cause

TestReadingStopsAtTheDeadline read manual.pdf with 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.

Extract(ctx done)
  └─ readPDF → open
       ├─ pool.GetInstanceWithContext(ctx)   may still return a worker
       ├─ killOnDone(ctx)                    kill runs in a goroutine
       └─ read pages                         can finish first → err == nil   (flake)

Fix

open checks the context before it starts the engine or takes a worker. readPDF and renderPDF both 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.

TestReadingEveryFormatStopsAtTheDeadline had 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. TestDrawingAPageStopsAtTheDeadline pins the same check on drawing a page. TestAReadCancelledBeforeItStartsKillsItsWorkerBeforeClosingIt is removed: with the check in open it can no longer reach a worker, and TestClosingAReadWaitsForAKillThatBeganAfterTheDocumentOpened still catches a stop() that does not wait for the kill.

Failures of TestReadingStopsAtTheDeadline, same commands on both sides

run base head
-count=60 3 0
-count=40 -cpu 1 32 0
4 copies in parallel, -count=30 each 4 of 120 0 of 120

Mutations

mutation test that fails
check removed TestReadingStopsAtTheDeadline (engine started)
error not wrapped with %w TestReadingStopsAtTheDeadline (ErrorIs)
check moved after the engine start TestReadingStopsAtTheDeadline (engine started)
check moved from open into readPDF only TestDrawingAPageStopsAtTheDeadline
docx reader's context check removed TestReadingEveryFormatStopsAtTheDeadline

Ticket: AI-4108

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Reason error, so no refusal reaches the uploader from it. No err.Error() or %v of 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 kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

kanat and others added 3 commits November 26, 2026 15:49
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>
@kanat

kanat commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Round 1 follow-ups, head b339141:

  • R1.2: removed TestAReadCancelledBeforeItStartsKillsItsWorkerBeforeClosingIt (df0d653). With the check in open it cannot reach a worker. Making stop() skip the kill wait is still caught under -race by TestClosingAReadWaitsForAKillThatBeganAfterTheDocumentOpened (DATA RACE reported).
  • R1.3: TestReadingEveryFormatStopsAtTheDeadline now uses a context whose deadline has passed (b339141), all five formats kept. -count=300 -cpu 1: PASS (it failed 24 of 300 before). With the docx reader context check removed it fails on handbook.docx.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 1 passes 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.

@kanat
kanat marked this pull request as ready for review November 26, 2026 21:03
@kanat
kanat merged commit 05cc521 into main Nov 26, 2026
15 checks passed
@kanat
kanat deleted the kanat/ai-4108-pdf-deadline-flake branch November 26, 2026 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant