Skip to content

Commit c191a43

Browse files
committed
feat: proper shutdown sequence on interrupt
1 parent 0c57c7a commit c191a43

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

daemon/app.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ func (app *App) SetDeviceName(name string) {
146146
// error occurs. The credential type configured in cfg.Credentials.Type
147147
// determines which login flow is used.
148148
func (app *App) Run(ctx context.Context) error {
149+
go func() {
150+
select {
151+
case <-ctx.Done():
152+
_ = app.Close()
153+
}
154+
}()
155+
149156
switch app.cfg.Credentials.Type {
150157
case "zeroconf":
151158
// Zeroconf mode unconditionally needs zeroconf to be enabled.
@@ -167,18 +174,22 @@ func (app *App) Close() error {
167174
}
168175
app.closed = true
169176

170-
var firstErr error
177+
var errs []error
171178
if app.server != nil {
172-
if err := app.server.Close(); err != nil && firstErr == nil {
173-
firstErr = err
179+
if err := app.server.Close(); err != nil {
180+
errs = append(errs, err)
174181
}
175182
}
176183
if app.mpris != nil {
177-
if err := app.mpris.Close(); err != nil && firstErr == nil {
178-
firstErr = err
184+
if err := app.mpris.Close(); err != nil {
185+
errs = append(errs, err)
179186
}
180187
}
181-
return firstErr
188+
if app.zeroconf != nil {
189+
app.zeroconf.Close()
190+
}
191+
192+
return errors.Join(errs...)
182193
}
183194

184195
func (app *App) persistState() error {
@@ -362,6 +373,14 @@ func (app *App) withAppPlayer(ctx context.Context, appPlayerFunc func(context.Co
362373
go func() {
363374
for {
364375
select {
376+
case <-ctx.Done():
377+
if currentPlayer != nil {
378+
currentPlayer.Close()
379+
currentPlayer = nil
380+
381+
close(apiCh)
382+
}
383+
return
365384
case p := <-app.logoutCh:
366385
if p != currentPlayer {
367386
continue

0 commit comments

Comments
 (0)