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:
beads/crew/dave
2026-01-12 18:29:42 -08:00
committed by Steve Yegge
parent 6b9be4595a
commit ec1a32b9a8
8 changed files with 149 additions and 0 deletions

View File

@@ -9,6 +9,11 @@ import (
// GetExportHash retrieves the content hash of the last export for an issue.
// Returns empty string if no hash is stored (first export).
func (s *SQLiteStorage) GetExportHash(ctx context.Context, issueID 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 hash string
err := s.db.QueryRowContext(ctx, `
SELECT content_hash FROM export_hashes WHERE issue_id = ?
@@ -26,6 +31,11 @@ func (s *SQLiteStorage) GetExportHash(ctx context.Context, issueID string) (stri
// SetExportHash stores the content hash of an issue after successful export.
func (s *SQLiteStorage) SetExportHash(ctx context.Context, issueID, contentHash 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 export_hashes (issue_id, content_hash, exported_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
@@ -44,6 +54,11 @@ func (s *SQLiteStorage) SetExportHash(ctx context.Context, issueID, contentHash
// ClearAllExportHashes removes all export hashes from the database.
// This is primarily used for test isolation to force re-export of issues.
func (s *SQLiteStorage) ClearAllExportHashes(ctx context.Context) 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 export_hashes`)
if err != nil {
return fmt.Errorf("failed to clear export hashes: %w", err)
@@ -54,6 +69,11 @@ func (s *SQLiteStorage) ClearAllExportHashes(ctx context.Context) error {
// GetJSONLFileHash retrieves the stored hash of the JSONL file.
// Returns empty string if no hash is stored (bd-160).
func (s *SQLiteStorage) GetJSONLFileHash(ctx context.Context) (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 hash string
err := s.db.QueryRowContext(ctx, `
SELECT value FROM metadata WHERE key = 'jsonl_file_hash'
@@ -71,6 +91,11 @@ func (s *SQLiteStorage) GetJSONLFileHash(ctx context.Context) (string, error) {
// SetJSONLFileHash stores the hash of the JSONL file after export (bd-160).
func (s *SQLiteStorage) SetJSONLFileHash(ctx context.Context, fileHash 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 ('jsonl_file_hash', ?)