unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* detecting broken pipe
@ 2006-04-24  4:23 Dan McMahill
  2006-04-25  0:03 ` Kevin Ryde
  0 siblings, 1 reply; 5+ messages in thread
From: Dan McMahill @ 2006-04-24  4:23 UTC (permalink / raw)


Hello,

I'm doing something like the following:

(use-modules (ice-9 popen))

(define pcb:pipe #f)

(define (pcb:launch-pcb)
   (set! pcb:pipe (open-output-pipe "pcb --listen"))
)

then later on I call pcb:launch-pcb and send stuff with

(display "some string" pcb:pipe)

This is all good right up until the "pcb --listen" command finishes 
(perhaps becuase it is a gui program and someone simply exited from it). 
  At this point the pipe is broken and the program which uses guile crashes.

So is there an easy way to detect when the pipe was terminated so I can 
avoid crashing?

Thanks
-Dan


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: detecting broken pipe
  2006-04-24  4:23 detecting broken pipe Dan McMahill
@ 2006-04-25  0:03 ` Kevin Ryde
  2006-04-25  0:44   ` Dan McMahill
  0 siblings, 1 reply; 5+ messages in thread
From: Kevin Ryde @ 2006-04-25  0:03 UTC (permalink / raw)
  Cc: guile-user

Dan McMahill <mcmahill@mtl.mit.edu> writes:
>
> So is there an easy way to detect when the pipe was terminated so I
> can avoid crashing?

You can set the SIGPIPE signal to avoid terminating on a broken pipe.
Usually setting it to SIG_IGN is the cleanest, with that a write
throws an EPIPE error.  (Read more in the libc manual.)

As to actually detecting the termination, SIGCHLD might tell you as
soon as the child dies (though you probably have to be careful not to
interfere with the cleanups close-pipe will do).


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: detecting broken pipe
  2006-04-25  0:03 ` Kevin Ryde
@ 2006-04-25  0:44   ` Dan McMahill
  2006-04-25  1:19     ` Kevin Ryde
  0 siblings, 1 reply; 5+ messages in thread
From: Dan McMahill @ 2006-04-25  0:44 UTC (permalink / raw)
  Cc: guile-user

Kevin Ryde wrote:
> Dan McMahill <mcmahill@mtl.mit.edu> writes:
> 
>>So is there an easy way to detect when the pipe was terminated so I
>>can avoid crashing?
> 
> 
> You can set the SIGPIPE signal to avoid terminating on a broken pipe.
> Usually setting it to SIG_IGN is the cleanest, with that a write
> throws an EPIPE error.  (Read more in the libc manual.)
> 
> As to actually detecting the termination, SIGCHLD might tell you as
> soon as the child dies (though you probably have to be careful not to
> interfere with the cleanups close-pipe will do).
> 

I tried this:

(sigaction SIGPIPE SIG_IGN)

before

(set! pcb:pipe (open-output-pipe "pcb --listen"))

but when I exit out of pcb, I still get

ERROR: In procedure write_all:
ERROR: Broken pipe

For various reasons I prefer to do whatever magic needs doing to deal 
with this completely in scheme rather than in the c program which uses 
guile.

Thanks
-Dan


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: detecting broken pipe
  2006-04-25  0:44   ` Dan McMahill
@ 2006-04-25  1:19     ` Kevin Ryde
  2006-04-25  2:10       ` Dan McMahill
  0 siblings, 1 reply; 5+ messages in thread
From: Kevin Ryde @ 2006-04-25  1:19 UTC (permalink / raw)
  Cc: guile-user

Dan McMahill <mcmahill@mtl.mit.edu> writes:
>
> ERROR: In procedure write_all:
> ERROR: Broken pipe

Yes, that's right, you get a scheme-level error instead of sigpipe
terminating the whole process.  (See `catch' in the guile manual for
trapping that error, or perhaps false-if-exception to ignore it.)


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: detecting broken pipe
  2006-04-25  1:19     ` Kevin Ryde
@ 2006-04-25  2:10       ` Dan McMahill
  0 siblings, 0 replies; 5+ messages in thread
From: Dan McMahill @ 2006-04-25  2:10 UTC (permalink / raw)
  Cc: guile-user

Kevin Ryde wrote:
> Dan McMahill <mcmahill@mtl.mit.edu> writes:
> 
>>ERROR: In procedure write_all:
>>ERROR: Broken pipe
> 
> 
> Yes, that's right, you get a scheme-level error instead of sigpipe
> terminating the whole process.  (See `catch' in the guile manual for
> trapping that error, or perhaps false-if-exception to ignore it.)
> 

sweet!  It seems to be working now.  I'm posting what I ended up with so 
the next lost soul searching the archives can find it.  Thanks so much 
for the help, I really appreciate it.

-Dan

;; Use this instead of
;; (display val pcb:pipe)
;;
(define (pcb:pipe-write val)
   (if pcb:pipe

       ;; pipe is open so try and write out our value
       (begin
         (catch #t

                ;; try to write our value
                (lambda ()
                  (display val pcb:pipe)
                  )

                ;; and if we fail spit out a message
                ;; and set pcb:pipe to false so we don't
                ;; try and write again
                (lambda (key . args)
                  (display "It appears that PCB has terminated.\n")
                  (display "If this is not the case, you should save and 
exit and\n")
                  (display "report this as a bug.\n\n")
                  (display "If you exited PCB on purpose, you can ignore 
this message\n\n")
                  (set! pcb:pipe #f)
                  )

                )
         )

       ;; pipe is not open so don't try and write to it
       ;;(display "pcb:pipe is not open\n")

       )

)


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-04-25  2:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-24  4:23 detecting broken pipe Dan McMahill
2006-04-25  0:03 ` Kevin Ryde
2006-04-25  0:44   ` Dan McMahill
2006-04-25  1:19     ` Kevin Ryde
2006-04-25  2:10       ` Dan McMahill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).