Catch errors in VM processes

This commit is contained in:
Tony Garnock-Jones 2012-03-24 19:29:00 -04:00
parent df3d76ae26
commit 859fb5a929
2 changed files with 23 additions and 8 deletions

View File

@ -19,9 +19,11 @@
(write `(sleeping for ,delay milliseconds)) (write `(sleeping for ,delay milliseconds))
(newline) (newline)
(sleep delay (lambda () (sleep delay (lambda ()
(write `(awoke after ,delay milliseconds)) (if (> delay 1500)
(newline) (error 'example-process "Oh noes!")
(transition 'no-state (kill))))) (begin (write `(awoke after ,delay milliseconds))
(newline)
(transition 'no-state (kill)))))))
(define spy (define spy
(lambda () (lambda ()

23
os2.rkt
View File

@ -228,10 +228,11 @@
[preaction [preaction
(loop rest (perform-action pid preaction state) outbound-actions)])]))) (loop rest (perform-action pid preaction state) outbound-actions)])])))
(define (send-to-user f . args) (define (send-to-user failure-proc f . args)
;; TODO: use this hook to find all the bits of code that will need ;; TODO: use this hook to find all the bits of code that will need
;; with-handlers and crash compensation. ;; with-handlers and crash compensation.
(apply f args)) (with-handlers ([exn:fail? failure-proc])
(apply f args)))
(define (perform-action pid preaction state) (define (perform-action pid preaction state)
(match preaction (match preaction
@ -332,7 +333,12 @@
(define (run-trapk state pid trap-k . args) (define (run-trapk state pid trap-k . args)
(if trap-k (if trap-k
(run-ready state pid (apply send-to-user trap-k args)) (run-ready state pid (apply send-to-user
(lambda (e) (lambda (process-state)
(transition process-state
(kill #f e))))
trap-k
args))
state)) state))
(define (maybe-transition->transition t) (define (maybe-transition->transition t)
@ -343,14 +349,21 @@
(define (run-ready state pid interrupt-k) (define (run-ready state pid interrupt-k)
(define old-process (hash-ref (vm-processes state) pid)) (define old-process (hash-ref (vm-processes state) pid))
(match-define (transition new-process-state actions) (match-define (transition new-process-state actions)
(maybe-transition->transition (send-to-user interrupt-k (process-state old-process)))) (maybe-transition->transition (send-to-user (lambda (e)
(transition (process-state old-process)
(kill #f e)))
interrupt-k
(process-state old-process))))
(struct-copy vm (enqueue-actions state pid actions) (struct-copy vm (enqueue-actions state pid actions)
[processes (hash-set (vm-processes state) pid [processes (hash-set (vm-processes state) pid
(struct-copy process old-process (struct-copy process old-process
[state new-process-state]))])) [state new-process-state]))]))
(define (do-spawn spawning-pid thunk k state) (define (do-spawn spawning-pid thunk k state)
(match-define (transition initial-state initial-actions) (send-to-user thunk)) (match-define (transition initial-state initial-actions) (send-to-user (lambda (e)
(transition #f
(kill #f e)))
thunk))
(define new-pid (vm-next-process-id state)) (define new-pid (vm-next-process-id state))
(run-trapk (struct-copy vm (enqueue-actions state new-pid initial-actions) (run-trapk (struct-copy vm (enqueue-actions state new-pid initial-actions)
[processes (hash-set (vm-processes state) new-pid (process new-pid [processes (hash-set (vm-processes state) new-pid (process new-pid