Skip to content
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

Fix race with table init channel #53

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,6 @@ func (t *genTable[Obj]) RegisterInitializer(txn WriteTxn, name string) func(Writ
slices.Clone(table.pendingInitializers),
func(n string) bool { return n == name },
)
if !table.initialized && len(table.pendingInitializers) == 0 {
close(table.initWatchChan)
table.initialized = true
}
}
})
}
Expand Down
14 changes: 14 additions & 0 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,18 @@ func (txn *txn) Commit() ReadTxn {
root := *db.root.Load()
root = slices.Clone(root)

var initChansToClose []chan struct{}

// Insert the modified tables into the root tree of tables.
for pos, table := range txn.modifiedTables {
if table != nil {
// Check if tables become initialized. We close the channel only after
// we've swapped in the new root so that one cannot get a snapshot of
// an uninitialized table after observing the channel closing.
if !table.initialized && len(table.pendingInitializers) == 0 {
initChansToClose = append(initChansToClose, table.initWatchChan)
table.initialized = true
}
root[pos] = *table
}
}
Expand All @@ -480,6 +489,11 @@ func (txn *txn) Commit() ReadTxn {
txn.Notify()
}

// Notify table initializations
for _, ch := range initChansToClose {
close(ch)
}

txn.db.metrics.WriteTxnDuration(
txn.handle,
txn.tableNames,
Expand Down
Loading