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); }); }