fix(storage): add reconnectMu RLock protection to prevent race condition (#1054)
Add missing reconnectMu.RLock() protection to storage methods that were vulnerable to the same race condition fixed in GH#607. The FreshnessChecker can trigger reconnect() which closes s.db while queries are in flight, causing "database is closed" errors during daemon export operations. Protected methods: - labels.go: GetLabelsForIssues (GetLabels intentionally unprotected - called from GetIssue which holds lock) - comments.go: GetIssueComments, GetCommentsForIssues - dependencies.go: GetDependencyCounts, GetDependencyRecords, GetAllDependencyRecords, GetDependencyTree, loadDependencyGraph - config.go: SetConfig, GetConfig, GetAllConfig, DeleteConfig, SetMetadata, GetMetadata - dirty.go: MarkIssueDirty, GetDirtyIssues, GetDirtyIssueHash, GetDirtyIssueCount - events.go: GetEvents, GetStatistics, GetMoleculeProgress - hash.go: All hash methods - hash_ids.go: GetNextChildID, ensureChildCounterUpdated (getNextChildNumber unprotected - called internally) Internal helpers called from already-locked contexts intentionally omit RLock to avoid deadlock (Go's RWMutex doesn't support recursive locking). Fixes: bd-vx7fp Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Executed-By: beads/crew/dave Rig: beads Role: crew
This commit is contained in:
committed by
Steve Yegge
parent
6b9be4595a
commit
ec1a32b9a8
@@ -8,6 +8,11 @@ import (
|
||||
|
||||
// SetConfig sets a configuration value
|
||||
func (s *SQLiteStorage) SetConfig(ctx context.Context, key, value string) error {
|
||||
// Hold read lock during database operations to prevent reconnect() from
|
||||
// closing the connection mid-query (GH#607 race condition fix)
|
||||
s.reconnectMu.RLock()
|
||||
defer s.reconnectMu.RUnlock()
|
||||
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO config (key, value) VALUES (?, ?)
|
||||
ON CONFLICT (key) DO UPDATE SET value = excluded.value
|
||||
@@ -17,6 +22,11 @@ func (s *SQLiteStorage) SetConfig(ctx context.Context, key, value string) error
|
||||
|
||||
// GetConfig gets a configuration value
|
||||
func (s *SQLiteStorage) GetConfig(ctx context.Context, key string) (string, error) {
|
||||
// Hold read lock during database operations to prevent reconnect() from
|
||||
// closing the connection mid-query (GH#607 race condition fix)
|
||||
s.reconnectMu.RLock()
|
||||
defer s.reconnectMu.RUnlock()
|
||||
|
||||
var value string
|
||||
err := s.db.QueryRowContext(ctx, `SELECT value FROM config WHERE key = ?`, key).Scan(&value)
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -27,6 +37,11 @@ func (s *SQLiteStorage) GetConfig(ctx context.Context, key string) (string, erro
|
||||
|
||||
// GetAllConfig gets all configuration key-value pairs
|
||||
func (s *SQLiteStorage) GetAllConfig(ctx context.Context) (map[string]string, error) {
|
||||
// Hold read lock during database operations to prevent reconnect() from
|
||||
// closing the connection mid-query (GH#607 race condition fix)
|
||||
s.reconnectMu.RLock()
|
||||
defer s.reconnectMu.RUnlock()
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT key, value FROM config ORDER BY key`)
|
||||
if err != nil {
|
||||
return nil, wrapDBError("query all config", err)
|
||||
@@ -46,6 +61,11 @@ func (s *SQLiteStorage) GetAllConfig(ctx context.Context) (map[string]string, er
|
||||
|
||||
// DeleteConfig deletes a configuration value
|
||||
func (s *SQLiteStorage) DeleteConfig(ctx context.Context, key string) error {
|
||||
// Hold read lock during database operations to prevent reconnect() from
|
||||
// closing the connection mid-query (GH#607 race condition fix)
|
||||
s.reconnectMu.RLock()
|
||||
defer s.reconnectMu.RUnlock()
|
||||
|
||||
_, err := s.db.ExecContext(ctx, `DELETE FROM config WHERE key = ?`, key)
|
||||
return wrapDBError("delete config", err)
|
||||
}
|
||||
@@ -78,6 +98,11 @@ func (s *SQLiteStorage) GetOrphanHandling(ctx context.Context) OrphanHandling {
|
||||
|
||||
// SetMetadata sets a metadata value (for internal state like import hashes)
|
||||
func (s *SQLiteStorage) SetMetadata(ctx context.Context, key, value string) error {
|
||||
// Hold read lock during database operations to prevent reconnect() from
|
||||
// closing the connection mid-query (GH#607 race condition fix)
|
||||
s.reconnectMu.RLock()
|
||||
defer s.reconnectMu.RUnlock()
|
||||
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO metadata (key, value) VALUES (?, ?)
|
||||
ON CONFLICT (key) DO UPDATE SET value = excluded.value
|
||||
@@ -87,6 +112,11 @@ func (s *SQLiteStorage) SetMetadata(ctx context.Context, key, value string) erro
|
||||
|
||||
// GetMetadata gets a metadata value (for internal state like import hashes)
|
||||
func (s *SQLiteStorage) GetMetadata(ctx context.Context, key string) (string, error) {
|
||||
// Hold read lock during database operations to prevent reconnect() from
|
||||
// closing the connection mid-query (GH#607 race condition fix)
|
||||
s.reconnectMu.RLock()
|
||||
defer s.reconnectMu.RUnlock()
|
||||
|
||||
var value string
|
||||
err := s.db.QueryRowContext(ctx, `SELECT value FROM metadata WHERE key = ?`, key).Scan(&value)
|
||||
if err == sql.ErrNoRows {
|
||||
|
||||
Reference in New Issue
Block a user