unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Writing to closed network port causes guile to exit with error 141, not error message
@ 2024-03-22 14:22 Ryan Raymond
  2024-03-22 16:05 ` Tomas Volf
  0 siblings, 1 reply; 4+ messages in thread
From: Ryan Raymond @ 2024-03-22 14:22 UTC (permalink / raw)
  To: Guile User

Hello, all
I have a problem with my web-server.
If the network connection to the server is closed (by killing the client)
and one attempts to write data to that port in GUILE, the interpreter will
exit without an error message. echo "$?" says GUILE exited with error code
141 but google doesn't give any results for this.

This code is running in a separate thread so it shouldn't be causing the
main thread to die.
Wrapping the write in a try-catch doesn't work. Maybe a bug with the C?
It only happens on the second write though.

It is reproducible. Run this code as a guile script:

(define sock (socket PF_INET SOCK_STREAM 0))
(bind sock AF_INET INADDR_LOOPBACK 8081)
(listen sock 100)
(define c (car (accept sock)))
(sleep 5)
; Start curl and then cancel here
(display "I am a banana" c)
(display "Banana also, lol" c)
(display "I should run if this doesn't fail\n")

In another terminal run
curl localhost:8081 --http0.9

Make sure to cancel curl and then type echo $? to see the error code 141.
Any help would be welcome, thanks
Ryan


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

* Re: Writing to closed network port causes guile to exit with error 141, not error message
  2024-03-22 14:22 Writing to closed network port causes guile to exit with error 141, not error message Ryan Raymond
@ 2024-03-22 16:05 ` Tomas Volf
  2024-03-22 23:00   ` Ryan Raymond
  0 siblings, 1 reply; 4+ messages in thread
From: Tomas Volf @ 2024-03-22 16:05 UTC (permalink / raw)
  To: Ryan Raymond; +Cc: Guile User

[-- Attachment #1: Type: text/plain, Size: 7116 bytes --]

Hi,

On 2024-03-22 10:22:03 -0400, Ryan Raymond wrote:
> Hello, all
> I have a problem with my web-server.
> If the network connection to the server is closed (by killing the client)
> and one attempts to write data to that port in GUILE, the interpreter will
> exit without an error message. echo "$?" says GUILE exited with error code
> 141 but google doesn't give any results for this.
>
> This code is running in a separate thread so it shouldn't be causing the
> main thread to die.
> Wrapping the write in a try-catch doesn't work. Maybe a bug with the C?
> It only happens on the second write though.
>
> It is reproducible. Run this code as a guile script:
>
> (define sock (socket PF_INET SOCK_STREAM 0))
> (bind sock AF_INET INADDR_LOOPBACK 8081)
> (listen sock 100)
> (define c (car (accept sock)))
> (sleep 5)
> ; Start curl and then cancel here
> (display "I am a banana" c)
> (display "Banana also, lol" c)
> (display "I should run if this doesn't fail\n")
>
> In another terminal run
> curl localhost:8081 --http0.9

Thank you for nice and short reproduction steps :)

>
> Make sure to cancel curl and then type echo $? to see the error code 141.
> Any help would be welcome, thanks

This seems to be caused by SIGPIPE.  When running your repro under GDB:

Thread 1 "guile" received signal SIGPIPE, Broken pipe.
0x00007ffff7a150af in write () from /gnu/store/ln6hxqjvz6m9gdd9s97pivlqck7hzs99-glibc-2.35/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7a150af in write () from /gnu/store/ln6hxqjvz6m9gdd9s97pivlqck7hzs99-glibc-2.35/lib/libc.so.6
#1  0x00007ffff7ed286f in fport_write (port=<optimized out>, src=<optimized out>, start=<optimized out>, count=16) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/fports.c:623
#2  0x00007ffff7f0ef44 in scm_i_write_bytes (port=#<port #<port-type file 7ffff76c3b40> 7ffff779a160>, src="#<vu8vector>" = {...}, start=0, count=16)
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/ports.c:2858
#3  0x00007ffff7f10f01 in scm_c_put_latin1_chars (port=#<port #<port-type file 7ffff76c3b40> 7ffff779a160>, chars=<optimized out>, len=16)
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/ports.c:3458
#4  0x00007ffff7f166ef in iprin1 (exp="Banana also, lol", port=#<port #<port-type file 7ffff76c3b40> 7ffff779a160>, pstate=0x7ffff778c4d8)
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:687
#5  0x00007ffff7f1693a in scm_iprin1 (exp=<optimized out>, port=<optimized out>, pstate=<optimized out>) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:561
#6  0x00007ffff7f16b0d in scm_prin1 (exp="Banana also, lol", port=#<port #<port-type file 7ffff76c3b40> 7ffff779a160>, writingp=0)
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:840
#7  0x00007ffff7f16cbf in scm_display (obj="Banana also, lol", port=#<port #<port-type file 7ffff76c3b40> 7ffff779a160>)
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:1114
#8  0x00007ffff7f43390 in vm_regular_engine (thread=0x7ffff767dd80) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm-engine.c:972
#9  0x00007ffff7f50db5 in scm_call_n (proc=<optimized out>, argv=<optimized out>, nargs=1) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm.c:1615
#10 0x00007ffff7ebb977 in scm_primitive_eval (exp=<optimized out>, exp@entry=((@ (ice-9 control) %) (begin ((@@ (ice-9 command-line) load/lang) "/tmp/x.scm") (quit))))
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/eval.c:671
#11 0x00007ffff7ec1846 in scm_eval (exp=((@ (ice-9 control) %) (begin ((@@ (ice-9 command-line) load/lang) "/tmp/x.scm") (quit))), module_or_state="#<struct module>" = {...})
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/eval.c:705
#12 0x00007ffff7f203e6 in scm_shell (argc=3, argv=0x7fffffffda88) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/script.c:357
#13 0x00007ffff7ed28cc in invoke_main_func (body_data=0x7fffffffd920) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/init.c:312
#14 0x00007ffff7eb9e1a in c_body (d=0x7fffffffd840) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/continuations.c:430
#15 0x00007ffff7f43390 in vm_regular_engine (thread=0x7ffff767dd80) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm-engine.c:972
#16 0x00007ffff7f50db5 in scm_call_n (proc=<optimized out>, argv=<optimized out>, nargs=2) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm.c:1615
#17 0x00007ffff7ebb5ca in scm_call_2 (proc=<optimized out>, arg1=<optimized out>, arg2=<optimized out>) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/eval.c:503
#18 0x00007ffff7f62092 in scm_c_with_exception_handler.constprop.0 (type=#t, handler_data=handler_data@entry=0x7fffffffd7d0, thunk_data=thunk_data@entry=0x7fffffffd7d0,
    thunk=<optimized out>, handler=<optimized out>) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/exceptions.c:170
#19 0x00007ffff7f3de1f in scm_c_catch (tag=<optimized out>, body=<optimized out>, body_data=<optimized out>, handler=<optimized out>, handler_data=<optimized out>,
    pre_unwind_handler=<optimized out>, pre_unwind_handler_data=0x7ffff76fd500) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/throw.c:168
#20 0x00007ffff7ebc396 in scm_i_with_continuation_barrier (pre_unwind_handler=0x7ffff7ebc0b0 <pre_unwind_handler>, pre_unwind_handler_data=0x7ffff76fd500, handler_data=0x7fffffffd840,
    handler=0x7ffff7ec2ba0 <c_handler>, body_data=0x7fffffffd840, body=0x7ffff7eb9e10 <c_body>) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/continuations.c:368
#21 scm_c_with_continuation_barrier (func=<optimized out>, data=<optimized out>) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/continuations.c:464
#22 0x00007ffff7f3d049 in with_guile (base=0x7fffffffd8c8, data=0x7fffffffd8f0) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/threads.c:649
#23 0x00007ffff7e207fa in GC_call_with_stack_base () from /gnu/store/k1ha4n9v8d7myiiszvl2ic7xnb56l219-libgc-8.2.2/lib/libgc.so.1
#24 0x00007ffff7f373f8 in scm_i_with_guile (dynamic_state=<optimized out>, data=<optimized out>, func=<optimized out>) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/threads.c:692
#25 scm_with_guile (func=<optimized out>, data=<optimized out>) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/threads.c:698
#26 0x00007ffff7edb8e5 in scm_boot_guile (argc=argc@entry=3, argv=argv@entry=0x7fffffffda88, main_func=main_func@entry=0x401240 <inner_main>, closure=closure@entry=0x0)
    at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/init.c:295
#27 0x00000000004010f7 in main (argc=3, argv=0x7fffffffda88) at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/guile.c:94


If you check module/web/server/http.scm in the guile's repository, you will
notice there is this line:

    (sigaction SIGPIPE SIG_IGN)

Which caused the signal to be ignored instead of using the default handler
(which is to die).

Have a nice day,
Tomas Volf

--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: Writing to closed network port causes guile to exit with error 141, not error message
  2024-03-22 16:05 ` Tomas Volf
