Skip to content

Commit

Permalink
Fixed client ejections causing segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmit799 committed Jan 8, 2024
1 parent 4e3a162 commit 4833554
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
10 changes: 9 additions & 1 deletion src/clientagent/client_participant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ ClientParticipant::ClientParticipant(
}

ClientParticipant::~ClientParticipant() {
NetworkClient::Shutdown();
// Call shutdown just in-case (most likely redundant.)
Shutdown();

_clientAgent->ParticipantLeft();
}
Expand All @@ -61,6 +62,13 @@ ClientParticipant::~ClientParticipant() {
* Manually disconnect and delete this client participant.
*/
void ClientParticipant::Shutdown() {
if (_disconnected) {
return;
}

// Kill the network connection.
NetworkClient::Shutdown();

// Stop the heartbeat timer (if we have one.)
if (_heartbeatTimer) {
_heartbeatTimer->stop();
Expand Down
12 changes: 11 additions & 1 deletion src/messagedirector/md_participant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ MDParticipant::MDParticipant(const std::shared_ptr<uvw::tcp_handle> &socket)
}

MDParticipant::~MDParticipant() {
NetworkClient::Shutdown();
// Call shutdown just in-case (most likely redundant.)
Shutdown();

MessageDirector::Instance()->ParticipantLeft();
}

/**
* Manually disconnect and delete this MD participant.
*/
void MDParticipant::Shutdown() {
if (_disconnected) {
return;
}

// Kill the network connection.
NetworkClient::Shutdown();

// Unsubscribe from all channels so post removes aren't accidently routed to us.
ChannelSubscriber::Shutdown();

Logger::Verbose(std::format("[MD] Routing {} post-remove(s) for '{}'",
Expand Down
4 changes: 2 additions & 2 deletions src/messagedirector/md_participant.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Ardos {

class MDParticipant : public NetworkClient, public ChannelSubscriber {
class MDParticipant final : public NetworkClient, public ChannelSubscriber {
public:
explicit MDParticipant(const std::shared_ptr<uvw::tcp_handle> &socket);
~MDParticipant();
~MDParticipant() override;

private:
void Shutdown() override;
Expand Down
22 changes: 12 additions & 10 deletions src/net/network_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ NetworkClient::NetworkClient(const std::shared_ptr<uvw::tcp_handle> &socket)

_socket->on<uvw::write_event>(
[this](const uvw::write_event &event, uvw::tcp_handle &) {
if (_disconnected && _socket != nullptr) {
if (_disconnected && !_socketClosed) {
_socket->close();
_socket.reset();

_socketClosed = true;
}

_isWriting = false;
Expand All @@ -61,15 +62,16 @@ bool NetworkClient::Disconnected() const { return _disconnected; }
uvw::socket_address NetworkClient::GetRemoteAddress() { return _remoteAddress; }

void NetworkClient::Shutdown() {
if (_socket == nullptr) {
if (_disconnected) {
return;
}

_disconnected = true;

if (!_isWriting) {
if (!_isWriting && !_socketClosed) {
_socket->close();
_socket.reset();

_socketClosed = true;
}
}

Expand All @@ -79,7 +81,7 @@ void NetworkClient::HandleClose(uv_errno_t code) {
return;
}

_disconnected = true;
_socketClosed = true;

HandleDisconnect(code);
}
Expand Down Expand Up @@ -139,14 +141,14 @@ void NetworkClient::SendDatagram(const std::shared_ptr<Datagram> &dg) {
return;
}

size_t sendSize = sizeof(uint16_t) + dg->Size();
const size_t sendSize = sizeof(uint16_t) + dg->Size();
auto sendBuffer = std::unique_ptr<char[]>(new char[sendSize]);

uint16_t dgSize = dg->Size();
const uint16_t dgSize = dg->Size();

auto sendPtr = &sendBuffer.get()[0];
const auto sendPtr = &sendBuffer.get()[0];
// Datagram size tag.
memcpy(sendPtr, (char *)&dgSize, sizeof(uint16_t));
memcpy(sendPtr, &dgSize, sizeof(uint16_t));
// Datagram data.
memcpy(sendPtr + sizeof(uint16_t), dg->GetData(), dg->Size());

Expand Down
5 changes: 4 additions & 1 deletion src/net/network_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class NetworkClient {
virtual void HandleClientDatagram(const std::shared_ptr<Datagram> &dg) = 0;
void SendDatagram(const std::shared_ptr<Datagram> &dg);

bool _disconnected = false;

private:
void HandleClose(uv_errno_t code);
void HandleData(const std::unique_ptr<char[]> &data, size_t size);
Expand All @@ -33,8 +35,9 @@ class NetworkClient {
std::shared_ptr<uvw::tcp_handle> _socket;
uvw::socket_address _remoteAddress;
std::vector<uint8_t> _data_buf;
bool _disconnected = false;

bool _isWriting = false;
bool _socketClosed = false;
};

} // namespace Ardos
Expand Down

0 comments on commit 4833554

Please sign in to comment.