0

I'm using Openssl BIO_write() and BIO_read() to send and receive data.

Occasionally, after writing, I attempt to read but the process crashes due to a broken pipe:

Thread 5 received signal SIGPIPE, Broken pipe.
[Switching to Thread 0x7fff8212b640 (LWP 39357)]
__GI___libc_write (nbytes=24, buf=0x7fff472a7a63, fd=11) at ../sysdeps/unix/sysv/linux/write.c:26
(gdb) bt
#0  __GI___libc_write (nbytes=24, buf=0x7fff472a7a63, fd=11) at ../sysdeps/unix/sysv/linux/write.c:26
#1  __GI___libc_write (fd=11, buf=0x7fff472a7a63, nbytes=24) at ../sysdeps/unix/sysv/linux/write.c:24
#2  0x000055555574ef3e in conn_write ()
#3  0x000055555574b32a in bwrite_conv ()
#4  0x0000555555749aa7 in bio_write_intern ()
#5  0x000055555574998f in BIO_write ()
#6  0x0000555555716135 in tls_retry_write_records ()
#7  0x00005555556f0531 in ssl3_dispatch_alert ()
#8  0x000055555572205d in ossl_statem_fatal ()
#9  0x000055555570f01b in ossl_tls_handle_rlayer_return ()
#10 0x000055555570f2bf in ssl3_read_bytes ()
#11 0x00005555556ef23d in ssl3_read_internal ()
#12 0x00005555556ec7d9 in ssl_read ()
#13 0x000055555574988b in bio_read_intern ()
#14 0x000055555574976f in BIO_read ()

I know the cause (external) but I wish to handle it myself.

I would prefer the underlying ::recv() returns an error code via BIO_read().

The problem is, I thought I already instructed this at start of my int main():

signal(SIGPIPE, SIG_IGN);

This is set from my main thread, not the write/read thread, does this matter?

Shouldn't signal(SIGPIPE, SIG_IGN); handle this/what else can I do?

The code looks like this:

if(BIO_write(bio, buf, strlen(buf)) <= 0)
{
    return std::nullopt;
}

read = BIO_read(bio, (void*)&recv_buffer, num_bytes);

// I'd like to handle SIGPIPE here instead of process crashing
rare77
  • 297
  • 6
  • If you're using threads, you should be using `sigaction()`, not `signal()`. (Actually, you should always be using it)... – Shawn Apr 04 '23 at 03:51
  • @Shawn Thanks for the suggestion. I've just had a look at `sigaction` and the examples I found all create a static handler, which is no good to me as it's not near where `BIO_read()` is called. I just want `recv()` and therefore `BIO_read()` to return an error code so I can handle the problem "locally", not "globally", if this makes sense? – rare77 Apr 04 '23 at 08:15
  • Solution here: https://stackoverflow.com/a/24921005/21343992 – rare77 Apr 08 '23 at 00:14

0 Answers0