Skip to content

Commit c29aa10

Browse files
committed
Fix race
1 parent 4b3b2bd commit c29aa10

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

cli/ssh.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,18 @@ func (r *RootCmd) ssh() *clibase.Cmd {
7777
if err != nil {
7878
return xerrors.Errorf("error opening %s for logging: %w", logFilePath, err)
7979
}
80-
logger = slog.Make(sloghuman.Sink(logFile))
81-
defer logFile.Close()
80+
// HACK: Something is keeping a reference to this logger
81+
// after the goroutine ends, leading to the race observed
82+
// here: https://github.com/coder/coder/actions/runs/5178818818/jobs/9331016395.
83+
rd, wr := io.Pipe()
84+
go func() {
85+
_, _ = io.Copy(logFile, rd)
86+
}()
87+
defer func() {
88+
_ = wr.Close()
89+
_ = logFile.Close()
90+
}()
91+
logger = slog.Make(sloghuman.Sink(wr))
8292
if r.verbose {
8393
logger = logger.Leveled(slog.LevelDebug)
8494
}

cli/ssh_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,12 @@ func TestSSH(t *testing.T) {
441441
pty.WriteLine("exit")
442442
<-cmdDone
443443

444-
_, err := os.Stat(logFile)
444+
info, err := os.Stat(logFile)
445445
if err != nil {
446446
t.Fatalf("failed to find ssh logfile: %v", err)
447447
}
448+
449+
require.Greater(t, info.Size(), int64(0), "ssh logfile is empty")
448450
})
449451
}
450452

docs/cli/ssh.md

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,14 @@ Specifies whether to forward the GPG agent. Unsupported on Windows workspaces, b
3939

4040
Specifies which identity agent to use (overrides $SSH_AUTH_SOCK), forward agent must also be enabled.
4141

42-
### --log-dir
42+
### -l, --log-file
4343

44-
| | |
45-
| ----------- | ------------------------------- |
46-
| Type | <code>string</code> |
47-
| Environment | <code>$CODER_SSH_LOG_DIR</code> |
48-
| Default | <code>/tmp</code> |
49-
50-
Specify the location for the log files.
51-
52-
### -l, --log-to-file
53-
54-
| | |
55-
| ----------- | ----------------------------------- |
56-
| Type | <code>bool</code> |
57-
| Environment | <code>$CODER_SSH_LOG_TO_FILE</code> |
44+
| | |
45+
| ----------- | -------------------------------- |
46+
| Type | <code>string</code> |
47+
| Environment | <code>$CODER_SSH_LOG_FILE</code> |
5848

59-
Enable diagnostic logging to file.
49+
Specify the location of an SSH diagnostic log file.
6050

6151
### --no-wait
6252

0 commit comments

Comments
 (0)