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

Skip to content

Commit d195ae6

Browse files
committed
fix(codersdk): abort in-progress writes/reads when closing websocket
Fixes #9203 Also, adds some basic tracing infrastructure that we can build upon for more improvements.
1 parent 0e8ebb9 commit d195ae6

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

cli/root.go

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"os/signal"
1919
"path/filepath"
2020
"runtime"
21+
"runtime/trace"
2122
"strings"
2223
"syscall"
2324
"text/tabwriter"
@@ -139,6 +140,22 @@ func (r *RootCmd) AGPL() []*serpent.Command {
139140

140141
// Main is the entrypoint for the Coder CLI.
141142
func (r *RootCmd) RunMain(subcommands []*serpent.Command) {
143+
// This configuration is not available as a standard option because we
144+
// want to trace the entire program, including Options parsing.
145+
goTraceFilePath, ok := os.LookupEnv("CODER_GO_TRACE")
146+
if ok {
147+
traceFile, err := os.OpenFile(goTraceFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
148+
if err != nil {
149+
panic(fmt.Sprintf("failed to open trace file: %v", err))
150+
}
151+
defer traceFile.Close()
152+
153+
if err := trace.Start(traceFile); err != nil {
154+
panic(fmt.Sprintf("failed to start trace: %v", err))
155+
}
156+
defer trace.Stop()
157+
}
158+
142159
rand.Seed(time.Now().UnixMicro())
143160

144161
cmd, err := r.Command(subcommands)

cli/ssh.go

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"os/exec"
1313
"path/filepath"
14+
"runtime/trace"
1415
"strings"
1516
"sync"
1617
"time"
@@ -67,6 +68,8 @@ func (r *RootCmd) ssh() *serpent.Command {
6768
r.InitClient(client),
6869
),
6970
Handler: func(inv *serpent.Invocation) (retErr error) {
71+
defer trace.StartRegion(inv.Context(), "ssh").End()
72+
7073
// Before dialing the SSH server over TCP, capture Interrupt signals
7174
// so that if we are interrupted, we have a chance to tear down the
7275
// TCP session cleanly before exiting. If we don't, then the TCP

codersdk/websocket.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *wsNetConn) Write(b []byte) (n int, err error) {
3232
}
3333

3434
func (c *wsNetConn) Close() error {
35-
defer c.cancel()
35+
c.cancel()
3636
return c.Conn.Close()
3737
}
3838

0 commit comments

Comments
 (0)