Skip to content

Commit a04c63d

Browse files
committed
feat: Update callers of to server.Serve to use buffered channel to avoid deadlocks when callers are not listening to the ready channel
1 parent cd0a385 commit a04c63d

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

cmd/cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func main() {
5454
wg.Add(1)
5555
go ph.Run(ctx, &wg)
5656
wg.Add(1)
57-
go server.Serve(ctx, &wg, ph, version, make(chan<- struct{}))
57+
go server.Serve(ctx, &wg, ph, version, make(chan<- struct{}, 1))
5858
wg.Wait()
5959

6060
if err := ph.SaveBalance(); err != nil {

cmd/winsvc/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes c
117117
wg.Add(1)
118118
go ph.Run(ctx, &wg)
119119
wg.Add(1)
120-
go server.Serve(ctx, &wg, ph, version, make(chan<- struct{}))
120+
go server.Serve(ctx, &wg, ph, version, make(chan<- struct{}, 1))
121121
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
122122

123123
default:

server/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ func Serve(ctx context.Context, wg *sync.WaitGroup, ph *engine.ProcessHunter, ve
131131
log.Println("starting service")
132132
go func() {
133133
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
134-
log.Fatalf("Web service listen error: %s", err)
134+
log.Println("Web service listen error:", err)
135135
}
136136
}()
137137

138138
ready <- struct{}{} // signal that the server is ready
139139

140140
<-ctx.Done()
141141

142+
log.Println("Web service shutting down")
142143
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
143144
defer cancel()
144145
err := s.Shutdown(ctx)

0 commit comments

Comments
 (0)