@ 2024-03-22 23:00   ` Ryan Raymond
  2024-03-23 12:09     ` Chris Vine
  0 siblings, 1 reply; 4+ messages in thread
From: Ryan Raymond @ 2024-03-22 23:00 UTC (permalink / raw)
  To: Ryan Raymond, Guile User

Thank you, Thomas.
I do wonder if this is good behavior to have in Guile. I feel like we
should replace that signal with an error. It's not good for programs to
exit without an explanation.

However, thank you for your help.
Ryan

On Fri, Mar 22, 2024 at 12:05 PM Tomas Volf <~@wolfsden.cz> wrote:

> Hi,
>
> On 2024-03-22 10:22:03 -0400, Ryan Raymond wrote:
> > Hello, all
> > I have a problem with my web-server.
> > If the network connection to the server is closed (by killing the client)
> > and one attempts to write data to that port in GUILE, the interpreter
> will
> > exit without an error message. echo "$?" says GUILE exited with error
> code
> > 141 but google doesn't give any results for this.
> >
> > This code is running in a separate thread so it shouldn't be causing the
> > main thread to die.
> > Wrapping the write in a try-catch doesn't work. Maybe a bug with the C?
> > It only happens on the second write though.
> >
> > It is reproducible. Run this code as a guile script:
> >
> > (define sock (socket PF_INET SOCK_STREAM 0))
> > (bind sock AF_INET INADDR_LOOPBACK 8081)
> > (listen sock 100)
> > (define c (car (accept sock)))
> > (sleep 5)
> > ; Start curl and then cancel here
> > (display "I am a banana" c)
> > (display "Banana also, lol" c)
> > (display "I should run if this doesn't fail\n")
> >
> > In another terminal run
> > curl localhost:8081 --http0.9
>
> Thank you for nice and short reproduction steps :)
>
> >
> > Make sure to cancel curl and then type echo $? to see the error code 141.
> > Any help would be welcome, thanks
>
> This seems to be caused by SIGPIPE.  When running your repro under GDB:
>
> Thread 1 "guile" received signal SIGPIPE, Broken pipe.
> 0x00007ffff7a150af in write () from
> /gnu/store/ln6hxqjvz6m9gdd9s97pivlqck7hzs99-glibc-2.35/lib/libc.so.6
> (gdb) bt
> #0  0x00007ffff7a150af in write () from
> /gnu/store/ln6hxqjvz6m9gdd9s97pivlqck7hzs99-glibc-2.35/lib/libc.so.6
> #1  0x00007ffff7ed286f in fport_write (port=<optimized out>,
> src=<optimized out>, start=<optimized out>, count=16) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/fports.c:623
> #2  0x00007ffff7f0ef44 in scm_i_write_bytes (port=#<port #<port-type file
> 7ffff76c3b40> 7ffff779a160>, src="#<vu8vector>" = {...}, start=0, count=16)
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/ports.c:2858
> #3  0x00007ffff7f10f01 in scm_c_put_latin1_chars (port=#<port #<port-type
> file 7ffff76c3b40> 7ffff779a160>, chars=<optimized out>, len=16)
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/ports.c:3458
> #4  0x00007ffff7f166ef in iprin1 (exp="Banana also, lol", port=#<port
> #<port-type file 7ffff76c3b40> 7ffff779a160>, pstate=0x7ffff778c4d8)
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:687
> #5  0x00007ffff7f1693a in scm_iprin1 (exp=<optimized out>, port=<optimized
> out>, pstate=<optimized out>) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:561
> #6  0x00007ffff7f16b0d in scm_prin1 (exp="Banana also, lol", port=#<port
> #<port-type file 7ffff76c3b40> 7ffff779a160>, writingp=0)
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:840
> #7  0x00007ffff7f16cbf in scm_display (obj="Banana also, lol", port=#<port
> #<port-type file 7ffff76c3b40> 7ffff779a160>)
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/print.c:1114
> #8  0x00007ffff7f43390 in vm_regular_engine (thread=0x7ffff767dd80) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm-engine.c:972
> #9  0x00007ffff7f50db5 in scm_call_n (proc=<optimized out>,
> argv=<optimized out>, nargs=1) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm.c:1615
> #10 0x00007ffff7ebb977 in scm_primitive_eval (exp=<optimized out>,
> exp@entry=((@ (ice-9 control) %) (begin ((@@ (ice-9 command-line)
> load/lang) "/tmp/x.scm") (quit))))
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/eval.c:671
> #11 0x00007ffff7ec1846 in scm_eval (exp=((@ (ice-9 control) %) (begin ((@@
> (ice-9 command-line) load/lang) "/tmp/x.scm") (quit))),
> module_or_state="#<struct module>" = {...})
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/eval.c:705
> #12 0x00007ffff7f203e6 in scm_shell (argc=3, argv=0x7fffffffda88) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/script.c:357
> #13 0x00007ffff7ed28cc in invoke_main_func (body_data=0x7fffffffd920) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/init.c:312
> #14 0x00007ffff7eb9e1a in c_body (d=0x7fffffffd840) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/continuations.c:430
> #15 0x00007ffff7f43390 in vm_regular_engine (thread=0x7ffff767dd80) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm-engine.c:972
> #16 0x00007ffff7f50db5 in scm_call_n (proc=<optimized out>,
> argv=<optimized out>, nargs=2) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/vm.c:1615
> #17 0x00007ffff7ebb5ca in scm_call_2 (proc=<optimized out>,
> arg1=<optimized out>, arg2=<optimized out>) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/eval.c:503
> #18 0x00007ffff7f62092 in scm_c_with_exception_handler.constprop.0
> (type=#t, handler_data=handler_data@entry=0x7fffffffd7d0,
> thunk_data=thunk_data@entry=0x7fffffffd7d0,
>     thunk=<optimized out>, handler=<optimized out>) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/exceptions.c:170
> #19 0x00007ffff7f3de1f in scm_c_catch (tag=<optimized out>,
> body=<optimized out>, body_data=<optimized out>, handler=<optimized out>,
> handler_data=<optimized out>,
>     pre_unwind_handler=<optimized out>,
> pre_unwind_handler_data=0x7ffff76fd500) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/throw.c:168
> #20 0x00007ffff7ebc396 in scm_i_with_continuation_barrier
> (pre_unwind_handler=0x7ffff7ebc0b0 <pre_unwind_handler>,
> pre_unwind_handler_data=0x7ffff76fd500, handler_data=0x7fffffffd840,
>     handler=0x7ffff7ec2ba0 <c_handler>, body_data=0x7fffffffd840,
> body=0x7ffff7eb9e10 <c_body>) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/continuations.c:368
> #21 scm_c_with_continuation_barrier (func=<optimized out>, data=<optimized
> out>) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/continuations.c:464
> #22 0x00007ffff7f3d049 in with_guile (base=0x7fffffffd8c8,
> data=0x7fffffffd8f0) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/threads.c:649
> #23 0x00007ffff7e207fa in GC_call_with_stack_base () from
> /gnu/store/k1ha4n9v8d7myiiszvl2ic7xnb56l219-libgc-8.2.2/lib/libgc.so.1
> #24 0x00007ffff7f373f8 in scm_i_with_guile (dynamic_state=<optimized out>,
> data=<optimized out>, func=<optimized out>) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/threads.c:692
> #25 scm_with_guile (func=<optimized out>, data=<optimized out>) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/threads.c:698
> #26 0x00007ffff7edb8e5 in scm_boot_guile (argc=argc@entry=3,
> argv=argv@entry=0x7fffffffda88, main_func=main_func@entry=0x401240
> <inner_main>, closure=closure@entry=0x0)
>     at /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/init.c:295
> #27 0x00000000004010f7 in main (argc=3, argv=0x7fffffffda88) at
> /tmp/guix-build-guile-3.0.9.drv-0/guile-3.0.9/libguile/guile.c:94
>
>
> If you check module/web/server/http.scm in the guile's repository, you will
> notice there is this line:
>
>     (sigaction SIGPIPE SIG_IGN)
>
> Which caused the signal to be ignored instead of using the default handler
> (which is to die).
>
> Have a nice day,
> Tomas Volf
>
> --
> There are only two hard things in Computer Science:
> cache invalidation, naming things and off-by-one errors.
>


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

