shim/task: wait for IO drain before closing stream connections#211
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a close-before-drain race in the shim IO shutdown path by ensuring stdout/stderr copy goroutines can finish draining buffered data before the underlying vsock stream connections are closed.
Changes:
- Wait for
ioDone(stdout/stderr copy completion) before closing the stream connections during IO shutdown. - Preserve
ctx.Done()as an exceptional-case escape hatch, returning the context error when cancellation/deadline occurs.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ioShutdown was closing the vsock stream connections before the host copy goroutines had finished draining them. A goroutine blocked in io.CopyBuffer's Read would see 'use of closed network connection' and return early, silently dropping bytes that were still buffered in the kernel socket receive queue (close-before-drain race). Fix: wait for ioDone (closed when both stdout and stderr copy goroutines return) before closing streams. In normal operation ioDone always fires on its own — the container process exiting closes the runc pipe, vminitd sees EOF and closes its vsock conn, which propagates EOF to the host copy goroutines. ctx.Done() is only reached in exceptional cases (VM crash, vminitd hang, kernel wedge), where it acts as a deadline-bounded fallback to avoid pinning cleanup indefinitely. Signed-off-by: Derek McGowan <derek@mcg.dev>
b0831a2 to
918929f
Compare
Member
Author
|
Shim tests currently fail without this change https://github.com/dmcgowan/shimtest/actions/runs/26741679025/job/78806983484 |
austinvazquez
approved these changes
Jun 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ioShutdown was closing the vsock stream connections before the host copy goroutines had finished draining them. A goroutine blocked in io.CopyBuffer's Read would see 'use of closed network connection' and return early, silently dropping bytes that were still buffered in the kernel socket receive queue (close-before-drain race).
Fix: wait for ioDone (closed when both stdout and stderr copy goroutines return) before closing streams. In normal operation ioDone always fires on its own — the container process exiting closes the runc pipe, vminitd sees EOF and closes its vsock conn, which propagates EOF to the host copy goroutines. ctx.Done() is only reached in exceptional cases (VM crash, vminitd hang, kernel wedge), where it acts as a deadline-bounded fallback to avoid pinning cleanup indefinitely.