purego: vendor v0.9.0 with concurrent-FFI race revert#13
Closed
nsaini-figma wants to merge 1 commit into
Closed
Conversation
Adds purego/ as a subdir module mirroring statsig-go/ and
binaries-linux-gnu/. Contents are upstream ebitengine/purego at tag
v0.9.0 verbatim, with one local patch reverting the package-wide
sync.Pool of *syscall15Args (introduced by upstream PR #282 and
reaffirmed in #328) back to a per-call stack allocation.
The pool optimization saved one allocation per FFI call but introduced
a race where two goroutines dispatching purego-registered functions
concurrently can observe each other's return values. In statsig-go
consumers (labmate is the in-tree example) this surfaces as SIGSEGV
in runtime.memmove on non-canonical pointers, glibc 'double free or
corruption (out)', nil-deref at the *gateJson read in
GetFeatureGateWithOptions, and — most insidiously — silently-swapped
feature-flag evaluation results that pass type checks downstream.
Reverting the pool resolves the race in 137M+ ops of the minimal
purego-only repro (32 goroutines × 60s, zero mismatches) and 37M+ ops
of the full statsig-go gate-eval workload (no crashes, no
double-frees). Allocation overhead is acceptable; correctness has to
come first.
Module path inside purego/go.mod is intentionally kept as
github.com/ebitengine/purego so that internal imports continue to
resolve. Consumers redirect via go.mod replace:
replace github.com/ebitengine/purego =>
github.com/figma/statsig-server-core/purego v0.9.0-figma1
When upstream lands a real fix, drop the replace line and delete this
directory. See purego/FIGMA-FORK.md for ongoing maintenance notes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
6 tasks
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
Adds
purego/as a new subdir module mirroringstatsig-go/andbinaries-linux-gnu/. Contents are upstreamebitengine/puregov0.9.0 verbatim except for an 8-line revert infunc.goandsyscall_sysv.gothat removes the racysync.Poolof*syscall15Argsintroduced by upstream PR #282 and reaffirmed in PR #328.Consumers redirect to this vendored copy via one new
replaceline in their owngo.mod. No change tostatsig-go/itself.Supersedes PR #12 (binding-side
sync.Mutexworkaround). #12 should be closed once this lands.Why
A statsig-go consumer was hitting recurring FFI SIGSEGVs ("memmove on non-canonical pointer" / "double free or corruption (out)" / nil-deref at
*gateJson) which narrowed to a concurrent-FFI-return-value race in purego v0.9.x. Two goroutines dispatching purego-registered functions in flight at the same time can have their return pointers swapped between callers. Discrimination matrix written up separately; the relevant facts:libstatsig_ffi.sois called from plain C with 32 pthreads under identical workload (16M+ ops clean). So it's not Rust, not libc malloc, not the kernel.char *alloc(uint64_t seed)+ Go driver with N goroutines — reproduces the swap within seconds at workers=2. Each goroutine uses a disjoint seed range; two goroutines observably get back the other's buffer.thePoolrecycling infunc.go:310-311andsyscall_sysv.go:18-19(back to pre-#282 stack allocation) eliminates the race in 137M+ ops of the minimal repro and 37M+ ops of the full statsig-go gate-eval workload.This vendored revert is the workaround pending an upstream fix. When upstream lands one, drop the
replaceline in consumers and deletepurego/from this repo. The revert is intentionally minimal (onlythePool.Get/Putremoved in two files) to make that a one-line revert.What changed
purego/containing all of upstreamebitengine/purego v0.9.0.// FIGMA PATCHcomments:purego/func.go:310-318— revert PR #282'sthePool.Get/Putto stack-allocatedsyscall15Args.purego/syscall_sysv.go:17-29— matching revert for theSyscallNpath from PR #328, including restoring//go:nosplit.purego/go.moddeclaresmodule github.com/ebitengine/purego(unchanged from upstream) so that internal imports continue to resolve. Thereplacedirective in consumer modules handles redirection.purego/FIGMA-FORK.mddocuments the patch, consumer wiring, and removal procedure..github/,.gitignore, andexamples/are excluded (would create CI / dependabot noise in this repo).Invariants worth calling out
purego/go.modkeepsgithub.com/ebitengine/purego. Cross-package imports inside the vendored copy continue to resolve. Thereplacesubstitution is what redirects consumers.thePooldeclaration retained. It's no longer used but is left in place to minimize the diff. Unused package-level vars don't fail Go compilation. Drop on next rebase if upstream removes it too.purego/v0.9.0-figma1, consumers addreplace github.com/ebitengine/purego => github.com/figma/statsig-server-core/purego v0.9.0-figma1to their owngo.mod.Test plan
go build ./...insidepurego/— clean.replace— ~245k ops/sec sustained, zero crashes, zero mismatches. Same workload against unpatched purego v0.9.0 reliably crashes within ~5 seconds.go test ./purego/...once CI is wired (purego ships a substantial test suite — should be run on the vendored copy with the patch).purego/v0.9.0-figma1, validate consumer integration end-to-end by pointing a consumer'sgo.modreplaceat the new tag and running its test suite + a soak.🤖 Generated with Claude Code