Google Groups

Re: Clojure infinite loop

Chouser Mar 6, 2009 2:58 PM
Posted in group: Clojure
On Fri, Mar 6, 2009 at 5:05 PM, Jason Wolfe <> wrote:
>> (Ctrl-C pressed here)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> KeyboardInterrupt
> That is something I miss from SBCL.  In SLIME-SBCL, you can just Ctrl-
> C Ctrl-C to interrupt your code.

I just added a couple functions to clojure.contrib.repl-utils in an
attempt to support Ctrl-C:

  user=> (use 'clojure.contrib.repl-utils)
  user=> (add-break-thread!)
  {1 #<WeakReference java.lang.ref.WeakReference@e29820>}

This registers the current thread to be stopped next time an INT
signal is recieved, which happens when the user presses Ctrl-C.  Try

  user=> (Thread/sleep 10000)

Then press Ctrl-C before the 10 seconds are up, and you'll see:

  java.lang.Exception: SIGINT (NO_SOURCE_FILE:0)

...and you're back at the repl so you can try something else.  This
works for busy loops and should work for IO and other blocking

It does use Thread.stop(), which is deprecated and has all
kinds of important warnings.  But are the risks of using Thread.stop()
really worse than the consequences of shutting down the entire JVM?
I'll leave that for you to decide.

Please let me know if this fails in some case, or even more
importantly if it causes any kind of breakage.