This fixes the long-standing crash on close when nbdkit exits.
I did try first to fix threads so we're using a proper thread pool,
but that's difficult to implement. So this does the minimal change
needed to fix the crash instead.
There are still two segfaults that happen during running the test
suite. One is deliberately caused (tests/test-captive.sh). The other
appears to be an assertion failure bug in the new finalize code:
Thread 2 (Thread 0x7f12b820fa40 (LWP 231456)):
#0 0x00007f12b8783a1f in __GI___poll (fds=0x55771f18c550, nfds=nfds@entry=2,
timeout=timeout@entry=-1) at ../sysdeps/unix/sysv/linux/poll.c:29
#1 0x00005577156b9646 in poll (__timeout=-1, __nfds=2, __fds=<optimized out>) at
/usr/include/bits/poll2.h:46
#2 check_sockets_and_quit_fd (nr_socks=1, socks=0x55771f18c2e0) at sockets.c:466
#3 accept_incoming_connections (socks=0x55771f18c2e0, nr_socks=1) at sockets.c:494
#4 0x00005577156ac6ac in start_serving () at main.c:914
#5 main (argc=<optimized out>, argv=0x7ffd0bcb15d8) at main.c:685
Thread 1 (Thread 0x7f12b820e700 (LWP 231469)):
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x00007f12b86b28d9 in __GI_abort () at abort.c:79
#2 0x00007f12b86b27a9 in __assert_fail_base (fmt=0x7f12b881db18 "%s%s%s:%u:
%s%sAssertion `%s' failed.\n%n", assertion=0x5577156bb09d "h->state &
HANDLE_CONNECTED", file=0x5577156bb064 "backend.c", line=240,
function=<optimized out>) at assert.c:92
#3 0x00007f12b86c1a66 in __GI___assert_fail (assertion=assertion@entry=0x5577156bb09d
"h->state & HANDLE_CONNECTED", file=file@entry=0x5577156bb064
"backend.c", line=line@entry=240, function=function@entry=0x5577156bb880
<__PRETTY_FUNCTION__.6528> "backend_finalize") at assert.c:101
#4 0x00005577156ad04e in backend_finalize (b=<optimized out>,
conn=conn@entry=0x55771f1ac710) at backend.c:240
#5 0x00005577156b6ea8 in negotiate_handshake_newstyle_options (conn=<optimized
out>) at protocol-handshake-newstyle.c:484
#6 protocol_handshake_newstyle (conn=0x55771f1ac710) at
protocol-handshake-newstyle.c:762
#7 0x00005577156b5705 in protocol_handshake (conn=conn@entry=0x55771f1ac710) at
protocol-handshake.c:55
#8 0x00005577156af73a in handle_single_connection (sockin=<optimized out>,
sockout=15) at connections.c:167
#9 0x00005577156b8a48 in start_thread (datav=0x55771f18c620) at sockets.c:356
#10 0x00007f12b885f4e2 in start_thread (arg=<optimized out>) at
pthread_create.c:479
#11 0x00007f12b878e643 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
Rich.