fix potential race in socket closure

This commit is contained in:
Martin Michelsen
2025-10-04 09:54:21 -07:00
parent d61cb1106d
commit 2534ff37de
4 changed files with 21 additions and 11 deletions
+3 -3
View File
@@ -335,7 +335,7 @@ asio::awaitable<WebSocketMessage> HTTPClient::recv_websocket_message(size_t max_
} else if (opcode == 0x08) {
// Close message
co_await this->send_websocket_message(msg.data, msg.opcode);
this->r.get_socket().close();
this->r.close();
} else if (opcode == 0x09) {
// Ping message
@@ -343,7 +343,7 @@ asio::awaitable<WebSocketMessage> HTTPClient::recv_websocket_message(size_t max_
} else {
// Unknown control message type
this->r.get_socket().close();
this->r.close();
}
continue;
}
@@ -351,7 +351,7 @@ asio::awaitable<WebSocketMessage> HTTPClient::recv_websocket_message(size_t max_
// If there's an existing fragment, the current message's opcode should be
// zero; if there's no pending message, it must not be zero
if (prev_msg_present == (opcode != 0)) {
this->r.get_socket().close();
this->r.close();
continue;
}
+7 -1
View File
@@ -189,8 +189,14 @@ public:
return this->sock;
}
inline bool is_open() const {
return this->sock.is_open();
}
inline void close() {
this->sock.close();
if (this->sock.is_open()) {
this->sock.close();
}
}
private:
+3 -1
View File
@@ -118,7 +118,9 @@ vector<shared_ptr<Client>> GameServer::get_clients_by_identifier(const string& i
shared_ptr<Client> GameServer::create_client(shared_ptr<GameServerSocket> listen_sock, asio::ip::tcp::socket&& client_sock) {
uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address());
if (this->state->banned_ipv4_ranges->check(addr)) {
client_sock.close();
if (client_sock.is_open()) {
client_sock.close();
}
return nullptr;
}
+8 -6
View File
@@ -154,7 +154,9 @@ void IPSSClient::reschedule_idle_timeout() {
this->idle_timeout_timer.async_wait([this, sim](std::error_code ec) {
if (!ec) {
sim->log.info_f("Idle timeout expired on N-{:X}", this->network_id);
this->sock.close();
if (this->sock.is_open()) {
this->sock.close();
}
}
});
}
@@ -1233,11 +1235,9 @@ asio::awaitable<void> IPStackSimulator::on_client_tcp_frame(shared_ptr<IPSSClien
// Send the new data to the server
if (!conn->server_channel) {
this->log.warning_f("Client sent data on TCP connection {}, but server channel is missing",
conn_str);
this->log.warning_f("Client sent data on TCP connection {}, but server channel is missing", conn_str);
} else if (!conn->server_channel->connected()) {
this->log.warning_f("Client sent data on TCP connection {}, but server channel is disconnected",
conn_str);
this->log.warning_f("Client sent data on TCP connection {}, but server channel is disconnected", conn_str);
} else {
conn->server_channel->add_inbound_data(payload, payload_size);
}
@@ -1425,7 +1425,9 @@ std::shared_ptr<IPSSClient> IPStackSimulator::create_client(
std::shared_ptr<IPSSSocket> listen_sock, asio::ip::tcp::socket&& client_sock) {
uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address());
if (this->state->banned_ipv4_ranges->check(addr)) {
client_sock.close();
if (client_sock.is_open()) {
client_sock.close();
}
return nullptr;
}