Skip to content

Commit a7eed3b

Browse files
committed
fix: register shutdown hooks for services after app's mutext is unlocked
1 parent 1974468 commit a7eed3b

3 files changed

Lines changed: 27 additions & 33 deletions

File tree

app.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,15 @@ func (app *App) init() *App {
11421142

11431143
// unlock application
11441144
app.mutex.Unlock()
1145+
1146+
// Register the Services shutdown handler once the app is initialized and unlocked.
1147+
app.Hooks().OnPostShutdown(func(_ error) error {
1148+
if err := app.shutdownServices(app.servicesShutdownCtx()); err != nil {
1149+
log.Errorf("failed to shutdown services: %v", err)
1150+
}
1151+
return nil
1152+
})
1153+
11451154
return app
11461155
}
11471156

services.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"fmt"
77
"io"
88
"strings"
9-
10-
"github.com/gofiber/fiber/v3/log"
119
)
1210

1311
// Service is an interface that defines the methods for a service.
@@ -42,17 +40,6 @@ func (app *App) initServices() {
4240
if err := app.startServices(app.servicesStartupCtx()); err != nil {
4341
panic(err)
4442
}
45-
46-
// Create the shutdown handler before registering the hook
47-
shutdownHandler := func(_ error) error {
48-
if err := app.shutdownServices(app.servicesShutdownCtx()); err != nil {
49-
log.Errorf("failed to shutdown services: %v", err)
50-
}
51-
return nil
52-
}
53-
54-
// Register the pre-created handler
55-
app.Hooks().OnPostShutdown(shutdownHandler)
5643
}
5744

5845
// servicesStartupCtx Returns the context for the services startup.

services_test.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ func Test_InitServices(t *testing.T) {
132132
require.NotPanics(t, app.initServices)
133133
})
134134

135-
t.Run("successful-start", func(t *testing.T) {
135+
t.Run("start/success", func(t *testing.T) {
136+
// Initialize the app using the struct and defining the state and hooks manually,
137+
// because we are not checking the shutdown hooks in this test.
136138
app := &App{
137139
configured: Config{
138140
Services: []Service{
@@ -148,7 +150,9 @@ func Test_InitServices(t *testing.T) {
148150
require.NotPanics(t, app.initServices)
149151
})
150152

151-
t.Run("failed-start", func(t *testing.T) {
153+
t.Run("start/error", func(t *testing.T) {
154+
// Initialize the app using the struct and defining the state and hooks manually,
155+
// because we are not checking the shutdown hooks in this test.
152156
app := &App{
153157
configured: Config{
154158
Services: []Service{
@@ -166,14 +170,11 @@ func Test_InitServices(t *testing.T) {
166170
})
167171

168172
t.Run("shutdown-hooks/success", func(t *testing.T) {
169-
app := &App{
170-
configured: Config{
171-
Services: []Service{&mockService{name: "dep1"}},
172-
},
173-
state: newState(),
174-
}
175-
176-
app.hooks = newHooks(app)
173+
// Initialize the app using the New function to verify that the shutdown hooks are registered
174+
// and the app mutex is not causing a deadlock.
175+
app := New(Config{
176+
Services: []Service{&mockService{name: "dep1"}},
177+
})
177178

178179
require.NotPanics(t, app.initServices)
179180

@@ -190,17 +191,14 @@ func Test_InitServices(t *testing.T) {
190191
})
191192

192193
t.Run("shutdown-hooks/error", func(t *testing.T) {
193-
app := &App{
194-
configured: Config{
195-
Services: []Service{
196-
&mockService{name: "dep1"},
197-
&mockService{name: "dep2", terminateError: errors.New(terminateErrorMessage + " 2")},
198-
},
194+
// Initialize the app using the New function to verify that the shutdown hooks are registered
195+
// and the app mutex is not causing a deadlock.
196+
app := New(Config{
197+
Services: []Service{
198+
&mockService{name: "dep1"},
199+
&mockService{name: "dep2", terminateError: errors.New(terminateErrorMessage + " 2")},
199200
},
200-
state: newState(),
201-
}
202-
203-
app.hooks = newHooks(app)
201+
})
204202

205203
require.NotPanics(t, app.initServices)
206204

0 commit comments

Comments
 (0)