Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][cli] Pulsar shell: do not exit on user interrupt during commands #18615

Merged
merged 2 commits into from
Nov 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ static final class MainOptions {
boolean noProgress;
}

enum ExecState {
IDLE,
RUNNING
}
private Properties properties;
@Getter
private final ConfigStore configStore;
Expand All @@ -132,6 +136,7 @@ static final class MainOptions {
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());
Expand Down Expand Up @@ -217,7 +222,18 @@ public void reload(Properties properties) throws Exception {
}

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();
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
} else {
exit(0);
}
}
})
.build();
run((providersMap) -> {
List<Completer> completers = new ArrayList<>();
String serviceUrl = "";
Expand Down Expand Up @@ -404,13 +420,15 @@ public List<String> readCommand() {

Runtime.getRuntime().addShutdownHook(new Thread(() -> quit(terminal)));
while (true) {
execState = ExecState.IDLE;
final List<String> words;
try {
words = commandReader.readCommand();
} catch (InterruptShellException interruptShellException) {
exit(0);
return;
}
execState = ExecState.RUNNING;
final String line = words.stream().collect(Collectors.joining(" "));
if (StringUtils.isBlank(line)) {
continue;
Expand All @@ -434,6 +452,8 @@ public List<String> readCommand() {
try {
printExecutingCommands(terminal, commandsInfo, false);
commandOk = pulsarShellCommandsProvider.runCommand(argv);
} catch (InterruptShellException t) {
// no-op
} catch (Throwable t) {
t.printStackTrace(terminal.writer());
} finally {
Expand Down