Repository navigation
fix(data-store): plug the FFI leak, NUL-terminate the Go get result, skip empty syncs #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
21c06c8
fix(data-store): free FFI strings and NUL-terminate the Go get result
vaidehi-figma b7961e8
fix(data-store): skip empty background sync updates instead of forwar…
vaidehi-figma 489088a
fix(data-store): free the callback args in Go, not in Rust
vaidehi-figma d7ed00e
fix(data-store): unmarshal the Go set args straight out of the C buffer
vaidehi-figma a5d86f6
fix(data-store): keep the Go get result alive until the core has read it
vaidehi-figma 1e23b94
fix(persistent-storage): apply the same three FFI fixes to the Go bin…
vaidehi-figma 1378e8b
test(go): cover the data store and persistent storage FFI fixes
vaidehi-figma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "strings" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| // Big enough that the runtime gives each buffer its own block, so a finalizer | ||
| // on it tracks that buffer and nothing else. | ||
| func testPayload(seed string) string { | ||
| return strings.Repeat(seed, 1024) | ||
| } | ||
|
|
||
| func readRetained(ptr *byte, length int) string { | ||
| return string(unsafe.Slice(ptr, length)) | ||
| } | ||
|
|
||
| // retainWatched retains payload and reports through freed when the buffer | ||
| // behind the returned pointer becomes unreachable. The pointer is deliberately | ||
| // not returned: the point is to test what keeps the buffer alive once the | ||
| // callback that produced it has no reference left, so only the keeper does. | ||
| func retainWatched(k *ResultKeeper, payload string, freed *atomic.Bool) { | ||
| ptr := k.Retain(payload) | ||
| runtime.SetFinalizer(ptr, func(*byte) { | ||
| freed.Store(true) | ||
| }) | ||
| } | ||
|
|
||
| func collectGarbage() { | ||
| for range 5 { | ||
| runtime.GC() | ||
| time.Sleep(10 * time.Millisecond) | ||
| } | ||
| } | ||
|
|
||
| func waitForFreed(freed *atomic.Bool) bool { | ||
| deadline := time.Now().Add(2 * time.Second) | ||
| for time.Now().Before(deadline) { | ||
| if freed.Load() { | ||
| return true | ||
| } | ||
| collectGarbage() | ||
| } | ||
|
|
||
| return freed.Load() | ||
| } | ||
|
|
||
| func TestResultKeeperCopiesAndNulTerminates(t *testing.T) { | ||
| k := NewResultKeeper(1) | ||
| payload := testPayload("a") | ||
|
|
||
| ptr := k.Retain(payload) | ||
|
|
||
| if got := readRetained(ptr, len(payload)); got != payload { | ||
| t.Errorf("retained content does not match: got %d bytes, want %d", len(got), len(payload)) | ||
| } | ||
|
|
||
| if terminator := readRetained(ptr, len(payload)+1)[len(payload)]; terminator != 0 { | ||
| t.Errorf("retained buffer is not NUL-terminated: trailing byte is %q", terminator) | ||
| } | ||
| } | ||
|
|
||
| func TestResultKeeperRetainsEmptyResult(t *testing.T) { | ||
| k := NewResultKeeper(1) | ||
|
|
||
| ptr := k.RetainBytes(nil) | ||
|
|
||
| if ptr == nil { | ||
| t.Fatal("expected a pointer to a lone terminator, got nil") | ||
| } | ||
|
|
||
| if *ptr != 0 { | ||
| t.Errorf("expected an empty NUL-terminated buffer, got %q", *ptr) | ||
| } | ||
| } | ||
|
|
||
| // The core reads the pointer with CStr::from_ptr after the callback has | ||
| // returned, so the buffer has to survive a GC with no Go reference to it other | ||
| // than the keeper's. | ||
| func TestResultKeeperHoldsResultAfterCallbackReturns(t *testing.T) { | ||
| k := NewResultKeeper(1) | ||
| var freed atomic.Bool | ||
|
|
||
| retainWatched(k, testPayload("b"), &freed) | ||
| collectGarbage() | ||
|
|
||
| if freed.Load() { | ||
| t.Error("retained buffer was collected while the keeper still held it") | ||
| } | ||
|
|
||
| // Without this the keeper is dead after its last use and the buffer goes | ||
| // with it - which is the same reason a slice local to the callback cannot | ||
| // be handed to the core. | ||
| runtime.KeepAlive(k) | ||
| } | ||
|
|
||
| func TestResultKeeperReleasesDisplacedResults(t *testing.T) { | ||
| const slots = 4 | ||
| k := NewResultKeeper(slots) | ||
|
|
||
| var freed atomic.Bool | ||
| retainWatched(k, testPayload("c"), &freed) | ||
|
|
||
| for range slots - 1 { | ||
| k.Retain(testPayload("d")) | ||
| } | ||
|
|
||
| collectGarbage() | ||
| if freed.Load() { | ||
| t.Fatal("buffer was released while still within the keeper's slots") | ||
| } | ||
|
|
||
| k.Retain(testPayload("e")) | ||
|
|
||
| if !waitForFreed(&freed) { | ||
| t.Error("displaced buffer was never released - the keeper retains without bound") | ||
| } | ||
|
|
||
| runtime.KeepAlive(k) | ||
| } | ||
|
|
||
| func TestResultKeeperConcurrentRetain(t *testing.T) { | ||
| const ( | ||
| goroutines = 8 | ||
| perRoutine = 200 | ||
| ) | ||
|
|
||
| k := NewResultKeeper(goroutines) | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := range goroutines { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
|
|
||
| payload := testPayload(string(rune('A' + i))) | ||
| for range perRoutine { | ||
| ptr := k.Retain(payload) | ||
| if got := readRetained(ptr, len(payload)); got != payload { | ||
| t.Errorf("goroutine %d read back a buffer it did not retain", i) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
| } | ||
| wg.Wait() | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
claude is also saying that
persistent_storage.goneeds these same fixes, if we want to add that to a follow up?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did it here instead of a follow-up! it's the same three bugs and the same review