Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Trap Ctrl-C in the REPL: if no command is running clear the prompt, if some command is running ask for confirmation before exiting #24127

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

Open
lihaoyi wants to merge 6 commits into scala:main
base: main
Choose a base branch
Loading
from lihaoyi:trap-ctrl-c

Conversation

Copy link
Contributor

@lihaoyi lihaoyi commented Oct 2, 2025
edited
Loading

This ports over one of the other Ammonite features to the Scala 3 REPL, making it behave much more similarly to other prompt environment (Bash, Python, etc.):

  • If there is no code running, Ctrl-C just resets the prompt without exiting
  • If there is code running, Ctrl-C first interrupts the thread to try and force a gentle exit. A second Ctrl-C would then terminate the process.

Unfortunately we will no longer be able to terminate the thread forcefully, as Thread.stop is busted Java >20 (see com-lihaoyi/Ammonite#1379), so using Thread.interrupt and sys.exit is the best we have

Tested manually using bin/scala. Doesn't work in sbt repl due to SBT also intercepting the signal, but that's a separate issue we can follow up later

bishabosha reacted with heart emoji
Copy link
Member

bishabosha commented Oct 2, 2025
edited
Loading

scala> while true do
 | Thread.sleep(1000)
 | println("still alive!")
 |
still alive!
still alive!
^C
Interrupting running thread, Ctrl-C again to terminate the REPL Process
java.lang.InterruptedException: sleep interrupted
 at java.base/java.lang.Thread.sleep(Native Method)
 ... 32 elided
scala>

Nice!

Copy link
Member

bishabosha commented Oct 2, 2025
edited
Loading

(削除) the double Ctl+c doesn't quit for me but keeps resetting the prompt (削除ここまで)

Edit: it has to be a true infinite loop, with no yielding, for double interrupt to work (so no sleep)

Copy link
Contributor Author

lihaoyi commented Oct 2, 2025

Ctrl-C only exits the process when it is running code that it is unable to interrupt. Doesn't need to be infinite, as long as it's running: the first Ctrl-C will ask for confirmation, the second Ctrl-C will exit the process

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /