purego: remove pooling of syscall#452
Open
TotallyGamerJet wants to merge 1 commit into
Open
Conversation
hajimehoshi
reviewed
May 23, 2026
Member
hajimehoshi
left a comment
There was a problem hiding this comment.
LGTM
My understanding is, introducing a pool broke some invariations for nosplit functions: is that correct?
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes the shared sync.Pool used for *syscallArgs during FFI calls, addressing a concurrency regression where concurrent calls could observe corrupted/swapped return values (issue #451). The change trades reduced allocations for correctness by ensuring each call uses a distinct syscallArgs instance.
Changes:
- Remove
sync.Pool-backed reuse of*syscallArgsand allocate freshsyscallArgsper call path. - Update syscall dispatch implementations (
syscall.go,syscall_32bit.go,syscall_ppc64le.go) to no longerGet/Putpooled structs. - Add a new concurrent malloc/free test intended to exercise the pointer-return path under contention.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| func.go | Removes global sync.Pool and stops pooling *syscallArgs in RegisterFunc call paths. |
| syscall.go | Stops pooling in SyscallN path by allocating a fresh syscallArgs. |
| syscall_32bit.go | Same as above for 32-bit/arm builds. |
| syscall_ppc64le.go | Same as above for ppc64le build path. |
| func_test.go | Adds a new concurrency test invoking malloc/free across goroutines. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| } | ||
|
|
||
| func TestRegisterFunc_(t *testing.T) { |
Comment on lines
+49
to
+66
| var alloc func(uint64) *byte | ||
| var free func(*byte) | ||
| purego.RegisterLibFunc(&alloc, libc, "malloc") | ||
| purego.RegisterLibFunc(&free, libc, "free") | ||
|
|
||
| var wg sync.WaitGroup | ||
|
|
||
| for i := 0; i < runtime.NumCPU(); i++ { | ||
| wg.Add(1) | ||
| go func(id int) { | ||
| defer wg.Done() | ||
| for j := 0; j < 400_000; j++ { | ||
| ptr := alloc(5) | ||
| if ptr == nil { | ||
| continue | ||
| } | ||
| free(ptr) | ||
| } |
Comment on lines
+54
to
+60
| var wg sync.WaitGroup | ||
|
|
||
| for i := 0; i < runtime.NumCPU(); i++ { | ||
| wg.Add(1) | ||
| go func(id int) { | ||
| defer wg.Done() | ||
| for j := 0; j < 400_000; j++ { |
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.
What issue is this addressing?
Closes #451
What type of issue is this addressing?
Bug
What this PR does | solves
This removes the pool which was occasionally causing issues. Unfortunately this does increase the memory cost of each call