SapiensAnatis commented on issue #1452: URL: https://github.com/apache/maven-mvnd/issues/1452#issuecomment-4746460138
@gnodet I found some free time to look into this properly and have prepared a fix locally. Before I put up a PR I just wanted to check the general approach with you: I think there are two issues at play here: ### 1. `DaemonInputStream.available()` The existing implementation of `available()` in `DaemonInputStream` tries to read 1 byte and then returns the length of whatever it has in its buffer: https://github.com/apache/maven-mvnd/blob/c6df09e891ec442e3ecef8b946d834cad8c5c754/daemon/src/main/java/org/mvndaemon/mvnd/daemon/DaemonInputStream.java#L75-L84 This seems incorrect to me because it will consume one byte from the stdin buffer, which has side effects - but maybe that's OK if it remains in the mvnd buffer to be returned on the next read. But I'm confused: would it ever read more than 1 byte to report? I have addressed this by adding two new message types, one that allows the daemon to directly ask a client to call `System.in.available()`, and another that allows the client to respond with the number. From testing, this makes `System.in.available()` return the correct number of bytes instead of 0. ### 2. Message ordering `TerminalInputHandler::handleProjectInput`, on encountering an EOF, sends an EOF message and breaks out of the loop, then sends what it has in its data buffer. This does not mesh well with how DaemonInputStream reads input. If read() is blocking 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 the data message that comes next. This leads to `System.in.readAllBytes` returning an empty array. I have refactored the loop logic a bit so that if we encounter EOF we will send the data buffer _first_ before the EOF message, and then return. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
