Pergunta

I am currently playing with sb-thread API, provided by SBCL, wondering what happens if an error is thrown inside a started thread and how to ensure that only that process is affected (and dies), and no other process is, as apparently the debugger will be entered even though not the main thread throws an error.

* (handler-case 
    (sb-thread:join-thread (sb-thread:make-thread #'(lambda()
                              (error 'simple-error))))
  (sb-thread:join-thread-error (err)
    (sb-thread:thread-error-thread err)
    (FORMAT t "allemeineentchen~%")))

(A SIMPLE-ERROR was caught when trying to print *DEBUG-CONDITION* when entering
the debugger. Printing was aborted and the SIMPLE-ERROR was stored in
SB-DEBUG::*NESTED-DEBUG-CONDITION*.)
;after this sbcl just yields until str-c enters the debugger

My suggestion would be to make each thread function body starting with an (handler-case (body) (error (err) err) but this seems awfully non-standard/malpractice and only works with threads whose function body are created by me, and I am not sure that this will, in every case, prevent entering the debugger.

Is there some guideline/(unofficial)standard concerning this topic?

Foi útil?

Solução

You need to be very clear about what is being signalled and what is being handled (and where): First, what error condition are you trying to handle? When you call #'error in your thread with 'simple-error, that is a problem, because sbcl will not be able to print the error which you have signalled, but otherwise things are working as they should. If I do this:

(handler-case 
         (sb-thread:join-thread 
          (sb-thread:make-thread #'(lambda()
                                     (error "simple error"))))
       (simple-error (c)
         c)
       (sb-thread:join-thread-error (c)
         (sb-thread:thread-error-thread c)))

Sbcl correctly enters the debugger for the unhandled simple-error, however the join-thread-error which gets signalled after I use the abort restart from the debugger does get handled, and I think that is what you were asking about:

simple error
[Condition of type SIMPLE-ERROR]

Restarts:
 0: [ABORT] Abort thread (#<THREAD RUNNING {DABCAD1}>) 0 ==> #<SB-THREAD:THREAD ABORTED {DABCAD1}>

Edit: I am adding this after reading Sim's comment:

If you just want to ignore any errors in the thread then do that:

(sb-thread:join-thread 
          (sb-thread:make-thread #'(lambda() (ignore-errors
                                               (error "simple error")))))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top