Repository navigation
Keep a thread's PDFium up until its last runtime shuts down - #128
Merged
Merged
Conversation
Two runtimes on one thread share that thread's PDFium on native: each is a new module object over the same library. Each calls EPDF_InitThread and EPDF_ShutdownThread once, and the first shutdown ran FPDF_DestroyLibrary for both. The runtime still open then read content with the thread's globals gone: EPDFAnnot_GetBlendMode crashed in CPDF_StreamContentParser::OnOperator on a null operator table (a read of address 0x8). Two local engines in one Node thread, one destroyed before the other, are enough. WASM is not affected: each runtime there has its own memory. EPDF_InitThread and EPDF_ShutdownThread are now counted per thread, and only the last EPDF_ShutdownThread tears PDFium down. A shutdown with no init before it still tears down, as FPDF_DestroyLibrary does. Test: EPDFViewEmbedderTest.ThreadLifecycleIsCounted, on a thread of its own. After two inits and one shutdown it reads an annotation's blend mode, which parses the appearance; after the last shutdown a new init starts PDFium again. Without the change it crashes at the line the engine crashed at.
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.
A second runtime on a thread could shut PDFium down under the first. On the native runtime, the one still open then crashed on its next content parse:
EPDFAnnot_GetBlendMode()read address 0x8 inCPDF_StreamContentParser::OnOperator. Two local engines in one Node thread, one destroyed before the other, are enough, and Node's default runtime is the native one. WASM is not affected. Found while checking #127 on the native runtime.Cause
With per-thread globals, a thread's PDFium is started by
EPDF_InitThread()and torn down byEPDF_ShutdownThread(), which wereFPDF_InitLibrary()andFPDF_DestroyLibrary()under another name. The engine makes one module object per runtime over the same native library, and each runtime calls the pair once. So with two runtimes on a thread:EPDF_InitThread(): PDFium starts.FPDF_DestroyLibrary()frees the thread's globals, among them the page module with the content-operator table, the font registry and the graphics module.g_opcodesis null.The crash was seen in an appearance stream (
CPDF_AnnotContext::SetForm), but nothing is wrong there: any content parse after step 3 does the same. On WASM each runtime is its own module instance with its own memory, so two runtimes never share a PDFium.Change
EPDF_InitThread()andEPDF_ShutdownThread()are counted per thread (fpdfsdk/epdf_threading.cpp). Only the lastEPDF_ShutdownThread()tears PDFium down.FPDF_DestroyLibrary()does. Nothing changes for a caller that uses the pair once.EPDF_TLS, like the state it guards. Without per-thread globals it counts for the process.public/epdf_view.hstates the counting, and the ledger'sper-thread-initrule names it and the test.The engine needs no change: each
WorkerHostalready calls the pair once.Tests
EPDFViewEmbedderTest.ThreadLifecycleIsCounted, inepdf_view_embeddertest.cpp. On a thread of its own, as a worker has: two inits, one shutdown, then an annotation's blend mode is read, which parses its appearance. Then the last shutdown, a new init, and the read again. Without the change it crashes atcpdf_streamcontentparser.cpp:643on address 0x8, as the engine did.It is compiled only with
EPDF_THREAD_LOCAL_GLOBALS: without per-thread globals the thread would share the harness's PDFium, and the test's last shutdown would tear that down. It only reads, so it is not in the writers list.Checked
macOS arm64, on 3b4fdaf:
EPDFViewEmbedderTest.*: 21 pass, none failing only in layer mode, no unlisted writer.check-fork-tags.py: no untagged block.check-c-api.py: ok.macOS arm64, on the same change before it was rebased onto #127 (on 47f0d99):
check-globals.pyon the release library: ok.annotation-export.test.tswithprefer: 'native': the worker exits on the released library (f2ea759), and 9 of 9 pass on this build.Linux x64 in Docker, on 3b4fdaf:
EPDFViewEmbedderTest.*: 21 pass, none failing only in layer mode, no unlisted writer.Not run: the ThreadSanitizer soak, WASM.
Left
WorkerHosts made over the same runtime object share one "initialized" entry in the engine'sbootstrap.ts, so the first to shut down still ends PDFium for both. Only hand-written tests do that.