Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ntcore] Fix crash on disconnect #5788

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ntcore/src/main/native/cpp/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ void NetworkClient3::TcpConnected(uv::Tcp& tcp) {
tcp.error.connect([this, &tcp](uv::Error err) {
DEBUG3("NT3 TCP error {}", err.str());
if (!tcp.IsLoopClosing()) {
DoDisconnect(err.str());
// we could be in the middle of sending data, so defer disconnect
uv::Timer::SingleShot(m_loop, uv::Timer::Time{0},
[this, reason = std::string{err.str()}] {
DoDisconnect(reason);
});
}
});
tcp.end.connect([this, &tcp] {
Expand Down Expand Up @@ -412,7 +416,10 @@ void NetworkClient::WsConnected(wpi::WebSocket& ws, uv::Tcp& tcp,
m_clientImpl->SendInitial();
ws.closed.connect([this, &ws](uint16_t, std::string_view reason) {
if (!ws.GetStream().IsLoopClosing()) {
DoDisconnect(reason);
// we could be in the middle of sending data, so defer disconnect
uv::Timer::SingleShot(
m_loop, uv::Timer::Time{0},
[this, reason = std::string{reason}] { DoDisconnect(reason); });
}
});
ws.text.connect([this](std::string_view data, bool) {
Expand Down
6 changes: 6 additions & 0 deletions ntcore/src/main/native/cpp/net/NetworkOutgoingQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,15 @@ void NetworkOutgoingQueue<MessageType>::SendOutgoing(uint64_t curTimeMs,
});
}
}
if (unsent < 0) {
return; // error
}
if (unsent == 0) {
// finish writing any partial buffers
unsent = m_wire.Flush();
if (unsent < 0) {
return; // error
}
}
int delta = it - msgs.begin() - unsent;
for (auto&& msg : std::span{msgs}.subspan(0, delta)) {
Expand Down
9 changes: 9 additions & 0 deletions ntcore/src/main/native/cpp/net/ServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ void ServerImpl::ClientData4::SendAnnounce(TopicData* topic,
WireEncodeAnnounce(os, topic->name, topic->id, topic->typeStr,
topic->properties, pubuid);
});
if (unsent < 0) {
return; // error
}
if (unsent == 0 && m_wire.Flush() == 0) {
return;
}
Expand All @@ -544,6 +547,9 @@ void ServerImpl::ClientData4::SendUnannounce(TopicData* topic) {
if (m_local) {
int unsent = m_wire.WriteText(
[&](auto& os) { WireEncodeUnannounce(os, topic->name, topic->id); });
if (unsent < 0) {
return; // error
}
if (unsent == 0 && m_wire.Flush() == 0) {
return;
}
Expand All @@ -565,6 +571,9 @@ void ServerImpl::ClientData4::SendPropertiesUpdate(TopicData* topic,
int unsent = m_wire.WriteText([&](auto& os) {
WireEncodePropertiesUpdate(os, topic->name, update, ack);
});
if (unsent < 0) {
return; // error
}
if (unsent == 0 && m_wire.Flush() == 0) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion ntcore/src/main/native/cpp/net/WebSocketConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ int WebSocketConnection::Flush() {
m_ws_frames.reserve(m_frames.size());
for (auto&& frame : m_frames) {
m_ws_frames.emplace_back(
frame.opcode, std::span{&m_bufs[frame.start], &m_bufs[frame.end]});
frame.opcode,
std::span{m_bufs}.subspan(frame.start, frame.end - frame.start));
}

auto unsentFrames = m_ws.TrySendFrames(
Expand Down
Loading