Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec54e01

Browse files
committed
fix: Disable USR1 on Windows (no signal)
1 parent c91637f commit ec54e01

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

cli/agent.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
_ "net/http/pprof" //nolint: gosec
77
"net/url"
88
"os"
9-
"os/signal"
109
"path/filepath"
11-
"syscall"
1210
"time"
1311

1412
"cloud.google.com/go/compute/metadata"
@@ -54,29 +52,14 @@ func workspaceAgent() *cobra.Command {
5452
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
5553
client := codersdk.New(coderURL)
5654

57-
usr1 := make(chan os.Signal, 1)
5855
if pprofEnabled {
59-
close(usr1)
6056
srvClose := serveHandler(cmd.Context(), logger, nil, pprofAddress, "pprof")
6157
defer srvClose()
6258
} else {
6359
// If pprof wasn't enabled at startup, allow a
6460
// `kill -USR1 $agent_pid` to start it.
65-
signal.Notify(usr1, syscall.SIGUSR1)
66-
go func() {
67-
defer close(usr1)
68-
defer signal.Stop(usr1)
69-
70-
select {
71-
case <-usr1:
72-
signal.Stop(usr1)
73-
srvClose := serveHandler(cmd.Context(), logger, nil, pprofAddress, "pprof")
74-
defer srvClose()
75-
case <-cmd.Context().Done():
76-
return
77-
}
78-
<-cmd.Context().Done() // Prevent defer close until done.
79-
}()
61+
srvClose := agentPPROFStartOnUSR1(cmd.Context(), logger, pprofAddress)
62+
defer srvClose()
8063
}
8164

8265
// exchangeToken returns a session token.
@@ -164,7 +147,6 @@ func workspaceAgent() *cobra.Command {
164147
},
165148
})
166149
<-cmd.Context().Done()
167-
<-usr1 // Wait for server to close (if started).
168150
return closer.Close()
169151
},
170152
}

cli/agent_unix.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
9+
"cdr.dev/slog"
10+
)
11+
12+
func agentPPROFStartOnUSR1(ctx context.Context, logger slog.Logger, pprofAddress string) (srvClose func()) {
13+
usr1 := make(chan os.Signal, 1)
14+
signal.Notify(usr1, syscall.SIGUSR1)
15+
go func() {
16+
defer close(usr1)
17+
defer signal.Stop(usr1)
18+
19+
select {
20+
case <-usr1:
21+
signal.Stop(usr1)
22+
srvClose := serveHandler(ctx, logger, nil, pprofAddress, "pprof")
23+
defer srvClose()
24+
case <-ctx.Done():
25+
return
26+
}
27+
<-ctx.Done() // Prevent defer close until done.
28+
}()
29+
30+
return func() {
31+
<-usr1 // Wait until usr1 is closed, ensures srvClose was run.
32+
}
33+
}

cli/agent_windows.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
9+
"cdr.dev/slog"
10+
)
11+
12+
// agentPPROFStartOnUSR1 is no-op on Windows (no SIGUSR1 signal).
13+
func agentPPROFStartOnUSR1(ctx context.Context, logger slog.Logger, pprofAddress string) (srvClose func()) {
14+
return func() {}
15+
}

0 commit comments

Comments
 (0)