Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions daemon/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ func (app *App) SetDeviceName(name string) {
// error occurs. The credential type configured in cfg.Credentials.Type
// determines which login flow is used.
func (app *App) Run(ctx context.Context) error {
go func() {
select {
case <-ctx.Done():
_ = app.Close()
}
}()

switch app.cfg.Credentials.Type {
case "zeroconf":
// Zeroconf mode unconditionally needs zeroconf to be enabled.
Expand All @@ -167,18 +174,22 @@ func (app *App) Close() error {
}
app.closed = true

var firstErr error
var errs []error
if app.server != nil {
if err := app.server.Close(); err != nil && firstErr == nil {
firstErr = err
if err := app.server.Close(); err != nil {
errs = append(errs, err)
}
}
if app.mpris != nil {
if err := app.mpris.Close(); err != nil && firstErr == nil {
firstErr = err
if err := app.mpris.Close(); err != nil {
errs = append(errs, err)
}
}
return firstErr
if app.zeroconf != nil {
app.zeroconf.Close()
}

return errors.Join(errs...)
}

func (app *App) persistState() error {
Expand Down Expand Up @@ -362,6 +373,14 @@ func (app *App) withAppPlayer(ctx context.Context, appPlayerFunc func(context.Co
go func() {
for {
select {
case <-ctx.Done():
if currentPlayer != nil {
currentPlayer.Close()
currentPlayer = nil

close(apiCh)
}
return
case p := <-app.logoutCh:
if p != currentPlayer {
continue
Expand Down
10 changes: 10 additions & 0 deletions zeroconf/zeroconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Zeroconf struct {

listener net.Listener
registrar ServiceRegistrar
closed bool

dh *dh.DiffieHellman

Expand Down Expand Up @@ -116,6 +117,11 @@ func (z *Zeroconf) SetCurrentUser(username string) {
// Close stops the zeroconf responder and HTTP listener,
// but does not close the last opened session.
func (z *Zeroconf) Close() {
if z.closed {
return
}

z.closed = true
z.registrar.Shutdown()
_ = z.listener.Close()
}
Expand Down Expand Up @@ -307,6 +313,10 @@ func (z *Zeroconf) Serve(handler HandleNewRequestFunc) error {
for {
select {
case err := <-serveErr:
if z.closed {
return nil
}

return err
case req := <-z.reqsChan:
req.result <- handler(req)
Expand Down
Loading