Skip to content

Commit 47f0689

Browse files
authored
Fix data race in x11 forwarding test. (#51005)
1 parent 0062437 commit 47f0689

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

lib/srv/regular/sshserver_test.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -1198,24 +1198,30 @@ func x11EchoSession(ctx context.Context, t *testing.T, clt *tracessh.Client) x11
11981198
os.Remove(tmpFile.Name())
11991199
})
12001200

1201-
// type 'printenv DISPLAY > /path/to/tmp/file' into the session (dumping the value of DISPLAY into the temp file)
1202-
_, err = keyboard.Write([]byte(fmt.Sprintf("printenv %v >> %s\n\r", x11.DisplayEnv, tmpFile.Name())))
1203-
require.NoError(t, err)
1204-
1205-
// wait for the output
1206-
var display string
1207-
require.Eventually(t, func() bool {
1208-
output, err := os.ReadFile(tmpFile.Name())
1209-
if err == nil && len(output) != 0 {
1210-
display = strings.TrimSpace(string(output))
1211-
return true
1212-
}
1213-
return false
1214-
}, 5*time.Second, 10*time.Millisecond, "failed to read display")
1201+
// Reading the display may fail if the session is not fully initialized
1202+
// and the write to stdin is swallowed.
1203+
display := make(chan string, 1)
1204+
require.EventuallyWithT(t, func(t *assert.CollectT) {
1205+
// enter 'printenv DISPLAY > /path/to/tmp/file' into the session (dumping the value of DISPLAY into the temp file)
1206+
_, err = keyboard.Write([]byte(fmt.Sprintf("printenv %v > %s\n\r", x11.DisplayEnv, tmpFile.Name())))
1207+
assert.NoError(t, err)
1208+
1209+
assert.Eventually(t, func() bool {
1210+
output, err := os.ReadFile(tmpFile.Name())
1211+
if err == nil && len(output) != 0 {
1212+
select {
1213+
case display <- strings.TrimSpace(string(output)):
1214+
default:
1215+
}
1216+
return true
1217+
}
1218+
return false
1219+
}, time.Second, 100*time.Millisecond, "failed to read display")
1220+
}, 10*time.Second, 1*time.Second)
12151221

12161222
// Make a new connection to the XServer proxy, the client
12171223
// XServer should echo back anything written on it.
1218-
serverDisplay, err := x11.ParseDisplay(display)
1224+
serverDisplay, err := x11.ParseDisplay(<-display)
12191225
require.NoError(t, err)
12201226

12211227
return serverDisplay

0 commit comments

Comments
 (0)