From d4919dbfc327b61d05f4287c6991b2bdc940e259 Mon Sep 17 00:00:00 2001 From: Kanat Date: Thu, 26 Nov 2026 15:21:01 -0500 Subject: [PATCH 1/4] AI-4108: a PDF read past its deadline takes no worker Co-Authored-By: Claude Sonnet 5.5 --- .../internal/knowledge/extract/extract_test.go | 14 +++++++++++--- acceleration/internal/knowledge/extract/pdf.go | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/acceleration/internal/knowledge/extract/extract_test.go b/acceleration/internal/knowledge/extract/extract_test.go index d0c47ab3a5..07eb38c822 100644 --- a/acceleration/internal/knowledge/extract/extract_test.go +++ b/acceleration/internal/knowledge/extract/extract_test.go @@ -196,13 +196,21 @@ func (s *ExtractSuite) TestAPDFWithMorePagesThanAllowedIsRefused() { s.requireReason(err, TooLarge) } +// The deadline has passed before the read starts, so the answer cannot depend on how fast the +// read is: it fails, and it does not even start the engine. A timeout of a nanosecond is not +// that: its timer may not have fired by the time the read ends. func (s *ExtractSuite) TestReadingStopsAtTheDeadline() { - hurried := New(Options{Timeout: time.Nanosecond}) - defer hurried.Close() + extractor := New(Options{}) + defer extractor.Close() + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) + defer cancel() - _, err := hurried.Extract(context.Background(), "manual.pdf", s.fixture("manual.pdf")) + _, err := extractor.Extract(ctx, "manual.pdf", s.fixture("manual.pdf")) s.Require().ErrorIs(err, context.DeadlineExceeded) + _, ok := ReasonOf(err) + s.False(ok, "a file stopped at the deadline is not called corrupt") + s.Nil(extractor.pdf, "a read past its deadline starts no engine") } // A context cancelled before the read starts kills the worker while the read opens the diff --git a/acceleration/internal/knowledge/extract/pdf.go b/acceleration/internal/knowledge/extract/pdf.go index ebe402a430..edb597bc22 100644 --- a/acceleration/internal/knowledge/extract/pdf.go +++ b/acceleration/internal/knowledge/extract/pdf.go @@ -86,6 +86,11 @@ type pdfSession struct { // open takes a worker and opens the document in it. PDFium takes no context, so a file // that keeps it busy past the deadline is stopped by killing the worker it runs in. func (e *Extractor) open(ctx context.Context, data []byte) (*pdfSession, error) { + // A deadline that has passed already takes no worker: the pool may still hand one out, and + // the kill then races a read that is short enough to finish first. + if err := ctx.Err(); err != nil { + return nil, stack.Wrap(fmt.Errorf("extract: open the PDF: %w", err)) + } engine, err := e.engine() if err != nil { return nil, err From adb9b287a09e1c45e75a8d07421238dfac8b48ad Mon Sep 17 00:00:00 2001 From: Kanat Date: Thu, 26 Nov 2026 15:49:11 -0500 Subject: [PATCH 2/4] AI-4108: Pin the deadline check on drawing a PDF page Co-Authored-By: Claude Sonnet 5.5 --- .../internal/knowledge/extract/extract_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/acceleration/internal/knowledge/extract/extract_test.go b/acceleration/internal/knowledge/extract/extract_test.go index 07eb38c822..b9358cbdc1 100644 --- a/acceleration/internal/knowledge/extract/extract_test.go +++ b/acceleration/internal/knowledge/extract/extract_test.go @@ -213,6 +213,22 @@ func (s *ExtractSuite) TestReadingStopsAtTheDeadline() { s.Nil(extractor.pdf, "a read past its deadline starts no engine") } +// Drawing a page is a read of its own: a deadline that has passed fails it before it takes a +// worker. The extractor is closed, so a draw that did take the engine would fail with that +// instead. +func (s *ExtractSuite) TestDrawingAPageStopsAtTheDeadline() { + extractor := New(Options{}) + document, err := extractor.Extract(context.Background(), "manual.pdf", s.fixture("manual.pdf")) + s.Require().NoError(err) + s.Require().NoError(extractor.Close()) + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) + defer cancel() + + _, err = document.Render(ctx, 1) + + s.Require().ErrorIs(err, context.DeadlineExceeded) +} + // A context cancelled before the read starts kills the worker while the read opens the // document, and the open that fails then closes the worker. Run under -race, as CI does, a // close while the kill is still going on is a data race in PDFium's instance. The kill runs in From df0d653f673bcff9406e18da0aa1d0a68eb494fe Mon Sep 17 00:00:00 2001 From: Kanat Date: Thu, 26 Nov 2026 15:51:55 -0500 Subject: [PATCH 3/4] AI-4108: Drop the test that can no longer reach a worker Co-Authored-By: Claude Sonnet 5.5 --- .../internal/knowledge/extract/extract_test.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/acceleration/internal/knowledge/extract/extract_test.go b/acceleration/internal/knowledge/extract/extract_test.go index b9358cbdc1..00e2bf2351 100644 --- a/acceleration/internal/knowledge/extract/extract_test.go +++ b/acceleration/internal/knowledge/extract/extract_test.go @@ -229,24 +229,6 @@ func (s *ExtractSuite) TestDrawingAPageStopsAtTheDeadline() { s.Require().ErrorIs(err, context.DeadlineExceeded) } -// A context cancelled before the read starts kills the worker while the read opens the -// document, and the open that fails then closes the worker. Run under -race, as CI does, a -// close while the kill is still going on is a data race in PDFium's instance. The kill runs in -// a goroutine of its own, so a read can also finish before it and return no error. -func (s *ExtractSuite) TestAReadCancelledBeforeItStartsKillsItsWorkerBeforeClosingIt() { - data := s.fixture("manual.pdf") - for range 200 { - ctx, cancel := context.WithCancel(context.Background()) - cancel() - - _, err := s.extractor.Extract(ctx, "manual.pdf", data) - - if err != nil { - s.Require().ErrorIs(err, context.Canceled) - } - } -} - // A deadline that passes after the document opened kills the worker while the read closes it. // Run under -race, a close that does not wait for the kill is a data race in PDFium's // instance. Such a close can also leave the pool's lock held, so the next open never returns: From b339141b5535deb33ff027e809357110a53d2a39 Mon Sep 17 00:00:00 2001 From: Kanat Date: Thu, 26 Nov 2026 15:52:07 -0500 Subject: [PATCH 4/4] AI-4108: Give the every-format deadline test a deadline that has already passed Co-Authored-By: Claude Sonnet 5.5 --- acceleration/internal/knowledge/extract/extract_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/acceleration/internal/knowledge/extract/extract_test.go b/acceleration/internal/knowledge/extract/extract_test.go index 00e2bf2351..d565aa9116 100644 --- a/acceleration/internal/knowledge/extract/extract_test.go +++ b/acceleration/internal/knowledge/extract/extract_test.go @@ -286,11 +286,14 @@ func (s *ExtractSuite) TestAWorkerKilledWhileAPageIsWalkedFailsTheRead() { } func (s *ExtractSuite) TestReadingEveryFormatStopsAtTheDeadline() { - hurried := New(Options{Timeout: time.Nanosecond}) - defer hurried.Close() + extractor := New(Options{}) + defer extractor.Close() + // The deadline has passed already: a timeout of a nanosecond is done only once its timer runs. + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) + defer cancel() for _, name := range []string{"handbook.docx", "handbook.odt", "handbook.rtf", "policies.doc", "menu.xlsx"} { - _, err := hurried.Extract(context.Background(), name, s.fixture(name)) + _, err := extractor.Extract(ctx, name, s.fixture(name)) s.Require().ErrorIs(err, context.DeadlineExceeded, name) _, ok := ReasonOf(err)