From d941be905e172b2851a2b2eb24fe718df2644aed Mon Sep 17 00:00:00 2001 From: Vasista Vovveti Date: Fri, 5 Jun 2026 15:01:44 -0700 Subject: [PATCH] [ntcore] NetworkClient: Add IsLoopClosing() guard in deferred disconnect timer (#8936) Co-authored-by: Claude Opus 4.8 --- ntcore/src/main/native/cpp/NetworkClient.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ntcore/src/main/native/cpp/NetworkClient.cpp b/ntcore/src/main/native/cpp/NetworkClient.cpp index 8f2f824505..f56dfb9159 100644 --- a/ntcore/src/main/native/cpp/NetworkClient.cpp +++ b/ntcore/src/main/native/cpp/NetworkClient.cpp @@ -285,6 +285,13 @@ void NetworkClient::WsConnected(wpi::net::WebSocket& ws, uv::Tcp& tcp, uv::Timer::SingleShot( m_loop, uv::Timer::Time{0}, [this, reason = std::string{reason}, keepws = ws.shared_from_this()] { + // Check that the loop is not shutting down. keepws->GetStream() + // would dereference the underlying uv::Tcp which may have been + // destroyed after Handle::Close() released its self-reference. + // m_loop is safe to access as long as `this` is alive. + if (m_loop.IsClosing()) { + return; + } DoDisconnect(reason); }); }