Skip to content

Commit 4034d7c

Browse files
authored
Fix data race in x11 forwarding test. (#50997)
1 parent ce30037 commit 4034d7c

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/srv/regular/sshserver_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,24 +1349,30 @@ func x11EchoSession(ctx context.Context, t *testing.T, clt *tracessh.Client) x11
13491349
os.Remove(tmpFile.Name())
13501350
})
13511351

1352-
// type 'printenv DISPLAY > /path/to/tmp/file' into the session (dumping the value of DISPLAY into the temp file)
1353-
_, err = keyboard.Write([]byte(fmt.Sprintf("printenv %v >> %s\n\r", x11.DisplayEnv, tmpFile.Name())))
1354-
require.NoError(t, err)
1352+
// Reading the display may fail if the session is not fully initialized
1353+
// and the write to stdin is swallowed.
1354+
display := make(chan string, 1)
1355+
require.EventuallyWithT(t, func(t *assert.CollectT) {
1356+
// enter 'printenv DISPLAY > /path/to/tmp/file' into the session (dumping the value of DISPLAY into the temp file)
1357+
_, err = keyboard.Write([]byte(fmt.Sprintf("printenv %v > %s\n\r", x11.DisplayEnv, tmpFile.Name())))
1358+
assert.NoError(t, err)
13551359

1356-
// wait for the output
1357-
var display string
1358-
require.Eventually(t, func() bool {
1359-
output, err := os.ReadFile(tmpFile.Name())
1360-
if err == nil && len(output) != 0 {
1361-
display = strings.TrimSpace(string(output))
1362-
return true
1363-
}
1364-
return false
1365-
}, 10*time.Second, 100*time.Millisecond, "failed to read display")
1360+
assert.Eventually(t, func() bool {
1361+
output, err := os.ReadFile(tmpFile.Name())
1362+
if err == nil && len(output) != 0 {
1363+
select {
1364+
case display <- strings.TrimSpace(string(output)):
1365+
default:
1366+
}
1367+
return true
1368+
}
1369+
return false
1370+
}, time.Second, 100*time.Millisecond, "failed to read display")
1371+
}, 10*time.Second, 1*time.Second)
13661372

13671373
// Make a new connection to the XServer proxy, the client
13681374
// XServer should echo back anything written on it.
1369-
serverDisplay, err := x11.ParseDisplay(display)
1375+
serverDisplay, err := x11.ParseDisplay(<-display)
13701376
require.NoError(t, err)
13711377

13721378
return serverDisplay

0 commit comments

Comments
 (0)