Fixed a data race when using UV loop data
Some checks are pending
C/C++ CI / build-alpine-static (map[arch:aarch64 branch:latest-stable flags:-ffunction-sections -Wno-error=inline -mfix-cortex-a53-835769 -mfix-cortex-a53-843419]) (push) Waiting to run
C/C++ CI / build-alpine-static (map[arch:riscv64 branch:edge flags:-ffunction-sections -Wno-error=inline]) (push) Waiting to run
C/C++ CI / build-alpine-static (map[arch:x86_64 branch:latest-stable flags:-ffunction-sections -Wno-error=inline]) (push) Waiting to run
C/C++ CI / build-ubuntu (map[c:gcc-11 cpp:g++-11 flags: os:ubuntu-20.04]) (push) Waiting to run
C/C++ CI / build-ubuntu (map[c:gcc-12 cpp:g++-12 flags: os:ubuntu-22.04]) (push) Waiting to run
C/C++ CI / build-ubuntu (map[c:gcc-8 cpp:g++-8 flags: os:ubuntu-20.04]) (push) Waiting to run
C/C++ CI / build-ubuntu-static-libs (map[flags:-fuse-linker-plugin -ffunction-sections -Wno-error=inline]) (push) Waiting to run
C/C++ CI / build-ubuntu-aarch64 (map[flags:-fuse-linker-plugin -ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 os:ubuntu-20.04]) (push) Waiting to run
C/C++ CI / build-ubuntu-aarch64 (map[flags:-fuse-linker-plugin -ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 os:ubuntu-22.04]) (push) Waiting to run
C/C++ CI / build-windows-msys2 (map[c:clang cxx:clang++ flags:-fuse-ld=lld -Wno-unused-command-line-argument -Wno-nan-infinity-disabled]) (push) Waiting to run
C/C++ CI / build-windows-msys2 (map[c:gcc cxx:g++ flags:-ffunction-sections -Wno-error=maybe-uninitialized -Wno-error=attributes]) (push) Waiting to run
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2019 rx:OFF tls:OFF upnp:OFF vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Waiting to run
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2019 rx:OFF tls:ON upnp:OFF vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Waiting to run
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2019 rx:OFF tls:ON upnp:ON vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Waiting to run
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2019 rx:ON tls:ON upnp:ON vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Waiting to run
C/C++ CI / build-windows-msbuild (map[grpc:ON os:2019 rx:ON tls:ON upnp:ON vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Waiting to run
cppcheck / cppcheck-ubuntu (push) Waiting to run
C/C++ CI / build-windows-msbuild (map[grpc:ON os:2022 rx:ON tls:ON upnp:ON vs:Visual Studio 17 2022 vspath:C:\Program Files\Microsoft Visual Studio\2022\Enterprise]) (push) Waiting to run
C/C++ CI / build-macos (push) Waiting to run
C/C++ CI / build-macos-aarch64 (push) Waiting to run
C/C++ CI / build-freebsd (map[architecture:x86-64 host:ubuntu-latest name:freebsd version:13.3]) (push) Waiting to run
C/C++ CI / build-openbsd (map[architecture:x86-64 host:ubuntu-latest name:openbsd version:7.4]) (push) Waiting to run
clang-tidy / clang-tidy (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Microsoft C++ Code Analysis / Analyze (push) Waiting to run
source-snapshot / source-snapshot (push) Waiting to run
Sync test (old) / sync-test-ubuntu-tsan (push) Waiting to run
Sync test (old) / sync-test-ubuntu-msan (push) Waiting to run
Sync test (old) / sync-test-ubuntu-ubsan (push) Waiting to run
Sync test (old) / sync-test-ubuntu-asan (push) Waiting to run
Sync test (old) / sync-test-macos (map[flags:-Og -ftrapv -target arm64-apple-macos-11 os:macos-14]) (push) Waiting to run
Sync test (old) / sync-test-macos (map[flags:-Og -ftrapv os:macos-13]) (push) Waiting to run
Sync test (old) / sync-test-windows-debug-asan (push) Waiting to run
Sync test (old) / sync-test-windows-leaks (push) Waiting to run
Sync test / sync-test-windows-leaks (push) Waiting to run
cppcheck / cppcheck-windows (push) Waiting to run
Sync test / sync-test-ubuntu-tsan (push) Waiting to run
Sync test / sync-test-ubuntu-msan (push) Waiting to run
Sync test / sync-test-ubuntu-ubsan (push) Waiting to run
Sync test / sync-test-ubuntu-asan (push) Waiting to run
Sync test / sync-test-macos (map[flags:-Og -ftrapv -target arm64-apple-macos-11 os:macos-14]) (push) Waiting to run
Sync test / sync-test-macos (map[flags:-Og -ftrapv os:macos-13]) (push) Waiting to run
Sync test / sync-test-windows-debug-asan (push) Waiting to run

This commit is contained in:
SChernykh 2024-10-10 14:46:15 +02:00
parent 210ae3f9df
commit d6c9c931fd
6 changed files with 26 additions and 12 deletions

View file

@ -76,7 +76,7 @@ private:
uv_close(reinterpret_cast<uv_handle_t*>(&client->m_shutdownAsync), nullptr); uv_close(reinterpret_cast<uv_handle_t*>(&client->m_shutdownAsync), nullptr);
delete GetLoopUserData(&client->m_loop, false); DeleteLoopUserData(&client->m_loop);
} }
void on_shutdown(); void on_shutdown();

View file

@ -769,9 +769,7 @@ void p2pool::on_stop(uv_async_t* async)
init_signals(pool, false); init_signals(pool, false);
uv_loop_t* loop = uv_default_loop_checked(); DeleteLoopUserData(uv_default_loop_checked());
delete GetLoopUserData(loop, false);
loop->data = nullptr;
} }
void p2pool::submit_block() const void p2pool::submit_block() const

