Wills, plumbers, and checking if a port is closed

35 views
Skip to first unread message

David Storrs

unread,
Jun 30, 2020, 6:37:38 PM6/30/20
to Racket Users
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?

Sorawee Porncharoenwase

unread,
Jun 30, 2020, 6:57:03 PM6/30/20
to David Storrs, Racket Users
It doesn't look like will executor will do what you want, since it has to do with garbage collection rather than port closing. 

This could be overkill, but it's possible to construct a custom port (https://docs.racket-lang.org/reference/customport.html). Is it possible to construct a new port that wraps your target port inside, and specify the `close` argument to do whatever you want to do?

On Tue, Jun 30, 2020 at 3:37 PM David Storrs <david....@gmail.com> 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?

--
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 racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKofeSmxqSs7RUL-PT0my-JcuW9atP2%2BDdj6o1o_hJVrsmw%40mail.gmail.com.

Matthew Flatt

unread,
Jun 30, 2020, 7:08:26 PM6/30/20
to David Storrs, Racket Users
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.

George Neuner

unread,
Jun 30, 2020, 7:18:13 PM6/30/20
to David Storrs, racket users
Ports are able to raise events.  I don't know if any of these are
directly useful to diagnose your early close problem, but you may be
able to cobble something using multiple events.

https://docs.racket-lang.org/reference/sync.html
https://docs.racket-lang.org/reference/port-lib.html?q=port#%28part._.Port_.Events%29


George

Ryan Culpepper

unread,
Jun 30, 2020, 7:21:27 PM6/30/20
to Matthew Flatt, David Storrs, Racket Users
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

--
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 racket-users...@googlegroups.com.

David Storrs

unread,
Jul 2, 2020, 6:55:04 AM7/2/20
to ry...@racket-lang.org, Matthew Flatt, Racket Users
Doh, just realized I never responded to this. Thank you guys so much. As always, it's really appreciated.
Reply all
Reply to author
Forward
0 new messages