From b409063ff2b0e7d0550f00ec36c1ce5547ada6e1 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 10 Jun 2021 16:46:20 +0200 Subject: [PATCH] Handle EPIPE in tcp driver --- syndicate/drivers/tcp.rkt | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/syndicate/drivers/tcp.rkt b/syndicate/drivers/tcp.rkt index ecdc32e..a3d5e7f 100644 --- a/syndicate/drivers/tcp.rkt +++ b/syndicate/drivers/tcp.rkt @@ -16,6 +16,8 @@ (require syndicate/schemas/gen/tcp) (require syndicate/schemas/gen/dataspace-patterns) +(require (for-syntax racket/base)) + (define-logger syndicate/drivers/tcp) (define (spawn-tcp-driver ds) @@ -108,17 +110,33 @@ (turn! facet (lambda () (send-data target bs))) (loop)))))) +(define-syntax (EPIPE stx) + (local-require ffi/unsafe) + (define errno-value (cons (lookup-errno 'EPIPE) 'posix)) + (syntax-case stx () + [(_) #`'#,errno-value])) + +(define (with-stop-current-facet-on-epipe operation thunk) + (with-handlers ([(lambda (e) + (and (exn:fail:network:errno? e) + (equal? (exn:fail:network:errno-errno e) (EPIPE)))) + (lambda (e) + (log-syndicate/drivers/tcp-debug "epipe while ~a" operation) + (stop-current-facet))]) + (thunk))) + (define (outbound-relay name o) (define flush-pending #f) (lambda (payload) (log-syndicate/drivers/tcp-debug "outbound data ~v for ~v" payload name) - (write-bytes payload o) + (with-stop-current-facet-on-epipe 'writing (lambda () (write-bytes payload o))) (when (not flush-pending) (set! flush-pending #t) (facet-on-end-of-turn! this-facet (lambda () (set! flush-pending #f) - (flush-output o)))))) + (with-stop-current-facet-on-epipe 'flushing + (lambda () (flush-output o)))))))) (define (read-bytes-avail input-port #:limit [limit 65536]) (define buffer (make-bytes limit))