Skip to content

Commit 5bcf5bb

Browse files
authored
fix: honor shutdown timeout when provided context already canceled (#3695)
1 parent 2c5e98d commit 5bcf5bb

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (a *App) Run() error {
102102
server := srv
103103
eg.Go(func() error {
104104
<-ctx.Done() // wait for stop signal
105-
stopCtx := octx
105+
stopCtx := context.WithoutCancel(octx)
106106
if a.opts.stopTimeout > 0 {
107107
var cancel context.CancelFunc
108108
stopCtx, cancel = context.WithTimeout(stopCtx, a.opts.stopTimeout)

app_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,18 @@ func TestApp_Context(t *testing.T) {
283283
})
284284
}
285285
}
286+
287+
func TestApp_ContextCanceled(t *testing.T) {
288+
ctx, stop := context.WithCancel(context.Background())
289+
stopFn := func(ctx context.Context) error {
290+
select {
291+
case <-ctx.Done():
292+
t.Fatal("context should not be done yet")
293+
default:
294+
}
295+
return nil
296+
}
297+
app := New(Context(ctx), Server(&mockServer{stopFn: stopFn}), StopTimeout(time.Hour))
298+
time.AfterFunc(time.Millisecond*10, stop)
299+
_ = app.Run()
300+
}

options_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,17 @@ func TestLogger(t *testing.T) {
8686
}
8787
}
8888

89-
type mockServer struct{}
89+
type mockServer struct {
90+
stopFn func(context.Context) error
91+
}
9092

9193
func (m *mockServer) Start(_ context.Context) error { return nil }
92-
func (m *mockServer) Stop(_ context.Context) error { return nil }
94+
func (m *mockServer) Stop(ctx context.Context) error {
95+
if m.stopFn != nil {
96+
return m.stopFn(ctx)
97+
}
98+
return nil
99+
}
93100

94101
func TestServer(t *testing.T) {
95102
o := &options{}

0 commit comments

Comments
 (0)