Skip to content

Commit c19b944

Browse files
committed
racket/port: fix evt constructors to work right with read errors
Propoagate read errors to the `sync` that is applied to an event, instead of writing the events to stdout. Related to racket/web-server#76
1 parent 89403dc commit c19b944

3 files changed

Lines changed: 93 additions & 13 deletions

File tree

‎pkgs/racket-doc/scribblings/reference/port-lib.scrbl‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,18 @@ closes @racket[in].}
655655
Returns a @tech{synchronizable event} that is ready when
656656
@racket[in] produces an @racket[eof]. If @racket[in] produces a
657657
mid-stream @racket[eof], the @racket[eof] is consumed by the event
658-
only if the event is chosen in a synchronization.}
658+
only if the event is chosen in a synchronization.
659+
660+
If attempting to read from @racket[in] raises an exception during a
661+
synchronization attempt, then the exception may be reported during the
662+
synchronization attempt, but it will silently discarded if some another
663+
event in the same synchronization is selected or if some other event
664+
raises an exception first.
665+
666+
@history[#:changed "7.5.0.3" @elem{Changed handling of read errors so
667+
they are propagated to a synchronization attempt,
668+
instead of treated as unhandled errors in a
669+
background thread.}]}
659670

660671

661672
@defproc[(read-bytes-evt [k exact-nonnegative-integer?] [in input-port?])
@@ -681,12 +692,14 @@ concurrently---and each synchronization corresponds to a distinct read
681692
request.
682693

683694
The @racket[in] must support progress events, and it must not produce
684-
a special non-byte value during the read attempt.}
695+
a special non-byte value during the read attempt.
696+
697+
Exceptions attempting to read from @racket[in] are handled in the same
698+
way as by @racket[eof-evt].}
685699

686700

