diff --git a/src/WebSocketContext.h b/src/WebSocketContext.h index d8b3fd035..a4120afde 100644 --- a/src/WebSocketContext.h +++ b/src/WebSocketContext.h @@ -184,6 +184,10 @@ struct WebSocketContext { } else { if (opCode == PING) { webSocket->send(std::string_view(data, length), (OpCode) OpCode::PONG); + /* The dropped handler may have ended the socket */ + if (us_socket_is_closed(SSL, (us_socket_t *) s) || webSocketData->isShuttingDown) { + return true; + } if (webSocketContextData->pingHandler) { webSocketContextData->pingHandler(webSocket, {data, length}); if (us_socket_is_closed(SSL, (us_socket_t *) s) || webSocketData->isShuttingDown) { @@ -213,6 +217,10 @@ struct WebSocketContext { } else { if (opCode == PING) { webSocket->send(std::string_view(controlBuffer, webSocketData->controlTipLength), (OpCode) OpCode::PONG); + /* The dropped handler may have ended the socket */ + if (us_socket_is_closed(SSL, (us_socket_t *) s) || webSocketData->isShuttingDown) { + return true; + } if (webSocketContextData->pingHandler) { webSocketContextData->pingHandler(webSocket, std::string_view(controlBuffer, webSocketData->controlTipLength)); if (us_socket_is_closed(SSL, (us_socket_t *) s) || webSocketData->isShuttingDown) { diff --git a/tests/Makefile b/tests/Makefile index 4e4338b70..947d64438 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -16,6 +16,8 @@ default: $(MAKE) -C ../uSockets $(CXX) -std=c++17 -fsanitize=address -flto=auto -I../src -I../uSockets/src HttpResponse.cpp ../uSockets/*.o -lz -pthread -o HttpResponse ./HttpResponse + $(CXX) -std=c++17 -fsanitize=address -flto=auto -I../src -I../uSockets/src WebSocketDropped.cpp ../uSockets/*.o -lz -pthread -o WebSocketDropped + ./WebSocketDropped performance: $(CXX) -std=c++17 HttpRouter.cpp -O3 -o HttpRouter diff --git a/tests/WebSocketDropped.cpp b/tests/WebSocketDropped.cpp new file mode 100755 index 000000000..9c0a64612 --- /dev/null +++ b/tests/WebSocketDropped.cpp @@ -0,0 +1,109 @@ +#include "../src/App.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +/* A dropped handler that ends the socket, with a client flooding PINGs and never reading. + * The PONG is dropped, end() emits close, and no handler may be called after that. */ + +struct PerSocketData {}; + +static std::atomic closes{0}; +static std::atomic droppedAfterClose{0}; + +static void flood(int port, uWS::Loop *loop, us_listen_socket_t **listenSocket) { + int fd = (int) ::socket(AF_INET, SOCK_STREAM, 0); + int small = 4096; + setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &small, sizeof(small)); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons((uint16_t) port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (::connect(fd, (sockaddr *) &addr, sizeof(addr)) != 0) { + std::cerr << "connect failed" << std::endl; + std::abort(); + } + + std::string upgrade = "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n"; + ::send(fd, upgrade.data(), upgrade.size(), 0); + std::string head; + char c; + while (head.find("\r\n\r\n") == std::string::npos && ::recv(fd, &c, 1, 0) == 1) { + head += c; + } + + /* Masked PINGs with a 125 byte payload, sent until the server stops taking them */ + std::string pings; + for (int i = 0; i < 4096; i++) { + pings += (char) 0x89; + pings += (char) (0x80 | 125); + pings += std::string(4, '\0'); + pings += std::string(125, 'p'); + } + timeval tv{}; + tv.tv_sec = 1; + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + for (int i = 0; i < 200 && !closes; i++) { + if (::send(fd, pings.data(), pings.size(), 0) <= 0) { + break; + } + } + /* Let the server go through what it has already read */ + usleep(200000); + ::close(fd); + + loop->defer([listenSocket]() { + us_listen_socket_close(0, *listenSocket); + }); +} + +int main() { + /* the client writes after the server ended the connection */ + signal(SIGPIPE, SIG_IGN); + us_listen_socket_t *listenSocket = nullptr; + int port = 0; + + uWS::App app; + app.ws("/*", { + .maxBackpressure = 16 * 1024, + .dropped = [](auto *ws, std::string_view, uWS::OpCode) { + if (closes) { + droppedAfterClose++; + } + ws->end(1008); + }, + .close = [](auto *, int, std::string_view) { + closes++; + } + }).listen(0, [&](auto *token) { + listenSocket = token; + port = us_socket_local_port(0, (us_socket_t *) token); + }); + + if (!listenSocket) { + std::cerr << "listen failed" << std::endl; + return 1; + } + + std::thread client(flood, port, uWS::Loop::get(), &listenSocket); + app.run(); + client.join(); + + if (closes != 1 || droppedAfterClose) { + std::cerr << "closes: " << closes << ", dropped after close: " << droppedAfterClose << std::endl; + return 1; + } + std::cout << "ALL PASS" << std::endl; + return 0; +}