Handle EPIPE in tcp driver

This commit is contained in:
Tony Garnock-Jones 2021-06-10 16:46:20 +02:00
parent 6ea074948a
commit b409063ff2
1 changed files with 20 additions and 2 deletions

View File

@ -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))