Fix context propagation lifecycle bugs
Critical fixes to context propagation implementation (bd-rtp, bd-yb8, bd-2o2): 1. Fix rootCtx lifecycle in main.go: - Removed premature defer rootCancel() from PersistentPreRun (line 132) - Added proper cleanup in PersistentPostRun (lines 544-547) - Context now properly spans from setup through command execution to cleanup 2. Fix test context contamination in cli_fast_test.go: - Reset rootCtx and rootCancel to nil in test cleanup (lines 139-140) - Prevents cancelled contexts from affecting subsequent tests 3. Fix export tests missing context in export_test.go: - Added rootCtx initialization in 5 export test subtests - Tests now properly set up context before calling exportCmd.Run() These fixes ensure: - Signal-aware contexts work correctly for graceful cancellation - Ctrl+C properly cancels import/export operations - Database integrity is maintained after cancellation - All cancellation tests pass (TestImportCancellation, TestExportCommand) Tested: - go build ./cmd/bd ✓ - go test ./cmd/bd -run TestImportCancellation ✓ - go test ./cmd/bd -run TestExportCommand ✓ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"runtime/pprof"
|
||||
"runtime/trace"
|
||||
"slices"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -60,6 +63,10 @@ var (
|
||||
daemonClient *rpc.Client // RPC client when daemon is running
|
||||
noDaemon bool // Force direct mode (no daemon)
|
||||
|
||||
// Signal-aware context for graceful cancellation
|
||||
rootCtx context.Context
|
||||
rootCancel context.CancelFunc
|
||||
|
||||
// Auto-flush state
|
||||
autoFlushEnabled = true // Can be disabled with --no-auto-flush
|
||||
isDirty = false // Tracks if DB has changes needing export (used by legacy code)
|
||||
@@ -123,6 +130,9 @@ var rootCmd = &cobra.Command{
|
||||
_ = cmd.Help()
|
||||
},
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
// Set up signal-aware context for graceful cancellation
|
||||
rootCtx, rootCancel = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
// Apply viper configuration if flags weren't explicitly set
|
||||
// Priority: flags > viper (config file + env vars) > defaults
|
||||
// Do this BEFORE early-return so init/version/help respect config
|
||||
@@ -437,7 +447,7 @@ var rootCmd = &cobra.Command{
|
||||
|
||||
// Fall back to direct storage access
|
||||
var err error
|
||||
store, err = sqlite.New(dbPath)
|
||||
store, err = sqlite.New(rootCtx, dbPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: failed to open database: %v\n", err)
|
||||
os.Exit(1)
|
||||
@@ -528,6 +538,11 @@ var rootCmd = &cobra.Command{
|
||||
}
|
||||
if profileFile != nil { pprof.StopCPUProfile(); _ = profileFile.Close() }
|
||||
if traceFile != nil { trace.Stop(); _ = traceFile.Close() }
|
||||
|
||||
// Cancel the signal context to clean up resources
|
||||
if rootCancel != nil {
|
||||
rootCancel()
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user