* Re: Writing to closed network port causes guile to exit with error 141, not error message
  2024-03-22 23:00   ` Ryan Raymond
@ 2024-03-23 12:09     ` Chris Vine
  0 siblings, 0 replies; 4+ messages in thread
From: Chris Vine @ 2024-03-23 12:09 UTC (permalink / raw)
  To: Ryan Raymond; +Cc: Guile User

On Fri, 22 Mar 2024 19:00:43 -0400
Ryan Raymond <rjraymond@oakland.edu> wrote:
> Thank you, Thomas.
> I do wonder if this is good behavior to have in Guile. I feel like we
> should replace that signal with an error. It's not good for programs to
> exit without an explanation.
> 
> However, thank you for your help.
> Ryan

I suppose the justification for leaving things as they are is that it
is consistent with POSIX.  Amongst the first things that any POSIX
application writing to a socket has to do is to set SIG_IGN as the
handler for SIGPIPE so that writing to a closed socket causes errno to
be set to EPIPE instead of the write raising a SIGPIPE signal.

You could argue that the POSIX defaults are wrong: another socket
configuration which is often needed is to set SO_REUSEADDR on the
socket, which you would have thought should be the default.  The
argument for SIGPIPE is that it is necessary in order to obtain the
correct behaviour when piping at the unix shell: you do want a signal to
terminate the chain of pipes in that case if one is broken.  But how
often do you pipe executables at the unix shell writing to sockets?
Approximately never.

Chris



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

end of thread, other threads:[~2024-03-23 12:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-22 14:22 Writing to closed network port causes guile to exit with error 141, not error message Ryan Raymond
2024-03-22 16:05 ` Tomas Volf
2024-03-22 23:00   ` Ryan Raymond
2024-03-23 12:09     ` Chris Vine

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).