[net] Ensure we don't spin loop by keeping the poll write bit set
authorMatt Corallo <git@bluematt.me>
Sun, 3 Oct 2021 21:41:18 +0000 (21:41 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 16 Oct 2021 02:30:00 +0000 (02:30 +0000)
Previously we'd not been unsetting POLLOUT, which will result in
us spin-loop'ing with ChannelManager_write_buffer_space_avail.

Further, we'd also not been unsetting the POLLIN bit, which would
result in us never pausing read as the SocketDescriptor API
requires us to.

ldk-net/ldk_net.c

index dc19e8ee6794a900b1a2a3939940cd333c6d8a85..f404ddd973d107477968011893a439e5c5c8e997 100644 (file)
@@ -111,10 +111,9 @@ static uintptr_t sock_send_data(void* desc, struct LDKu8slice data, bool resume_
                for (int i = 0; i < descriptor->handler->sockcount; i++) {
                        if (descriptor->handler->pollfds[i].fd == descriptor->fd) {
                                if (pause_read) {
-                                       descriptor->handler->pollfds[i].events &= POLLIN;
-                                       descriptor->handler->pollfds[i].events |= POLLOUT;
+                                       descriptor->handler->pollfds[i].events = POLLOUT;
                                } else {
-                                       descriptor->handler->pollfds[i].events |= POLLIN;
+                                       descriptor->handler->pollfds[i].events = POLLIN;
                                }
                                break;
                        }
@@ -243,8 +242,7 @@ static void *sock_thread_fn(void* arg) {
                                                                        lockres = pthread_mutex_lock(&handler->sockets_mutex);
                                                                        assert(lockres == 0);
                                                                        assert(handler->pollfds[i - 1].fd == pollfds[i].fd); // Only we change fd order!
-                                                                       handler->pollfds[i - 1].events &= POLLIN;
-                                                                       handler->pollfds[i - 1].events |= POLLOUT;
+                                                                       handler->pollfds[i - 1].events = POLLOUT;
                                                                        lockres = pthread_mutex_unlock(&handler->sockets_mutex);
                                                                        assert(lockres == 0);
                                                                }