f45c01885f617f96bb3f62966bae2e70a5cee3c6

Author
Christian Muehlhaeuser <muesli@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

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.

Diff

 1diff --git a/cmd/soft/serve.go b/cmd/soft/serve.go
 2index 6ceebbad6f6defdfa670493e6ed83351c1822f64..a8da0b6abf5c8b64707a06151fb9078b9fea1a53 100644
 3--- a/cmd/soft/serve.go
 4+++ b/cmd/soft/serve.go
 5@@ -23,22 +23,27 @@ var (
 6 			cfg := config.DefaultConfig()
 7 			s := server.NewServer(cfg)
 8 
 9-			done := make(chan os.Signal, 1)
10-			signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
11-
12 			log.Printf("Starting SSH server on %s:%d", cfg.BindAddr, cfg.Port)
13+
14+			lch := make(chan error)
15 			go func() {
16-				if err := s.Start(); err != nil {
17-					log.Fatalln(err)
18-				}
19+				defer close(lch)
20+				lch <- s.Start()
21 			}()
22 
23+			done := make(chan os.Signal, 1)
24+			signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
25 			<-done
26 
27 			log.Printf("Stopping SSH server on %s:%d", cfg.BindAddr, cfg.Port)
28 			ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
29-			defer func() { cancel() }()
30-			return s.Shutdown(ctx)
31+			defer cancel()
32+			if err := s.Shutdown(ctx); err != nil {
33+				return err
34+			}
35+
36+			// wait for serve to finish
37+			return <-lch
38 		},
39 	}
40 )