Skip to content

Conversation

lihaoyi
Copy link
Contributor

@lihaoyi lihaoyi commented Oct 2, 2025

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
Copy link
Member

bishabosha commented Oct 2, 2025

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!

@bishabosha
Copy link
Member

bishabosha commented Oct 2, 2025

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)

@lihaoyi
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants