fix[issue #142]: SIGSEV during concurrent rendering of PDF in Image() or ImageDPI()#147
Open
intx4 wants to merge 1 commit into
Open
fix[issue #142]: SIGSEV during concurrent rendering of PDF in Image() or ImageDPI()#147intx4 wants to merge 1 commit into
intx4 wants to merge 1 commit into
Conversation
…r concurrent use across multiple Document instances, while clearly documenting that a single Document is still not concurrent-safe.
Author
|
This fixes #142 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes intermittent SIGSEGVs during concurrent rendering by adding proper MuPDF lock wiring during context creation and tightening resource lifetimes in the cgo path.
Root cause:
fz_new_context_impwas called withnillock callbacks, which violates MuPDF’s multithreading contract when rendering overlaps across contexts/goroutines.What changed
Added MuPDF lock callbacks (
fz_locks_context) in cgo:lock/unlockcallbacks andFZ_LOCK_MAXmutex array.fz_new_context_imp(...)for everyDocument.Hardened context/document lifecycle:
Close()cleanup ordering explicit and defensive (stream -> doc -> context -> C buffer).Fixed
NewFromMemorylifetime hazards:C.CBytes) instead of relying on Go slice memory.fz_keep_streamand released temporary stream ref.Close().Added/updated documentation:
Documentinstances,Document(including racing withClose) is not supported.fitz_cgo.godocumenting same-document concurrency limits and lock-free method expectations.Why this fixes the crash
MuPDF requires caller-provided locks for safe multithreaded use. With lock callbacks registered, internal shared paths (alloc/glyph/freetype and related global state) are synchronized correctly across concurrent rendering workloads, preventing unsafe overlap that could lead to SIGSEGV.
Compatibility
New,NewFromMemory,NewFromReader,ImageDPI, etc.) continue to work unchanged.Tests
concurrency.pdffile for the test to work. I could not share the one I used as it contains sensitive data (it is an artifact from my app production environment)Notes
Documentper worker/goroutine for parallel conversion workflows.