Skip to content

Commit

Permalink
app.Run: Respect Shutdower exit code
Browse files Browse the repository at this point in the history
We added support for changing the exit code for a Shutdowner with the
ExitCode option in uber-go#989, but this was somehow not respected by App.Run.

This changes App.Run to use the same underlying machinery (`Wait()`)
to decide on the exit code to use.

Resolves uber-go#1074
  • Loading branch information
abhinav committed Apr 29, 2023
1 parent d252024 commit 9cc86f4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 5 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,12 @@ func (app *App) Run() {
// Historically, we do not os.Exit(0) even though most applications
// cede control to Fx with they call app.Run. To avoid a breaking
// change, never os.Exit for success.
if code := app.run(app.Done()); code != 0 {
if code := app.run(app.Wait()); code != 0 {
app.exit(code)
}
}

func (app *App) run(done <-chan os.Signal) (exitCode int) {
func (app *App) run(done <-chan ShutdownSignal) (exitCode int) {
startCtx, cancel := app.clock.WithTimeout(context.Background(), app.StartTimeout())
defer cancel()

Expand All @@ -588,7 +588,8 @@ func (app *App) run(done <-chan os.Signal) (exitCode int) {
}

sig := <-done
app.log().LogEvent(&fxevent.Stopping{Signal: sig})
app.log().LogEvent(&fxevent.Stopping{Signal: sig.Signal})
exitCode = sig.ExitCode

stopCtx, cancel := app.clock.WithTimeout(context.Background(), app.StopTimeout())
defer cancel()
Expand All @@ -597,7 +598,7 @@ func (app *App) run(done <-chan os.Signal) (exitCode int) {
return 1
}

return 0
return exitCode
}

// Err returns any error encountered during New's initialization. See the
Expand Down
5 changes: 2 additions & 3 deletions app_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package fx
import (
"errors"
"fmt"
"os"
"sync"
"testing"

Expand All @@ -42,7 +41,7 @@ func TestAppRun(t *testing.T) {
app := New(
WithLogger(func() fxevent.Logger { return spy }),
)
done := make(chan os.Signal)
done := make(chan ShutdownSignal)

var wg sync.WaitGroup
wg.Add(1)
Expand All @@ -51,7 +50,7 @@ func TestAppRun(t *testing.T) {
app.run(done)
}()

done <- _sigINT
done <- ShutdownSignal{Signal: _sigINT}
wg.Wait()

assert.Equal(t, []string{
Expand Down

0 comments on commit 9cc86f4

Please sign in to comment.