From 967ab75d8e83c152b4004720f8bb4cef1a8488e9 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 3 Aug 2023 17:08:03 -0400 Subject: [PATCH] network/protocols/unix: Fix UnixDatagramEndpoint::Close(). 1. Set fShutdownRead & fShutdownWrite. This is necessary because in Send(), there is a lock-unlock dance between the source and target sockets which could potentially race with some other thread closing the socket. The "shutdown" state is checked before actually writing, and so we need to set it here. Otherwise, the other thread could allocate a new receive FIFO for us, which we do not want. 2. Don't return early when unbinding, but only if there's an error (there shouldn't be.) 3. Merely ASSERT() in Free() that closing was already done and nothing remains to be deallocated. Should fix #18535. unix_dgram_test still passes. --- .../protocols/unix/UnixDatagramEndpoint.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/add-ons/kernel/network/protocols/unix/UnixDatagramEndpoint.cpp b/src/add-ons/kernel/network/protocols/unix/UnixDatagramEndpoint.cpp index ba63736450..fedc5eb10f 100644 --- a/src/add-ons/kernel/network/protocols/unix/UnixDatagramEndpoint.cpp +++ b/src/add-ons/kernel/network/protocols/unix/UnixDatagramEndpoint.cpp @@ -82,8 +82,13 @@ UnixDatagramEndpoint::Close() UnixDatagramEndpointLocker endpointLocker(this); - if (IsBound()) - RETURN_ERROR(UnixEndpoint::_Unbind()); + fShutdownRead = fShutdownWrite = true; + + if (IsBound()) { + status_t status = UnixEndpoint::_Unbind(); + if (status != B_OK) + RETURN_ERROR(status); + } _UnsetReceiveFifo(); @@ -99,9 +104,10 @@ UnixDatagramEndpoint::Free() UnixDatagramEndpointLocker endpointLocker(this); - _UnsetReceiveFifo(); + ASSERT(fReceiveFifo == NULL); + ASSERT(fTargetEndpoint == NULL); - RETURN_ERROR(_Disconnect()); + return B_OK; }