π fix(tsan): initialise libxml2 at load time via C constructor#34
Merged
Conversation
Add `XMLNode.lineNumber: Int?` backed by `xmlGetLineNo()` from libxml2. Returns the 1-based source line number recorded during parsing, or `nil` when unavailable (programmatically created nodes or libxml2 returns 0). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Under ThreadSanitizer, TSan's pthread interceptors interfere with libxml2's internal pthread_once guard inside xmlInitParser(), leaving the encoding-handler table NULL when xmlGetCharEncodingHandler is called from a concurrent thread β causing a SEGV in the warm-up that was meant to prevent that very crash. Add an __attribute__((constructor)) in SwiftXMLCoderCShim.c that runs xmlInitParser() and the UTF-8 handler warm-up at library-load time, in a single-threaded context, before TSan instruments any pthread primitives. This guarantees libxml2's global tables are fully populated before any thread is created. LibXML2.initializeOnce now only calls xmlInitParser() as a no-op safeguard for static-linking configurations that do not run constructors. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
__attribute__((constructor))inSwiftXMLCoderCShim.cthat callsxmlInitParser()and pre-warms the UTF-8 encoding handler at library-load time, in a single-threaded contextswiftxmlcoder_warm_encoding_handlercall fromLibXML2.initializeOnce(now handled by the constructor)test_encodeDecode_concurrent_roundTripreported on PR β¬οΈ chore: bump actions/github-script from 7 to 8Β #31Root cause
Under TSan, its pthread interceptors interfere with libxml2's internal
pthread_onceguard insidexmlInitParser(). This leaves the encoding-handler table NULL whenxmlGetCharEncodingHandleris subsequently called β causing a SEGV in the very warm-up that was meant to prevent that crash. Moving initialisation to a C constructor ensures it runs before TSan instruments any pthread primitive.Test plan
Concurrency (TSan, ubuntu-22.04)) passesπ€ Generated with Claude Code