Here's a function that creates a thread that waits until a port is closed
and then prints a message:
(define (watch-for-port-close p)
(thread (lambda () (sync (port-closed-evt out)) (eprintf "port
closed\n"))))
For example:
(define out (open-output-string))
(watch-for-port-close out) ;; => #<thread>
(close-output-port out) ;; prints "port closed"
One reason a port can get closed is because of a custodian shutdown, and in
that case you'd need to make sure that the watcher thread and its
current-error-port are not managed by the same custodian. Here's a version
that creates an unkillable thread (generally a bad idea, but probably okay
for debugging):
(require ffi/unsafe/custodian)
(define (watch-for-port-close p)
(parameterize ((current-custodian (make-custodian-at-root)))
(thread (lambda () (sync (port-closed-evt out)) (eprintf "port
closed\n")))))
Ryan
On Wed, Jul 1, 2020 at 1:08 AM Matthew Flatt <[email protected]> wrote:
> At Tue, 30 Jun 2020 16:27:56 -0400, David Storrs wrote:
> > I have a port that (my current theory says) is being closed when it
> > shouldn't, but I'm having trouble isolating exactly where and when. I
> > thought maybe I could do something Rackety to say "as soon as this port
> > gets closed, run this function". I went digging through Wills and
> Plumbers
> > but I'm having trouble grokking it. Am I headed in the right direction,
> or
> > is there a better way?
>
> Wills and plumbers will not help.
>
> Do you have control over where the port is used to that you can
> substitute another port? In that case, you could wrap the port with
> `make-input-port` or `make-output-port`, and then you have control over
> the close method.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/20200630164403.17c%40sirmail.smtp.cs.utah.edu
> .
>
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/CANy33qn%3Da6gWS0geiYWCd4W%3Djo5mVPSgxye6b_MxvGkzZ1N5Cw%40mail.gmail.com.