From f45c01885f617f96bb3f62966bae2e70a5cee3c6 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 2 Nov 2022 06:25:05 +0100 Subject: [PATCH] fix: wait for ssh.Serve to terminate We need to wait for Serve to finish and clean up, otherwise we terminate active connections by exiting through main. --- cmd/soft/serve.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/soft/serve.go b/cmd/soft/serve.go index 6ceebbad6..a8da0b6ab 100644 --- a/cmd/soft/serve.go +++ b/cmd/soft/serve.go @@ -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 }, } )