Commit 15c9553
committed
fix(proxy): wait for in-flight agent-to-upstream replies before closing srcChan
When sshHandleChannel processes a session, two independent forwarders
run in parallel: agent-to-upstream requests (e.g. exec, env, pty-req)
go through sshForwardChannelRequests(srcReqs, dstChan), and
upstream-to-agent requests (e.g. exit-status) go the other way. The
agent-to-upstream loop is NOT awaited before sluice closes srcChan.
A fast upstream that, in response to exec, immediately replies + writes
data + sends exit-status + closes lets sluice's wait on the three
upstream-to-agent goroutines complete and close srcChan while the
agent-to-upstream forwarder is still mid-flight: it has received the
upstream's CHANNEL_REQUEST_SUCCESS reply via dstChan.SendRequest but
has not yet called req.Reply on the agent side. The agent then receives
SSH_MSG_CHANNEL_CLOSE before its session.SendRequest("exec", true, ...)
sees the SUCCESS message on ch.msg. gossh closes ch.msg on
SSH_MSG_CHANNEL_CLOSE, which causes the blocked SendRequest to return
io.EOF (channel.go:603-606). session.Output("cmd") surfaces this as
"exec command via SSH: EOF" even though the upstream replied
successfully.
The fix wraps each iteration of the agent-to-upstream forwarder in a
sync.WaitGroup. sshHandleChannel waits for the WaitGroup to drain after
the three upstream-side signals, then closes srcChan. Any in-flight
exec reply has been fully written to srcChan before the close arrives.
The race was deterministically reproducible on the e2e-linux push-event
runner since commit d27b05e moved srcChan.CloseWrite() out of the data
goroutine, narrowing the window between the upstream's reply landing
and sluice's close. Eight successive main-branch e2e runs passed
before d27b05e; the next two (after PR #38 and PR #40 merges) both
failed at TestCredential_SSHInjection with the EOF symptom.1 parent 55092f4 commit 15c9553
1 file changed
Lines changed: 60 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
263 | 264 | | |
264 | 265 | | |
265 | 266 | | |
266 | | - | |
267 | | - | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
268 | 285 | | |
269 | 286 | | |
270 | 287 | | |
| |||
304 | 321 | | |
305 | 322 | | |
306 | 323 | | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
307 | 333 | | |
308 | | - | |
309 | | - | |
310 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
311 | 338 | | |
312 | 339 | | |
313 | 340 | | |
| |||
328 | 355 | | |
329 | 356 | | |
330 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
0 commit comments