This is an automated email from the ASF dual-hosted git repository. nicoloboschi pushed a commit to branch branch-2.11 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit 247e05739fe56dd94ff13342512981ed71f48c02 Author: Nicolò Boschi <[email protected]> AuthorDate: Fri Nov 25 14:30:29 2022 +0100 [fix][cli] Pulsar shell: do not exit on user interrupt during commands (#18615) (cherry picked from commit 43da8b90ff7ca475b7335166a4b1378c8554edc4) --- .../java/org/apache/pulsar/shell/PulsarShell.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/PulsarShell.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/PulsarShell.java index 24a222e0e7f..3d31fce6cee 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/PulsarShell.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/PulsarShell.java @@ -122,6 +122,10 @@ public class PulsarShell { boolean noProgress; } + enum ExecState { + IDLE, + RUNNING + } private Properties properties; @Getter private final ConfigStore configStore; @@ -132,6 +136,7 @@ public class PulsarShell { private Function<Map<String, ShellCommandsProvider>, InteractiveLineReader> readerBuilder; private InteractiveLineReader reader; private final ConfigShell configShell; + private ExecState execState = ExecState.IDLE; public PulsarShell(String args[]) throws IOException { this(args, new Properties()); @@ -217,7 +222,18 @@ public class PulsarShell { } public void run() throws Exception { - final Terminal terminal = TerminalBuilder.builder().build(); + final Terminal terminal = TerminalBuilder.builder() + .nativeSignals(true) + .signalHandler(signal -> { + if (signal == Terminal.Signal.INT || signal == Terminal.Signal.QUIT) { + if (execState == ExecState.RUNNING) { + throw new InterruptShellException(); + } else { + exit(0); + } + } + }) + .build(); run((providersMap) -> { List<Completer> completers = new ArrayList<>(); String serviceUrl = ""; @@ -404,6 +420,7 @@ public class PulsarShell { Runtime.getRuntime().addShutdownHook(new Thread(() -> quit(terminal))); while (true) { + execState = ExecState.IDLE; final List<String> words; try { words = commandReader.readCommand(); @@ -411,6 +428,7 @@ public class PulsarShell { exit(0); return; } + execState = ExecState.RUNNING; final String line = words.stream().collect(Collectors.joining(" ")); if (StringUtils.isBlank(line)) { continue; @@ -434,6 +452,8 @@ public class PulsarShell { try { printExecutingCommands(terminal, commandsInfo, false); commandOk = pulsarShellCommandsProvider.runCommand(argv); + } catch (InterruptShellException t) { + // no-op } catch (Throwable t) { t.printStackTrace(terminal.writer()); } finally {