687701
@defproc[(read-bytes!-evt [bstr (and/c bytes? (not/c immutable?))]
688-
[in input-port?]
689-
[progress-evt (or/c progress-evt? #f)])
702+
[in input-port?])
690703
evt?]{
691704

692705
Like @racket[read-bytes-evt], except that the read bytes are placed
@@ -703,7 +716,10 @@ might be mutated if the event is not selected by a synchronzation;
703716
nevertheless, multiple synchronization attempts can use the same
704717
result from @racket[read-bytes!-evt] as long as there is no
705718
intervening read on @racket[in] until one of the synchronization
706-
attempts selects the event.}
719+
attempts selects the event.
720+
721+
Exceptions attempting to read from @racket[in] are handled in the same
722+
way as by @racket[eof-evt].}
707723

708724

709725
@defproc[(read-bytes-avail!-evt [bstr (and/c bytes? (not/c immutable?))] [in input-port?])
@@ -730,7 +746,7 @@ a byte string.}
730746

731747

732748
@defproc[(read-line-evt [in input-port?]
733-
[mode (or/c 'linefeed 'return 'return-linefeed 'any 'any-one)])
749+
[mode (or/c 'linefeed 'return 'return-linefeed 'any 'any-one) 'linefeed])
734750
evt?]{
735751

736752
Returns a @tech{synchronizable event} that is ready when a line of
@@ -741,11 +757,14 @@ separator).
741757

742758
A line is read from the port if and only if the event is chosen in a
743759
synchronization, and the returned line always represents contiguous
744-
bytes in the port's stream.}
760+
bytes in the port's stream.
761+
762+
Exceptions attempting to read from @racket[in] are handled in the same
763+
way as by @racket[eof-evt].}
745764

746765

747766
@defproc[(read-bytes-line-evt [in input-port?]
748-
[mode (or/c 'linefeed 'return 'return-linefeed 'any 'any-one)])
767+
[mode (or/c 'linefeed 'return 'return-linefeed 'any 'any-one) 'linefeed])
749768
evt?]{
750769

751770
Like @racket[read-line-evt], but returns a byte string instead of a
@@ -762,7 +781,7 @@ string.}
762781
[(peek-string!-evt [str (and/c string? (not/c immutable?))] [skip exact-nonnegative-integer?]
763782
[progress-evt (or/c progress-evt? #f)] [in input-port?]) evt?])]{
764783

765-
Like the @racket[read-...-evt] functions, but for peeking. The
784+
Like the @racket[read-bytes-evt], etc., functions, but for peeking. The
766785
@racket[skip] argument indicates the number of bytes to skip, and
767786
@racket[progress-evt] indicates an event that effectively cancels the peek
768787
(so that the event never becomes ready). The @racket[progress-evt]
@@ -796,7 +815,10 @@ each synchronization corresponds to a distinct match request.
796815

797816
The @racket[in] port must support progress events. If @racket[in]
798817
returns a special non-byte value during the match attempt, it is
799-
treated like @racket[eof].}
818+
treated like @racket[eof].
819+
820+
Exceptions attempting to read from @racket[in] are handled in the same
821+
way as by @racket[eof-evt].}
800822

801823
@; ----------------------------------------------------------------------
802824

‎pkgs/racket-test-core/tests/racket/portlib.rktl‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,61 @@
682682
(sync (system-idle-evt))
683683
(write-char #\b o)))
684684
(test 1 peek-bytes-avail! (make-bytes 1) 2 #f (make-limited-input-port i 10)))
685-
685+
686+
;; ----------------------------------------
687+
;; Check that events raise an exception in the right thread
688+
;; when a point goes bad
689+
690+
(let ()
691+
(define (check make-evt)
692+
(define-values (p fail)
693+
(let* ([s (make-semaphore)]
694+
[e (wrap-evt (semaphore-peek-evt s)
695+
(lambda (v) 0))])
696+
(values
697+
(make-input-port
698+
'test
699+
(lambda (bsr)
700+
(if (sync/timeout 0 e)
701+
(raise 'forced-failure)
702+
e))
703+
(lambda (bstr offset evt)
704+
(if (sync/timeout 0 e)
705+
(raise 'forced-failure)
706+
e))
707+
void
708+
(lambda ()
709+
(make-semaphore))
710+
(lambda (n evt1 evt2)
711+
(error "no")))
712+
(lambda ()
713+
(semaphore-post s)))))
714+
715+
(thread (lambda ()
716+
(sync (system-idle-evt))
717+
(fail)))
718+
719+
(err/rt-test (sync (make-evt p))
720+
(lambda (exn) (eq? exn 'forced-failure))))
721+
722+
(check (lambda (p) (eof-evt p)))
723+
(check (lambda (p) (read-bytes-evt 10 p)))
724+
(check (lambda (p) (read-bytes!-evt (make-bytes 10) p)))
725+
(check (lambda (p) (read-bytes-avail!-evt (make-bytes 10) p)))
726+
(check (lambda (p) (read-string-evt 10 p)))
727+
(check (lambda (p) (read-string!-evt (make-string 10) p)))
728+
(check (lambda (p) (read-line-evt p)))
729+
(check (lambda (p) (read-bytes-line-evt p)))
730+
(check (lambda (p) (peek-bytes-evt 10 0 #f p)))
731+
(check (lambda (p) (peek-bytes!-evt (make-bytes 10) 0 #f p)))
732+
(check (lambda (p) (peek-bytes!-evt (make-bytes 10) 0 (port-progress-evt p) p)))
733+
(check (lambda (p) (peek-bytes-avail!-evt (make-bytes 10) 0 #f p)))
734+
(check (lambda (p) (peek-bytes-avail!-evt (make-bytes 10) 0 (port-progress-evt p) p)))
735+
(check (lambda (p) (peek-string-evt 10 0 #f p)))
736+
(check (lambda (p) (peek-string-evt 10 0 (port-progress-evt p) p)))
737+
(check (lambda (p) (peek-string!-evt (make-string 10) 0 #f p)))
738+
(check (lambda (p) (peek-string!-evt (make-string 10) 0 (port-progress-evt p) p))))
739+
686740
;; ----------------------------------------
687741
;; Conversion wrappers
688742

‎racket/collects/racket/port.rkt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,17 +1138,21 @@
11381138
(nack-guard-evt
11391139
(lambda (nack)
11401140
(define ch (make-channel))
1141+
(define exn-ch (make-channel))
11411142
(define ready (make-semaphore))
11421143
(let ([t (thread (lambda ()
11431144
(parameterize-break #t
1144-
(with-handlers ([exn:break? void])
1145+
(with-handlers ([(lambda (exn) #t)
1146+
(lambda (exn)
1147+
(channel-put exn-ch exn))])
11451148
(semaphore-post ready)
11461149
(go nack ch #f)))))])
11471150
(thread (lambda ()
11481151
(sync nack)
11491152
(semaphore-wait ready)
11501153
(break-thread t))))
1151-
ch))))))
1154+
(choice-evt ch
1155+
(wrap-evt exn-ch (lambda (exn) (raise exn))))))))))
11521156

11531157
(define (read-at-least-bytes!-evt orig-bstr input-port need-more? shrink combo
11541158
peek-offset prog-evt)

0 commit comments

Comments
 (0)