Skip to content

Retry partial writes in ResourceOutputStream - #27

Open
AydinHassan wants to merge 1 commit into
masterfrom
fix-partial-writes
Open

AydinHassan wants to merge 1 commit into
masterfrom
fix-partial-writes

Conversation

@AydinHassan

Copy link
Copy Markdown
Member

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, so O_NONBLOCK applies to STDOUT too. A cli-menu redraw is a few KB of escape codes, and fwrite on 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 if fwrite returns false).

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant