-
Notifications
You must be signed in to change notification settings - Fork 3
addresses finalizer race condition #8
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
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
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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| module github.com/LadybugDB/go-ladybug/examples | ||
|
|
||
| go 1.20 | ||
| go 1.25 | ||
|
|
||
| replace github.com/LadybugDB/go-ladybug => ../ | ||
|
|
||
| require github.com/LadybugDB/go-ladybug v0.0.0 | ||
|
|
||
| require github.com/google/uuid v1.6.0 // indirect | ||
|
|
||
| require github.com/shopspring/decimal v1.4.0 // indirect |
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,203 @@ | ||
| package lbug | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "runtime" | ||
| "sync" | ||
| "testing" | ||
| ) | ||
|
|
||
| // The race only manifests when running MULTIPLE tests in batch, not in isolation. | ||
| // That is, if you run the test individually it won't fail. | ||
| // But if you run "go test -v" it will. | ||
| // | ||
| // Key insight: The issue is accumulated GC pressure across test sessions. | ||
| // When test A creates QueryResults and test B runs, the GC may finalize | ||
| // QueryResult A while test B's iteration is still in progress. | ||
|
|
||
| // The race occurs when: | ||
| // 1. QueryResult is finalized while FlatTuple.GetValue() is still accessing C memory | ||
| // 2. The GC runs during lbugValueToGoValue() and destroys the parent QueryResult | ||
| // 3. The finalizer calls lbug_query_result_destroy() on memory still in use | ||
| func TestFinalizerRaceCondition(t *testing.T) { | ||
| // Skip if running with -short (this test can be slow and flaky) | ||
| if testing.Short() { | ||
| t.Skip("skipping race condition test in short mode") | ||
| } | ||
|
|
||
| db, conn := setupTestDatabase(t) | ||
| defer db.Close() | ||
| defer conn.Close() | ||
|
|
||
| createTestData(t, conn, 100000) | ||
|
|
||
| const numGoroutines = 20 | ||
| const queriesPerGoroutine = 30 | ||
|
|
||
| var wg sync.WaitGroup | ||
| errChan := make(chan error, numGoroutines*queriesPerGoroutine) | ||
|
|
||
| for g := range numGoroutines { | ||
| wg.Add(1) | ||
| go func(goroutineID int) { | ||
| defer wg.Done() | ||
|
|
||
| for range queriesPerGoroutine { | ||
| // Query without storing result in a variable that persists | ||
| // This pattern allows the QueryResult to become "unreachable" quickly | ||
| if err := runQueryAndIterate(conn); err != nil { | ||
| errChan <- err | ||
| return | ||
| } | ||
|
|
||
| // Force GC to increase likelihood of triggering the race | ||
| runtime.GC() | ||
| } | ||
| }(g) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| close(errChan) | ||
|
|
||
| var errors []error | ||
| for err := range errChan { | ||
| errors = append(errors, err) | ||
| } | ||
|
|
||
| if len(errors) > 0 { | ||
| t.Fatalf("got %d errors during concurrent queries: %v", len(errors), errors[0]) | ||
| } | ||
| } | ||
|
|
||
| // setupTestDatabase creates an in-memory database with test schema. | ||
| // | ||
| // Returns the database and connection, which the caller must close. | ||
| func setupTestDatabase(t *testing.T) (*Database, *Connection) { | ||
| t.Helper() | ||
|
|
||
| db, err := OpenDatabase(":memory:", DefaultSystemConfig()) | ||
| if err != nil { | ||
| t.Fatalf("failed to open database: %v", err) | ||
| } | ||
|
|
||
| conn, err := OpenConnection(db) | ||
| if err != nil { | ||
| db.Close() | ||
| t.Fatalf("failed to open connection: %v", err) | ||
| } | ||
|
|
||
| schemas := []string{ | ||
| `CREATE NODE TABLE Node ( | ||
| id INT64, | ||
| name STRING, | ||
| fqn STRING, | ||
| category STRING, | ||
| file_path STRING, | ||
| PRIMARY KEY (id) | ||
| )`, | ||
| `CREATE REL TABLE CONNECTS ( | ||
| FROM Node TO Node, | ||
| label STRING | ||
| )`, | ||
| } | ||
|
|
||
| for _, schema := range schemas { | ||
| result, err := conn.Query(schema) | ||
| if err != nil { | ||
| conn.Close() | ||
| db.Close() | ||
| t.Fatalf("failed to create schema: %v", err) | ||
| } | ||
| result.Close() | ||
| } | ||
|
|
||
| return db, conn | ||
| } | ||
|
|
||
| // createTestData populates the database with synthetic test data. | ||
| // Creates nodes and relationships to simulate a real codebase. | ||
| // | ||
| // numNodes: number of DefinitionNode records to create | ||
| func createTestData(t *testing.T, conn *Connection, numNodes int) { | ||
| t.Helper() | ||
|
|
||
| const batchSize = 100 | ||
| for i := 0; i < numNodes; i += batchSize { | ||
| end := min(i + batchSize, numNodes) | ||
|
|
||
| for j := i; j < end; j++ { | ||
| query := fmt.Sprintf(` | ||
| CREATE (n:Node { | ||
| id: %d, | ||
| name: 'item_%d', | ||
| fqn: 'src/module%d.item_%d', | ||
| category: 'entity', | ||
| file_path: 'src/module%d.ext' | ||
| }) | ||
| `, j, j, j/10, j, j/10) | ||
|
|
||
| result, err := conn.Query(query) | ||
| if err != nil { | ||
| t.Fatalf("failed to insert node %d: %v", j, err) | ||
| } | ||
| result.Close() | ||
| } | ||
| } | ||
|
|
||
| for i := 0; i < numNodes-3; i++ { | ||
| for offset := 1; offset <= 3; offset++ { | ||
| query := fmt.Sprintf(` | ||
| MATCH (from:Node {id: %d}) | ||
| MATCH (to:Node {id: %d}) | ||
| CREATE (from)-[:CONNECTS {label: 'links'}]->(to) | ||
| `, i, i+offset) | ||
|
|
||
| result, err := conn.Query(query) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| result.Close() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // runQueryAndIterate executes a query returning OLAP-scale results (15k+ rows). | ||
| // This matches real-world usage patterns where large result sets create GC pressure. | ||
| // The query returns all CONNECTS relationships with multiple columns per row. | ||
| // | ||
| // Returns error if the query or iteration fails. | ||
| func runQueryAndIterate(conn *Connection) error { | ||
| result, err := conn.Query(` | ||
| MATCH (source:Node)-[r:CONNECTS]->(target:Node) | ||
| RETURN source.file_path, source.fqn, source.id, | ||
| target.file_path, target.fqn, target.id, | ||
| r.label | ||
| LIMIT 15000 | ||
| `) | ||
| if err != nil { | ||
| return fmt.Errorf("query failed: %w", err) | ||
| } | ||
| defer result.Close() | ||
|
|
||
| rowCount := 0 | ||
| for result.HasNext() { | ||
| row, err := result.Next() | ||
| if err != nil { | ||
| return fmt.Errorf("Next() failed at row %d: %w", rowCount, err) | ||
| } | ||
|
|
||
| // Access all 7 columns - each GetValue enters a race | ||
| for col := range uint64(7) { | ||
| _, err = row.GetValue(col) | ||
| if err != nil { | ||
| row.Close() | ||
| return fmt.Errorf("GetValue(%d) failed at row %d: %w", col, rowCount, err) | ||
| } | ||
| } | ||
| row.Close() | ||
|
|
||
| rowCount++ | ||
| } | ||
|
|
||
| return nil | ||
| } |
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/LadybugDB/go-ladybug | ||
|
|
||
| go 1.20 | ||
| go 1.25 | ||
|
|
||
| require github.com/google/uuid v1.6.0 | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.