From e77de1804f4c229fb8fea3277c6f0c80c71243ae Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 21 May 2024 01:53:45 +0300 Subject: [PATCH] Kernel: Fix some race conditions in TCP stack Remove race condition if two acks are to be sent one after another. Always unblock semaphore once TCP thread has done something. This allows better chance of TCP sending to succeed. There are multiple places in the networking code that would require thread-safe entering to blocking mode. I should add some API for this so that a lot of race conditions could be removed. --- kernel/kernel/Networking/TCPSocket.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kernel/kernel/Networking/TCPSocket.cpp b/kernel/kernel/Networking/TCPSocket.cpp index b53736eb..a23bd4a3 100644 --- a/kernel/kernel/Networking/TCPSocket.cpp +++ b/kernel/kernel/Networking/TCPSocket.cpp @@ -438,10 +438,8 @@ namespace Kernel { LockGuard _(m_mutex); - if (m_should_ack) + if (m_should_ack.compare_exchange(true, false)) { - m_should_ack = false; - ASSERT(m_connection_info.has_value()); auto* target_address = reinterpret_cast(&m_connection_info->address); auto target_address_len = m_connection_info->address_len; @@ -510,6 +508,7 @@ namespace Kernel } } + m_semaphore.unblock(); m_semaphore.block_with_wake_time(current_ms + retransmit_timeout_ms); }