Skip to content

Fix stdin behaviour when piping to mvnd#1648

Open
SapiensAnatis wants to merge 3 commits into
apache:masterfrom
SapiensAnatis:fix/stdin-available
Open

Fix stdin behaviour when piping to mvnd#1648
SapiensAnatis wants to merge 3 commits into
apache:masterfrom
SapiensAnatis:fix/stdin-available

Conversation

@SapiensAnatis

Copy link
Copy Markdown

Closes #1452.

This PR addresses two issues with standard input handling, when invoking mvnd in a dumb terminal with piped input, for example:

echo "Hello world" | mvnd somegoal

The motivating scenario for this is to use Spotless with mvnd, which includes an IDE integration that uses stdin to pipe a document to be formatted and sent back on stdout instead of providing a path and relying on slower filesystem reads/writes: https://github.com/diffplug/spotless/blob/main/plugin-maven/IDE_HOOK.md

The issues are:

  1. DaemonInputStream.available() often misleadingly reports 0 or 1, and in general can only report bytes that have already been pre-read into a buffer by the daemon
    • This PR addresses this by introducing 2 new message types: one that asks clients to call System.in.available() and one that contains the result of this operation.
  2. If TerminalInputHandler is able to read to the end of stdin (as it will in a pipe scenario) it sends an EOF message and then the data message. This leads to the data being ignored by DaemonInputStream as it sets the eof field to true, which causes a return from read with 0 bytes.
    • This PR re-arranges the order in which these messages are sent to ensure data is sent before an EOF.

Closes apache#1452

The current implementation of `available()` on DaemonInputStream requests
to read 1 byte and then attempts to calculate the available length from
that. This seems to cause problems with shells though, and reading the
1 byte will cause `available()` to affect the standard input when it
should be a no-op.

This commit adds a new message type that instructs the client to report
the number of bytes it has available, without actually reading any data.
This is then added to the existing stdin data we already have buffered.

It also adds an IT for this case, which failed before any code changes
but now passes. Manual testing was performed with bash to confirm that
the repro in apache#1452 now behaves identically on `mvnd` and `mvn`.
If TerminalInputHandler::handleProjectInput receives a small amount of
input (e.g. 12 chars) then it will reach the c < 0 EOF branch in the
loop. This sends an EOF message, then proceeds to send the data buffer.

This does not mesh well with how DaemonInputStream reads input. If
read() is suspended waiting for new data, receiving an EOF message first
will lead to the eof field being set to true, which means when `read`
wakes up it will end before it can actually read anything. This leads to
`System.in.readAllBytes` returning an empty array.

We can fix this by changing the order so that the data message is sent
before the EOF message.
Some tests which call `exec` goals were hanging because the exec plugin
behind the scenes may call `System.in.available()`, sending a
`RequestInputAvailable` message, but the `TestClientOutput` does not
respond to these, leading to an infinite deadlock.

We could make `TestClientOutput` respond, but it seems like it would be
a good idea to prevent a deadlock by imposing a maximum time limit, in
case a message gets dropped or something else goes wrong. If this does
occur, then `available` will be `Optional.empty()` and the input stream
will return only the number of bytes currently in its buffer.

This timeout has not been applied to `read()` because it feels more
natural / expected that `System.in.read()` could block indefinitely if
there is no standard input ready but the stream is open.
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.

Can only read 1 byte from stdin

1 participant