Skip to content

Commit 3816633

Browse files
intel352claude
andcommitted
fix: production engine/module shutdown — close DB connections and stop goroutines properly
- Add Stop() to FeatureFlagModule to close its SQLite store on shutdown (was leaking the DB connection since the module never implemented Stoppable) - Add 30-second timeout to multi-workflow shutdown context to prevent indefinite hangs if in-flight requests or modules fail to stop - Add 30-second timeout to RunUntilSignal shutdown for the same reason Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2f359f4 commit 3816633

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

cmd/server/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,8 +1671,10 @@ func runMultiWorkflow(logger *slog.Logger) error {
16711671
}
16721672
cancel()
16731673

1674-
// Graceful shutdown
1675-
shutdownCtx := context.Background()
1674+
// Graceful shutdown with a 30-second timeout to avoid blocking forever
1675+
// if in-flight requests or engine modules hang.
1676+
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
1677+
defer shutdownCancel()
16761678
if err := mgr.StopAll(shutdownCtx); err != nil {
16771679
logger.Error("Engine manager shutdown error", "error", err)
16781680
}

engine_builder.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/signal"
99
"syscall"
10+
"time"
1011

1112
"github.com/GoCodeAlone/modular"
1213
"github.com/GoCodeAlone/workflow/config"
@@ -266,7 +267,9 @@ func (b *EngineBuilder) RunUntilSignal(cfg *config.WorkflowConfig) error {
266267
<-sigCh
267268

268269
cancel()
269-
if err := engine.Stop(context.Background()); err != nil {
270+
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
271+
defer shutdownCancel()
272+
if err := engine.Stop(shutdownCtx); err != nil {
270273
return fmt.Errorf("shutdown error: %w", err)
271274
}
272275
return nil

module/feature_flag_module.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package module
22

33
import (
4+
"context"
45
"fmt"
56
"log/slog"
67
"os"
@@ -126,6 +127,14 @@ func (m *FeatureFlagModule) Store() *generic.Store {
126127
return m.store
127128
}
128129

130+
// Stop closes the underlying store's database connection during shutdown.
131+
func (m *FeatureFlagModule) Stop(_ context.Context) error {
132+
if m.store != nil {
133+
return m.store.Close()
134+
}
135+
return nil
136+
}
137+
129138
// SSEEnabled returns whether SSE streaming is enabled for this module.
130139
func (m *FeatureFlagModule) SSEEnabled() bool {
131140
return m.config.SSEEnabled

0 commit comments

Comments
 (0)