Restarting loop from exception handler breaks with-handlers, Ctrl+C?

10 views
Skip to first unread message

William J. Bowman

unread,
Oct 7, 2021, 8:36:04 PM10/7/21
to Racket Users
I have a little model of a server below. The server handles all exceptions, and restarts itself after some errors. It also handles break?, to actually exit when requested.

While this server is running, if I press Ctrl+C before `(error 'foo)` is handled, the server exits correctly with 120. However, if I press Ctrl+C after `(error 'foo)` is handled, the `exn:break?` handler is never called, the server no longer responds to `Ctrl+C`, and (in this example) it eventually exits with status `1`.

I cannot figure out why. As far as I can tell, this should always handle `Ctrl+C` by exiting with 120.

> #lang racket/base
>
> (let loop ([n 0])
> (with-handlers ([exn:break? (lambda _ (exit 120))]
> [void
> (lambda (exn)
> (printf "handled error: ~a~n" (exn-message exn))
> (loop (add1 n)))])
> (displayln n)
> (sleep 1)
> (cond
> [(zero? n) (error 'foo)]
> [(= n 5) (exit 1)])
> (loop (add1 n))))

--
William J. Bowman

Sorawee Porncharoenwase

unread,
Oct 7, 2021, 8:52:14 PM10/7/21
to William J. Bowman, Racket Users
I think you want with-handler*? According to the docs:

with-handlers:

Before any predicate or handler procedure is invoked, the continuation of the entire with-handlers expression is restored, but also parameterize-breaked to disable breaks. Thus, breaks are disabled by default during the predicate and handler procedures (see Breaks), and the exception handler is the one from the continuation of the with-handlers expression.

with-handler*

Like with-handlers, but if a handler-expr procedure is called, breaks are not explicitly disabled, and the handler call is in tail position with respect to the with-handlers* form.

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/YV%2BSWNpldfOB5tA3%40williamjbowman.com.

Sorawee Porncharoenwase

unread,
Oct 7, 2021, 8:53:08 PM10/7/21
to William J. Bowman, Racket Users
Typo: with-handlers*

William J. Bowman

unread,
Oct 7, 2021, 10:49:53 PM10/7/21
to Sorawee Porncharoenwase, Racket Users
Ah of course, thanks!

On Thu, Oct 07, 2021 at 05:52:55PM -0700, Sorawee Porncharoenwase wrote:
> Typo: with-handlers*
>
> On Thu, Oct 7, 2021 at 5:52 PM Sorawee Porncharoenwase <
> sorawe...@gmail.com> wrote:
>
> > I think you want with-handler*? According to the docs:
> >
> > with-handlers:
> >
> > Before any predicate or handler procedure is invoked, the continuation of
> > the entire with-handlers
> > <https://docs.racket-lang.org/reference/exns.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._with-handlers%29%29>
> > expression is restored, but also parameterize-break
> > <https://docs.racket-lang.org/reference/breakhandler.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._parameterize-break%29%29>ed
> > to disable breaks. Thus, breaks are disabled by default during the
> > predicate and handler procedures (see Breaks
> > <https://docs.racket-lang.org/reference/breakhandler.html>), and the
> > exception handler is the one from the continuation of the with-handlers
> > <https://docs.racket-lang.org/reference/exns.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._with-handlers%29%29>
> > expression.
> >
> > with-handler*
> >
> > Like with-handlers
> > <https://docs.racket-lang.org/reference/exns.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._with-handlers%29%29>,
> > but if a handler-expr procedure is called, breaks are not explicitly
> > disabled, and the handler call is in tail position with respect to the
> > with-handlers*
> > <https://docs.racket-lang.org/reference/exns.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._with-handlers%2A%29%29>
Reply all
Reply to author
Forward
0 new messages