Skip to content

Commit 0d8a5af

Browse files
committed
Fix orphaned process on shutdown
1 parent cfb2f90 commit 0d8a5af

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

cmd/cog/main.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,31 @@ func serverCommand() *ff.Command {
7878
"upload-url", cfg.UploadUrl,
7979
)
8080

81-
ctx, cancel := context.WithCancel(ctx)
82-
go func() {
83-
ch := make(chan os.Signal, 1)
84-
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
85-
s := <-ch
86-
if cfg.AwaitExplicitShutdown {
87-
log.Warnw("ignoring signal to stop", "signal", s)
88-
} else {
89-
log.Infow("stopping Cog HTTP server", "signal", s)
90-
cancel()
91-
}
92-
}()
93-
9481
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
9582
log.Infow("starting Cog HTTP server", "addr", addr, "version", util.Version(), "pid", os.Getpid())
9683
serverCfg := server.Config{
9784
UseProcedureMode: cfg.UseProcedureMode,
9885
AwaitExplicitShutdown: cfg.AwaitExplicitShutdown,
9986
UploadUrl: cfg.UploadUrl,
10087
}
88+
ctx, cancel := context.WithCancel(ctx)
10189
h := server.NewHandler(serverCfg, cancel)
10290
s := server.NewServer(addr, h, cfg.UseProcedureMode)
10391
go func() {
10492
<-ctx.Done()
10593
must.Do(s.Shutdown(ctx))
10694
}()
95+
go func() {
96+
ch := make(chan os.Signal, 1)
97+
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
98+
sig := <-ch
99+
if cfg.AwaitExplicitShutdown {
100+
log.Warnw("ignoring signal to stop", "signal", sig)
101+
} else {
102+
log.Infow("stopping Cog HTTP server", "signal", sig)
103+
must.Do(h.Stop())
104+
}
105+
}()
107106
if err := s.ListenAndServe(); errors.Is(err, http.ErrServerClosed) {
108107
exitCode := h.ExitCode()
109108
if exitCode == 0 {

internal/server/server.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,32 @@ func (h *Handler) OpenApi(w http.ResponseWriter, r *http.Request) {
107107
}
108108

109109
func (h *Handler) Shutdown(w http.ResponseWriter, r *http.Request) {
110+
err := h.Stop()
111+
if err != nil {
112+
http.Error(w, err.Error(), http.StatusInternalServerError)
113+
} else {
114+
w.WriteHeader(http.StatusOK)
115+
}
116+
}
117+
118+
func (h *Handler) Stop() error {
110119
// Procedure mode and no runner yet
111120
if h.runner == nil {
112-
w.WriteHeader(http.StatusOK)
113121
// Shut down immediately
114122
h.shutdown()
115-
return
123+
return nil
116124
}
117125

118126
// Request runner stop
119127
if err := h.runner.Stop(); err != nil {
120-
http.Error(w, err.Error(), http.StatusInternalServerError)
121-
} else {
122-
w.WriteHeader(http.StatusOK)
128+
return err
123129
}
124130
go func() {
125131
// Shut down once runner exists
126132
h.runner.WaitForStop()
127133
h.shutdown()
128134
}()
135+
return nil
129136
}
130137

131138
func (h *Handler) updateRunner(srcDir string) error {

internal/tests/shutdown_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ func TestShutdownByServerSigTerm(t *testing.T) {
2323
assert.Equal(t, 0, ct.cmd.ProcessState.ExitCode())
2424
}
2525

26-
func TestShutdownByRunnerSigTerm(t *testing.T) {
27-
ct := NewCogTest(t, "sleep")
28-
assert.NoError(t, ct.Start())
29-
30-
hc := ct.WaitForSetup()
31-
assert.Equal(t, server.StatusReady.String(), hc.Status)
32-
assert.Equal(t, server.SetupSucceeded, hc.Setup.Status)
33-
34-
must.Do(syscall.Kill(ct.ServerPid(), syscall.SIGTERM))
35-
assert.NoError(t, ct.Cleanup())
36-
assert.Equal(t, 0, ct.cmd.ProcessState.ExitCode())
37-
}
38-
3926
func TestShutdownIgnoreSignal(t *testing.T) {
4027
ct := NewCogTest(t, "sleep")
4128
ct.AppendArgs("--await-explicit-shutdown=true")

0 commit comments

Comments
 (0)