Fix shim fully drain stdin on close IO#246
Open
austinvazquez wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the shim’s stdin forwarding logic to ensure that when CloseIO fires, any remaining bytes already buffered on the stdin reader are fully drained and forwarded before the shim sends an in-band EOF (CloseWrite) to the guest.
Changes:
- Replace the single “drain one read” behavior on
CloseIOwith a loop that continues reading/writing until the stdin reader returns EOF or an error. - Expand the in-code rationale to document why a single read can truncate stdin under backpressure (e.g., slow guest consumption / vsock credit).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
cc0a1a4 to
2316bcb
Compare
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
2316bcb to
f054a26
Compare
Comment on lines
+62
to
86
| for draining := true; draining; { | ||
| select { | ||
| case res := <-readCh: | ||
| if res.n > 0 { | ||
| if _, err := sc.Write(buf[:res.n]); err != nil { | ||
| log.G(ctx).WithError(err).Warn("error writing stdin on CloseIO") | ||
| draining = false | ||
| break | ||
| } | ||
| } | ||
| if res.err != nil { | ||
| // EOF (write end closed) or read error: fully drained. | ||
| draining = false | ||
| break | ||
| } | ||
| // More may be buffered; continue to drain. | ||
| go read() | ||
| case <-time.After(stdinDrainGrace): | ||
| // Stalled draining the buffer; EOF not yet reached. | ||
| log.G(ctx).Warn("stdin drain stalled after CloseIO; signaling EOF without client write-end close") | ||
| f.Close() | ||
| <-readCh | ||
| draining = false | ||
| } | ||
| } |
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.
This change addresses an edge case where draining stdin on close will truncate data. Attempt to read until EOF and bind the read with a timeout (currently 2 seconds which should only occur for bad clients that trigger CloseIO without close the pipe's write end)
Ref: https://github.com/containerd/nerdbox/actions/runs/28613973997/job/84853144969?pr=245