Skip to content

Commit 2b60853

Browse files
authored
fix: handle context cancellation gracefully during startup/shutdown (#11)
* fix: handle context cancellation gracefully during startup/shutdown When the gravity process receives SIGTERM during startup or is shut down gracefully, context cancellation errors should not be logged as fatal errors. Changes: - Propagate context.Canceled cleanly from stack.CreateNetworkProvider() - Handle context cancellation in cmd/start.go by returning gracefully instead of calling logger.Fatal() - Change context done during startup from Fatal to Debug log level Fixes: agentuity/sdk#775 * bump go-common * fix: use errors.Is() for context cancellation detection Replace direct equality checks against context.Canceled with errors.Is() to correctly detect wrapped cancellation errors: - cmd/start.go: Use errors.Is(err, context.Canceled) for network provider error - internal/stack/stack.go: Use errors.Is() for proxy error handler and client start This ensures wrapped cancellation errors are properly detected and treated as graceful shutdowns rather than unexpected errors.
1 parent 6f56940 commit 2b60853

4 files changed

Lines changed: 18 additions & 5 deletions

File tree

cmd/start.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/x509"
77
"encoding/base64"
88
"encoding/pem"
9+
"errors"
910
"fmt"
1011
"net/http"
1112
"os"
@@ -156,13 +157,20 @@ var rootCmd = &cobra.Command{
156157
return nil
157158
})
158159
if err != nil {
160+
// Don't log fatal error for context cancellation (graceful shutdown)
161+
if errors.Is(err, context.Canceled) {
162+
logger.Debug("gravity client shutdown due to context cancellation")
163+
return
164+
}
159165
logger.Fatal("failed to create network provider: %v", err)
160166
}
161167

162168
// Wait for provider connection
163169
select {
164170
case <-ctx.Done():
165-
logger.Fatal("context done: %v", ctx.Err())
171+
// Context cancelled during startup - graceful shutdown, not an error
172+
logger.Debug("context cancelled during startup: %v", ctx.Err())
173+
return
166174
case <-time.After(time.Second * 10):
167175
logger.Error("timed out waiting for provider connection")
168176
os.Exit(1)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/agentuity/gravity
33
go 1.25.5
44

55
require (
6-
github.com/agentuity/go-common v1.0.142
6+
github.com/agentuity/go-common v1.0.144
77
github.com/spf13/cobra v1.10.1
88
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/agentuity/go-common v1.0.142 h1:hcrf9y0pYSne47B1ReM+VZdhuAJxDmJn1mM1VXiBemw=
2-
github.com/agentuity/go-common v1.0.142/go.mod h1:rvM6VhfltrwrfbjKjNToBQ7kNx3lR6955+n/G1C8soM=
1+
github.com/agentuity/go-common v1.0.144 h1:tbIqPNIa0xJA+4XVV/Z9Dg6JF5+Saqgu+i/qZAZbBhc=
2+
github.com/agentuity/go-common v1.0.144/go.mod h1:rvM6VhfltrwrfbjKjNToBQ7kNx3lR6955+n/G1C8soM=
33
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
44
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
55
github.com/cockroachdb/errors v1.12.0 h1:d7oCs6vuIMUQRVbi6jWWWEJZahLCfJpnJSVobd1/sUo=

internal/stack/stack.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/tls"
77
"crypto/x509"
88
"encoding/pem"
9+
"errors"
910
"fmt"
1011
"io"
1112
"net"
@@ -175,7 +176,7 @@ func StartServer(ctx context.Context, logger _logger.Logger, tlsConfig *tls.Conf
175176
proxy.FlushInterval = -1
176177
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
177178
// Suppress expected context cancellation errors (client disconnect, WebSocket close)
178-
if ctx.Err() == context.Canceled || r.Context().Err() == context.Canceled {
179+
if errors.Is(ctx.Err(), context.Canceled) || errors.Is(r.Context().Err(), context.Canceled) {
179180
return
180181
}
181182
logger.Error("proxy error: %v", err)
@@ -379,6 +380,10 @@ func CreateNetworkProvider(
379380
network.client = client
380381

381382
if err := client.Start(); err != nil {
383+
// Don't wrap context.Canceled - let it propagate cleanly for graceful shutdown
384+
if errors.Is(err, context.Canceled) {
385+
return nil, nil, err
386+
}
382387
return nil, nil, fmt.Errorf("failed to start gravity client: %w", err)
383388
}
384389

0 commit comments

Comments
 (0)