diff --git a/os2.rkt b/os2.rkt index 6e6a833..e788d9d 100644 --- a/os2.rkt +++ b/os2.rkt @@ -193,30 +193,33 @@ hs))])) (define (do-unsubscribe pid eid reason state) - (define endpoint-to-remove (hash-ref (vm-endpoints state) eid)) - (define removed-topic (endpoint-topic endpoint-to-remove)) - (define old-process (hash-ref (vm-processes state) pid)) - (define new-process (struct-copy process old-process - [endpoints (set-remove (process-endpoints old-process) eid)])) - (let ((state (struct-copy vm state - [endpoints (hash-remove (vm-endpoints state) eid)] - [processes (if (set-empty? (process-endpoints new-process)) - (hash-remove (vm-processes state) pid) - (hash-set (vm-processes state) pid new-process))]))) - (for*/fold ([state state]) - ([(matching-pid p) (in-hash (vm-processes state))] - [matching-eid (in-set (process-endpoints p))] - [e (in-value (hash-ref (vm-endpoints state) matching-eid))] - [matching-topic (in-value (endpoint-topic e))] - [flow-pattern (in-value (topic-intersection removed-topic matching-topic))] - #:when flow-pattern) - (define outbound-flow (refine-topic removed-topic flow-pattern)) - (run-trapk state - matching-pid - (handlers-absence (endpoint-handlers e)) - matching-eid - outbound-flow - reason)))) + (cond + [(hash-has-key? (vm-endpoints state) eid) + (define endpoint-to-remove (hash-ref (vm-endpoints state) eid)) + (define removed-topic (endpoint-topic endpoint-to-remove)) + (define old-process (hash-ref (vm-processes state) pid)) + (define new-process (struct-copy process old-process + [endpoints (set-remove (process-endpoints old-process) eid)])) + (let ((state (struct-copy vm state + [endpoints (hash-remove (vm-endpoints state) eid)] + [processes (if (set-empty? (process-endpoints new-process)) + (hash-remove (vm-processes state) pid) + (hash-set (vm-processes state) pid new-process))]))) + (for*/fold ([state state]) + ([(matching-pid p) (in-hash (vm-processes state))] + [matching-eid (in-set (process-endpoints p))] + [e (in-value (hash-ref (vm-endpoints state) matching-eid))] + [matching-topic (in-value (endpoint-topic e))] + [flow-pattern (in-value (topic-intersection removed-topic matching-topic))] + #:when flow-pattern) + (define outbound-flow (refine-topic removed-topic flow-pattern)) + (run-trapk state + matching-pid + (handlers-absence (endpoint-handlers e)) + matching-eid + outbound-flow + reason)))] + [else state])) (define (route-and-deliver message-topic body state) (define pids-and-endpoints