Fix stdin behaviour when piping to mvnd#1648
Open
SapiensAnatis wants to merge 3 commits into
Open
Conversation
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.
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.
Closes #1452.
This PR addresses two issues with standard input handling, when invoking
mvndin a dumb terminal with piped input, for example: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:
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 daemonSystem.in.available()and one that contains the result of this operation.TerminalInputHandleris 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 byDaemonInputStreamas it sets theeoffield to true, which causes a return fromreadwith 0 bytes.