View file

@ -121,7 +121,7 @@ private:
RandomX_Hasher_RPC* server = reinterpret_cast<RandomX_Hasher_RPC*>(async->data); RandomX_Hasher_RPC* server = reinterpret_cast<RandomX_Hasher_RPC*>(async->data);
uv_close(reinterpret_cast<uv_handle_t*>(&server->m_shutdownAsync), nullptr); uv_close(reinterpret_cast<uv_handle_t*>(&server->m_shutdownAsync), nullptr);
delete GetLoopUserData(&server->m_loop, false); DeleteLoopUserData(&server->m_loop);
} }
}; };

View file

@ -879,7 +879,7 @@ void TCPServer::on_shutdown(uv_async_t* async)
uv_close(reinterpret_cast<uv_handle_t*>(&s->m_dropConnectionsAsync), nullptr); uv_close(reinterpret_cast<uv_handle_t*>(&s->m_dropConnectionsAsync), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&s->m_shutdownAsync), nullptr); uv_close(reinterpret_cast<uv_handle_t*>(&s->m_shutdownAsync), nullptr);
delete GetLoopUserData(&s->m_loop, false); DeleteLoopUserData(&s->m_loop);
s->m_numHandles = 0; s->m_numHandles = 0;
uv_walk(&s->m_loop, [](uv_handle_t*, void* n) { (*reinterpret_cast<uint32_t*>(n))++; }, &s->m_numHandles); uv_walk(&s->m_loop, [](uv_handle_t*, void* n) { (*reinterpret_cast<uint32_t*>(n))++; }, &s->m_numHandles);

View file

@ -667,14 +667,30 @@ bool is_localhost(const std::string& host)
UV_LoopUserData* GetLoopUserData(uv_loop_t* loop, bool create) UV_LoopUserData* GetLoopUserData(uv_loop_t* loop, bool create)
{ {
UV_LoopUserData* data = reinterpret_cast<UV_LoopUserData*>(loop->data); static_assert(sizeof(std::atomic<UV_LoopUserData*>) <= sizeof(void*), "loop->data size mismatch");
static_assert(alignof(std::atomic<UV_LoopUserData*>) <= alignof(void*), "loop->data alignment mismatch");
if (!data && create) { std::atomic<UV_LoopUserData*>& data = reinterpret_cast<std::atomic<UV_LoopUserData*>&>(loop->data);
data = new UV_LoopUserData(loop);
loop->data = data; UV_LoopUserData* result = data.load();
if (!result && create) {
UV_LoopUserData* new_data = new UV_LoopUserData(loop);
if (data.compare_exchange_strong(result, new_data)) {
result = new_data;
} else {
delete new_data;
}
} }
return data; return result;
}
void DeleteLoopUserData(uv_loop_t* loop)
{
std::atomic<UV_LoopUserData*>& data = reinterpret_cast<std::atomic<UV_LoopUserData*>&>(loop->data);
delete data.exchange(nullptr);
} }
#ifdef WITH_UPNP #ifdef WITH_UPNP

View file

@ -94,7 +94,6 @@ struct UV_LoopUserData
~UV_LoopUserData() ~UV_LoopUserData()
{ {
m_loop->data = nullptr;
uv_mutex_destroy(&m_callbacksLock); uv_mutex_destroy(&m_callbacksLock);
uv_close(reinterpret_cast<uv_handle_t*>(m_async), [](uv_handle_t* h) { delete reinterpret_cast<uv_async_t*>(h); }); uv_close(reinterpret_cast<uv_handle_t*>(m_async), [](uv_handle_t* h) { delete reinterpret_cast<uv_async_t*>(h); });
for (const UV_LoopCallbackBase* cb : m_callbacks) { for (const UV_LoopCallbackBase* cb : m_callbacks) {
@ -123,6 +122,7 @@ struct UV_LoopUserData
}; };
UV_LoopUserData* GetLoopUserData(uv_loop_t* loop, bool create = true); UV_LoopUserData* GetLoopUserData(uv_loop_t* loop, bool create = true);
void DeleteLoopUserData(uv_loop_t* loop);
template<typename T> template<typename T>
bool CallOnLoop(uv_loop_t* loop, T&& callback) bool CallOnLoop(uv_loop_t* loop, T&& callback)