On Wednesday, May 3, 2017 at 3:04:20 PM UTC-7, Ryan Culpepper wrote: > On 5/3/17 10:41 PM, Eric Griffis wrote: > > Hello, > > > > I'm having trouble catching "terminate break" exceptions when combining > > break-thread with thread-wait. > > > > MWE 1: > > > > (with-handlers ([exn:break:terminate? writeln]) > > (let ([t (thread (lambda () (thread-wait (current-thread))))]) > > (break-thread t 'terminate) > > (thread-wait t))) > > Threads do not inherit exception handlers. You need to move the > `with-handlers` to the new thread: > > (let ([t (thread (lambda () > (with-handlers ([exn:break:terminate? writeln]) > (thread-wait (current-thread)))))]) > (break-thread t 'terminate) > (thread-wait t)) > > Except that isn't quite right either, because , the main thread might > (very likely) send the break to `t` before `t` is ready to catch it. So > we need some additional synchronization: > > (define t-ready (make-semaphore 0)) > (let ([t (thread (lambda () > (with-handlers ([exn:break:terminate? writeln]) > (semaphore-post t-ready) > (thread-wait (current-thread)))))]) > (semaphore-wait t-ready) > (break-thread t 'terminate) > (thread-wait t)) > > That version should reliably print out the break exception. > > Ryan
Got it. I had with-handlers inside the thread at first, but got the same result. A simple test showed clearly that the thread was not ready to catch the break, hence my attempt to extract with-handlers. The semaphore did the trick, though. The big lesson (for me) here is that exceptions inside a thread do not "bubble up" to the parent. This was not obvious (to me) from the docs. My code is now behaving predictably. Thanks, Ryan. Eric -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.