Skip to content

Commit 6708e6b

Browse files
janiszclaude
andcommitted
fix: create cancellable context before client connection
Move cancellable context creation to the beginning of Run() so that the entire server lifecycle, including the client connection, can be properly cancelled. This allows graceful shutdown to interrupt the connection phase if needed. The signal handler is also set up earlier to ensure interrupts are caught as soon as possible. Addresses PR #50 review comment #5 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent afc96aa commit 6708e6b

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

internal/app/app.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ func Run(ctx context.Context, cfg *config.Config, stdin io.ReadCloser, stdout io
3333
// Log full configuration with sensitive data redacted.
3434
slog.Info("Configuration loaded successfully", "config", cfg.Redacted())
3535

36+
// Create a cancellable context for the entire server lifecycle
37+
ctx, cancel := context.WithCancel(ctx)
38+
defer cancel()
39+
40+
// Set up signal handling for graceful shutdown.
41+
sigChan := make(chan os.Signal, 1)
42+
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
43+
44+
go func() {
45+
<-sigChan
46+
slog.Info("Received shutdown signal")
47+
cancel()
48+
}()
49+
3650
stackroxClient, err := client.NewClient(&cfg.Central)
3751
if err != nil {
3852
return errors.Wrap(err, "failed to create client")
@@ -46,20 +60,6 @@ func Run(ctx context.Context, cfg *config.Config, stdin io.ReadCloser, stdout io
4660
return errors.Wrap(err, "failed to connect to central")
4761
}
4862

49-
// Set up signal handling for graceful shutdown.
50-
sigChan := make(chan os.Signal, 1)
51-
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
52-
53-
// Create a cancellable context from the input context
54-
ctx, cancel := context.WithCancel(ctx)
55-
defer cancel()
56-
57-
go func() {
58-
<-sigChan
59-
slog.Info("Received shutdown signal")
60-
cancel()
61-
}()
62-
6363
slog.Info("Starting StackRox MCP server")
6464

6565
return errors.Wrap(srv.Start(ctx, stdin, stdout), "failed to start server")

0 commit comments

Comments
 (0)