Skip to content

Commit

Permalink
fix: wait for ssh.Serve to terminate
Browse files Browse the repository at this point in the history
We need to wait for Serve to finish and clean up, otherwise we
terminate active connections by exiting through main.
  • Loading branch information
muesli authored and aymanbagabas committed Nov 8, 2022
1 parent 494ecb1 commit f45c018
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions cmd/soft/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ var (
cfg := config.DefaultConfig()
s := server.NewServer(cfg)

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

log.Printf("Starting SSH server on %s:%d", cfg.BindAddr, cfg.Port)

lch := make(chan error)
go func() {
if err := s.Start(); err != nil {
log.Fatalln(err)
}
defer close(lch)
lch <- s.Start()
}()

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-done

log.Printf("Stopping SSH server on %s:%d", cfg.BindAddr, cfg.Port)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
return s.Shutdown(ctx)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
return err
}

// wait for serve to finish
return <-lch
},
}
)

0 comments on commit f45c018

Please sign in to comment.