Do not require that no calls are made post-disconnect_socket
authorMatt Corallo <git@bluematt.me>
Thu, 17 Jun 2021 22:30:09 +0000 (22:30 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 21 Jun 2021 20:25:40 +0000 (20:25 +0000)
commit4703d4e72565ddfd150b9368ea036f4973fd7590
tree9db386b978acc97d8bdb81fb7cd17beeb9458aba
parent2f6205bfb0ea500e7d622971be4a2ebca2791099
Do not require that no calls are made post-disconnect_socket

The only practical way to meet this requirement is to block
disconnect_socket until any pending events are fully processed,
leading to this trivial deadlock:

 * Thread 1: select() woken up due to a read event
 * Thread 2: Event processing causes a disconnect_socket call to
             fire while the PeerManager lock is held.
 * Thread 2: disconnect_socket blocks until the read event in
             thread 1 completes.
 * Thread 1: bytes are read from the socket and
             PeerManager::read_event is called, waiting on the lock
             still held by thread 2.

There isn't a trivial way to address this deadlock without simply
making the final read_event call return immediately, which we do
here. This also implies that users can freely call event methods
after disconnect_socket, but only so far as the socket descriptor
is different from any later socket descriptor (ie until the file
descriptor is re-used).
lightning/src/ln/peer_handler.rs