jbonofre commented on code in PR #2926:
URL: https://github.com/apache/karaf/pull/2926#discussion_r4578601952
##########
client/src/main/java/org/apache/karaf/client/Main.java:
##########
@@ -188,7 +188,10 @@ else if (console != null) {
if (config.getCommand().length() > 0) {
ChannelExec channel =
session.createExecChannel(config.getCommand() + "\n");
channel.setIn(new ByteArrayInputStream(new byte[0]));
- if (!config.isBatch()) {
+ // watch stdin to let the user abort the command with EOF
(ctrl-d), but only when
+ // stdin is a terminal - when it is redirected (script,
pipe, cron) EOF is reached
+ // immediately and would close the channel before any
output is received
+ if (!config.isBatch() && isTerminal(console)) {
Review Comment:
Before JDK 22 `System.console()` is only non-null when stdin and stdout are
terminals, and from JDK 22 `Console.isTerminal()` checks the same thing. So
this also turns the watcher off when stdin is a terminal but stdout is
redirected or piped, e.g. `bin/client log:tail > out.log` or `bin/client
log:tail | grep ERROR` from a shell. In that setup JLine still builds a system
terminal on stderr with `SIG_IGN`, so CTRL-C and CTRL-Z are ignored too, and
the client can only be stopped with `kill` from another shell. On `main`,
CTRL-D stops it. I reproduced this on JDK 17, 21, 25 and 27, with JLine 4.4.8
and 4.6.0.
The terminal built just above already has the answer: when JLine can't use
stdin as a terminal, it falls back to a `DumbTerminal`, which doesn't install
the signal handlers, so CTRL-C works there. Gating on it keeps CTRL-D exactly
where CTRL-C is ignored:
```suggestion
// watch stdin to let the user abort the command with
EOF (ctrl-d), as JLine ignores ctrl-c;
// skipped when stdin is not a terminal (script, pipe,
cron): JLine then falls back to a dumb
// terminal, EOF would close the channel before any
output, and ctrl-c still works
if (!config.isBatch() && !(terminal instanceof
DumbTerminal)) {
```
(plus `import org.jline.terminal.impl.DumbTerminal;`). I tried this on JDK
17 and 25: `< /dev/null` and runs with no terminal at all still print the
output and exit 0, CTRL-D works again with stdout redirected or piped, and
CTRL-C stops the command when stdin is not a terminal. One difference from
`main`: with `TERM` unset JLine also returns a dumb terminal, so CTRL-D is off
there, but CTRL-C works.
It should also cover `< NUL` on Windows, which `Console` counts as a
terminal because NUL is a character device, while JLine checks the console mode.
##########
client/src/main/java/org/apache/karaf/client/Main.java:
##########
@@ -356,6 +359,19 @@ private static int getFlag(Attributes attributes,
LocalFlag flag) {
return attributes.getLocalFlag(flag) ? 1 : 0;
}
+ private static boolean isTerminal(Console console) {
+ if (console == null) {
+ return false;
+ }
+ try {
+ // Console.isTerminal() exists since Java 22, where
System.console() also returns
+ // a console for redirected streams - before that a console
implies a terminal
+ return (Boolean)
Console.class.getMethod("isTerminal").invoke(console);
+ } catch (Exception e) {
Review Comment:
With the JLine check above, this helper can go. If you keep it, only
`NoSuchMethodException` means "JDK older than 22", catching `Exception` would
turn the watcher back on for any other failure.
--
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]