Repository navigation
Retry partial writes in ResourceOutputStream - #27
Open
AydinHassan wants to merge 1 commit into
Open
AydinHassan wants to merge 1 commit into
AydinHassan wants to merge 1 commit into
Conversation
ResourceInputStream switches STDIN to non-blocking after reading input so pasted text can be read in one go. On a TTY, STDIN and STDOUT share a file description, so STDOUT becomes non-blocking too and fwrite can write only part of a large buffer. cli-menu redraws were truncated after the first keypress, garbling the menu. Keep writing until the whole buffer is out.
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.
Problem
Since 0.3.0, cli-menu (and every PHP School workshop on php-workshop master) garbles the menu as soon as you press up or down. The first draw is fine.
ResourceInputStream::read()sets STDIN to non-blocking after reading input, to handle pasted text (#14). On a TTY, STDIN and STDOUT share a file description, soO_NONBLOCKapplies to STDOUT too. A cli-menu redraw is a few KB of escape codes, andfwriteon a non-blocking TTY can write only part of it.ResourceOutputStream::write()ignored the return value, so the rest of the frame was dropped.Fix
Loop in
ResourceOutputStream::write()until the whole buffer has been written (stop iffwritereturnsfalse).Added a regression test using a stream wrapper that alternates between accepting 2 bytes and accepting nothing, which is what a full non-blocking stream does. It fails without the fix.
Verified by driving a workshop menu in an emulated 140x50 terminal: before, three down presses left a corrupted frame; after, the selection moves cleanly.
Once merged, tagging 0.3.1 fixes cli-menu 4.5 and php-workshop without changes there, since they require
^0.3.