purego: remove pooling of syscall (#452)#453
Merged
Merged
Conversation
(cherry picked from commit 11272d2) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes reuse/pooling of *syscall15Args during FFI calls to eliminate a concurrency bug where concurrent calls could observe swapped/corrupted return-value pointers (issue #451). It updates both the main RegisterFunc path and the SysV syscall_syscall15X path, and adds a regression test intended to exercise concurrent pointer returns.
Changes:
- Remove the global
sync.Poolof*syscall15Argsand allocate a freshsyscall15Argsper call inRegisterFunc. - Stop using the pool in
syscall_syscall15X(SysV path) by allocating a newsyscall15Argsfor each call. - Add a concurrent pointer-return regression test using
malloc/freefrom the system C runtime.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| syscall_sysv.go | Removes pooled syscall15Args usage in syscall_syscall15X to avoid cross-goroutine corruption. |
| func.go | Removes sync.Pool usage and allocates syscall15Args per RegisterFunc invocation to fix the race. |
| func_test.go | Adds a concurrent pointer-return test to help prevent regressions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+54
to
+69
| 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) | ||
| } | ||
| }(i) | ||
| } | ||
|
|
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 is the same as #452, but for 0.10. As a simple cherry-pick didn't work, I needed some work so I'd like to test with GitHub Actions. After the tests pass, I'll self-approve this.