Re: [racket-users] Wills, plumbers, and checking if a port is closed

2020-07-02 Thread David Storrs
Doh, just realized I never responded to this. Thank you guys so much. As
always, it's really appreciated.

On Tue, Jun 30, 2020, 7:21 PM Ryan Culpepper  wrote:

> 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) ;; => #
>   (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  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 racket-users+unsubscr...@googlegroups.com.
>> 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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKoe%3D5rVWODw848kta9dfz-KGNgTeP5UMCPTPSso0WhwcTw%40mail.gmail.com.


Re: [racket-users] Wills, plumbers, and checking if a port is closed

2020-06-30 Thread Ryan Culpepper
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) ;; => #
  (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  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 racket-users+unsubscr...@googlegroups.com.
> 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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CANy33qn%3Da6gWS0geiYWCd4W%3Djo5mVPSgxye6b_MxvGkzZ1N5Cw%40mail.gmail.com.


Re: [racket-users] Wills, plumbers, and checking if a port is closed

2020-06-30 Thread George Neuner



On 6/30/2020 4:27 PM, 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?


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

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/47967210-b87b-7a94-a9e2-035a8159e380%40comcast.net.


Re: [racket-users] Wills, plumbers, and checking if a port is closed

2020-06-30 Thread Matthew Flatt
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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/20200630164403.17c%40sirmail.smtp.cs.utah.edu.


Re: [racket-users] Wills, plumbers, and checking if a port is closed

2020-06-30 Thread Sorawee Porncharoenwase
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  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+unsubscr...@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
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegueQkjYUq1NTcyEUv-XMhdAYrbh4o0PVtxqs9BEWOgUoA%40mail.gmail.com.