Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions acceleration/internal/knowledge/extract/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,37 @@ 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
// 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()
// 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 := s.extractor.Extract(ctx, "manual.pdf", data)
_, err = document.Render(ctx, 1)

if err != nil {
s.Require().ErrorIs(err, context.Canceled)
}
}
s.Require().ErrorIs(err, context.DeadlineExceeded)
}

// A deadline that passes after the document opened kills the worker while the read closes it.
Expand Down Expand Up @@ -280,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)
Expand Down
5 changes: 5 additions & 0 deletions acceleration/internal/knowledge/extract/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Comment thread
kanat marked this conversation as resolved.
Resolved
return nil, stack.Wrap(fmt.Errorf("extract: open the PDF: %w", err))
}
engine, err := e.engine()
if err != nil {
return nil, err
Expand Down
Loading