Context propagation with graceful cancellation (bd-rtp, bd-yb8, bd-2o2)
Complete implementation of signal-aware context propagation for graceful cancellation across all commands and storage operations. Key changes: 1. Signal-aware contexts (bd-rtp): - Added rootCtx/rootCancel in main.go using signal.NotifyContext() - Set up in PersistentPreRun, cancelled in PersistentPostRun - Daemon uses same pattern in runDaemonLoop() - Handles SIGINT/SIGTERM for graceful shutdown 2. Context propagation (bd-yb8): - All commands now use rootCtx instead of context.Background() - sqlite.New() receives context for cancellable operations - Database operations respect context cancellation - Storage layer propagates context through all queries 3. Cancellation tests (bd-2o2): - Added import_cancellation_test.go with comprehensive tests - Added export cancellation test in export_test.go - Tests verify database integrity after cancellation - All cancellation tests passing Fixes applied during review: - Fixed rootCtx lifecycle (removed premature defer from PersistentPreRun) - Fixed test context contamination (reset rootCtx in test cleanup) - Fixed export tests missing context setup Impact: - Pressing Ctrl+C during import/export now cancels gracefully - No database corruption or hanging transactions - Clean shutdown of all operations Tested: - go build ./cmd/bd ✓ - go test ./cmd/bd -run TestImportCancellation ✓ - go test ./cmd/bd -run TestExportCommand ✓ - Manual Ctrl+C testing verified 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -13,14 +13,13 @@ func TestAdaptiveIDLength_E2E(t *testing.T) {
|
||||
t.Skip("skipping slow E2E test in short mode")
|
||||
}
|
||||
// Create in-memory database
|
||||
db, err := New(":memory:")
|
||||
ctx := context.Background()
|
||||
db, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Initialize with prefix
|
||||
if err := db.SetConfig(ctx, "issue_prefix", "test"); err != nil {
|
||||
t.Fatalf("Failed to set prefix: %v", err)
|
||||
@@ -121,14 +120,13 @@ func formatTitle(format string, i int) string {
|
||||
|
||||
func TestAdaptiveIDLength_CustomConfig(t *testing.T) {
|
||||
// Create in-memory database
|
||||
db, err := New(":memory:")
|
||||
ctx := context.Background()
|
||||
db, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Initialize with custom config
|
||||
if err := db.SetConfig(ctx, "issue_prefix", "test"); err != nil {
|
||||
t.Fatalf("Failed to set prefix: %v", err)
|
||||
|
||||
@@ -90,7 +90,10 @@ func getCachedOrGenerateDB(b *testing.B, cacheKey string, generateFn func(contex
|
||||
b.Logf("This is a one-time operation that will be cached for future runs...")
|
||||
b.Logf("Expected time: ~1-3 minutes for 10K issues, ~2-6 minutes for 20K issues")
|
||||
|
||||
store, err := New(dbPath)
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
store, err := New(ctx, dbPath)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to create storage: %v", err)
|
||||
}
|
||||
@@ -165,7 +168,9 @@ func setupLargeBenchDB(b *testing.B) (*SQLiteStorage, func()) {
|
||||
}
|
||||
|
||||
// Open the temporary copy
|
||||
store, err := New(tmpPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, tmpPath)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
@@ -198,7 +203,9 @@ func setupXLargeBenchDB(b *testing.B) (*SQLiteStorage, func()) {
|
||||
}
|
||||
|
||||
// Open the temporary copy
|
||||
store, err := New(tmpPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, tmpPath)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
@@ -234,7 +241,9 @@ func setupLargeFromJSONL(b *testing.B) (*SQLiteStorage, func()) {
|
||||
}
|
||||
|
||||
// Open the temporary copy
|
||||
store, err := New(tmpPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, tmpPath)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
|
||||
@@ -120,7 +120,9 @@ func generateID(b testing.TB, prefix string, n int) string{
|
||||
func setupBenchDB(tb testing.TB) (*SQLiteStorage, func()) {
|
||||
tb.Helper()
|
||||
tmpDB := tb.TempDir() + "/test.db"
|
||||
store, err := New(tmpDB)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, tmpDB)
|
||||
if err != nil {
|
||||
tb.Fatalf("Failed to create storage: %v", err)
|
||||
}
|
||||
|
||||
@@ -9,13 +9,15 @@ import (
|
||||
)
|
||||
|
||||
func TestHashIDGeneration(t *testing.T) {
|
||||
store, err := New(":memory:")
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create storage: %v", err)
|
||||
}
|
||||
defer func() { _ = store.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.Background()
|
||||
|
||||
// Set up database with prefix
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
|
||||
@@ -73,13 +75,15 @@ func TestHashIDDeterministic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHashIDCollisionHandling(t *testing.T) {
|
||||
store, err := New(":memory:")
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create storage: %v", err)
|
||||
}
|
||||
defer func() { _ = store.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.Background()
|
||||
|
||||
// Set up database with prefix
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
|
||||
@@ -132,13 +136,15 @@ func TestHashIDCollisionHandling(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHashIDBatchCreation(t *testing.T) {
|
||||
store, err := New(":memory:")
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create storage: %v", err)
|
||||
}
|
||||
defer func() { _ = store.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.Background()
|
||||
|
||||
// Set up database with prefix
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
|
||||
|
||||
@@ -17,13 +17,15 @@ func TestPrefixValidation(t *testing.T) {
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
store, err := New(dbPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create storage: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.Background()
|
||||
|
||||
// Set prefix to "test"
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "test"); err != nil {
|
||||
@@ -93,13 +95,15 @@ func TestPrefixValidationBatch(t *testing.T) {
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
store, err := New(dbPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create storage: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.Background()
|
||||
|
||||
// Set prefix to "batch"
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "batch"); err != nil {
|
||||
|
||||
@@ -76,7 +76,7 @@ func init() {
|
||||
}
|
||||
|
||||
// New creates a new SQLite storage backend
|
||||
func New(path string) (*SQLiteStorage, error) {
|
||||
func New(ctx context.Context, path string) (*SQLiteStorage, error) {
|
||||
// Build connection string with proper URI syntax
|
||||
// For :memory: databases, use shared cache so multiple connections see the same data
|
||||
var connStr string
|
||||
@@ -180,7 +180,6 @@ func New(path string) (*SQLiteStorage, error) {
|
||||
// Hydrate from multi-repo config if configured (bd-307)
|
||||
// Skip for in-memory databases (used in tests)
|
||||
if path != ":memory:" {
|
||||
ctx := context.Background()
|
||||
_, err := storage.HydrateFromMultiRepo(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to hydrate from multi-repo: %w", err)
|
||||
|
||||
@@ -22,14 +22,16 @@ func setupTestDB(t *testing.T) (*SQLiteStorage, func()) {
|
||||
}
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
store, err := New(dbPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, dbPath)
|
||||
if err != nil {
|
||||
os.RemoveAll(tmpDir)
|
||||
t.Fatalf("failed to create storage: %v", err)
|
||||
}
|
||||
|
||||
// CRITICAL (bd-166): Set issue_prefix to prevent "database not initialized" errors
|
||||
ctx := context.Background()
|
||||
ctx = context.Background()
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
|
||||
store.Close()
|
||||
os.RemoveAll(tmpDir)
|
||||
@@ -1234,7 +1236,9 @@ func TestPath(t *testing.T) {
|
||||
|
||||
// Test with relative path
|
||||
relPath := filepath.Join(tmpDir, "test.db")
|
||||
store, err := New(relPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, relPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create storage: %v", err)
|
||||
}
|
||||
@@ -1266,13 +1270,14 @@ func TestMultipleStorageDistinctPaths(t *testing.T) {
|
||||
}
|
||||
defer os.RemoveAll(tmpDir2)
|
||||
|
||||
store1, err := New(filepath.Join(tmpDir1, "db1.db"))
|
||||
ctx := context.Background()
|
||||
store1, err := New(ctx, filepath.Join(tmpDir1, "db1.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create storage 1: %v", err)
|
||||
}
|
||||
defer store1.Close()
|
||||
|
||||
store2, err := New(filepath.Join(tmpDir2, "db2.db"))
|
||||
store2, err := New(ctx, filepath.Join(tmpDir2, "db2.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create storage 2: %v", err)
|
||||
}
|
||||
@@ -1296,7 +1301,9 @@ func TestInMemoryDatabase(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Test that :memory: database works
|
||||
store, err := New(":memory:")
|
||||
ctx = context.Background()
|
||||
|
||||
store, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create in-memory storage: %v", err)
|
||||
}
|
||||
@@ -1345,7 +1352,9 @@ func TestInMemorySharedCache(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create first connection
|
||||
store1, err := New(":memory:")
|
||||
ctx = context.Background()
|
||||
|
||||
store1, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create first in-memory storage: %v", err)
|
||||
}
|
||||
@@ -1372,7 +1381,9 @@ func TestInMemorySharedCache(t *testing.T) {
|
||||
|
||||
// Create second connection - Note: this creates a SEPARATE database
|
||||
// Shared cache only works within a single sql.DB connection pool
|
||||
store2, err := New(":memory:")
|
||||
ctx = context.Background()
|
||||
|
||||
store2, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create second in-memory storage: %v", err)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,8 @@ func newTestStore(t *testing.T, dbPath string) *SQLiteStorage {
|
||||
dbPath = t.TempDir() + "/test.db"
|
||||
}
|
||||
|
||||
store, err := New(dbPath)
|
||||
ctx := context.Background()
|
||||
store, err := New(ctx, dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test database: %v", err)
|
||||
}
|
||||
@@ -38,7 +39,6 @@ func newTestStore(t *testing.T, dbPath string) *SQLiteStorage {
|
||||
})
|
||||
|
||||
// CRITICAL (bd-166): Set issue_prefix to prevent "database not initialized" errors
|
||||
ctx := context.Background()
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
|
||||
_ = store.Close()
|
||||
t.Fatalf("Failed to set issue_prefix: %v", err)
|
||||
|
||||
@@ -211,7 +211,9 @@ func TestUnderlyingDB_AfterClose(t *testing.T) {
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
store, err := New(dbPath)
|
||||
ctx := context.Background()
|
||||
|
||||
store, err := New(ctx, dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create storage: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user