Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

fix: use a lock for extracting text from pdf via mupdf to avoid cgo panic (issue #135) #136

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
10 changes: 9 additions & 1 deletion pkg/datastore/documentloader/pdf/mupdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
// Compile time check to ensure PDF satisfies the DocumentLoader interface.
var _ types.DocumentLoader = (*PDF)(nil)

var mupdfLock sync.Mutex

type PDFOptions struct {
// Password for encrypted PDF files.
Password string
Expand Down Expand Up @@ -88,6 +90,9 @@ func (l *PDF) Load(ctx context.Context) ([]vs.Document, error) {
docs := make([]vs.Document, 0, l.document.NumPage())
numPages := l.document.NumPage()

// We need a lock here, since MuPDF is not thread-safe and there are some edge cases that can cause a CGO panic.
// See https://github.com/gptscript-ai/knowledge/issues/135
mupdfLock.Lock()
g, childCtx := errgroup.WithContext(ctx)
g.SetLimit(l.opts.NumThread)
for pageNum := 0; pageNum < numPages; pageNum++ {
Expand Down Expand Up @@ -131,7 +136,10 @@ func (l *PDF) Load(ctx context.Context) ([]vs.Document, error) {
})
}

return docs, g.Wait()
err := g.Wait()
mupdfLock.Unlock()

return docs, err
}

// LoadAndSplit loads PDF documents from the provided reader and splits them using the specified text splitter.
Expand Down