Skip to content

Commit f97ec6a

Browse files
authored
Merge pull request #564 from garethsb/bugfix/ws-listener-close-race
Stop the websocket_listener listening on its own thread
2 parents 81b2d6d + 7bf31ec commit f97ec6a

2 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎Development/cpprest/test/ws_listener_test.cpp‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
// The first "test" is of course whether the header compiles standalone
22
#include "cpprest/ws_listener.h"
33

4+
#include <atomic>
5+
#include <chrono>
6+
#include <thread>
7+
#include <vector>
8+
#include <boost/version.hpp>
9+
#if BOOST_VERSION >= 106600
10+
#include <boost/asio/io_context.hpp>
11+
#else
12+
#include <boost/asio/io_service.hpp>
13+
#endif
14+
#include <boost/asio/ip/tcp.hpp>
415
#include "bst/test/test.h"
516

617
BST_TEST_CASE(testWebSocketListenerCloseOpen)
@@ -27,3 +38,80 @@ BST_TEST_CASE(testWebSocketListenerCloseOpen)
2738
// hmm, ran out of dynamic ports?!
2839
BST_REQUIRE(false);
2940
}
41+
42+
////////////////////////////////////////////////////////////////////////////////////////////
43+
BST_TEST_CASE(testWebSocketListenerCloseWhileAccepting)
44+
{
45+
// closing the listener while clients are connecting must not crash
46+
// see https://github.com/sony/nmos-cpp/issues/562
47+
for (auto port = 49152; port <= 65535; ++port)
48+
{
49+
web::websockets::experimental::listener::websocket_listener ws(web::uri_builder(U("ws://127.0.0.1")).set_port(port).to_uri());
50+
try
51+
{
52+
ws.open().wait();
53+
}
54+
catch (const web::websockets::websocket_exception&)
55+
{
56+
// could well be that port is already in use, so just try the next one
57+
continue;
58+
}
59+
60+
// each connection completes an accept on the listener thread, which then starts the next accept
61+
std::atomic<bool> done{ false };
62+
const boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), (unsigned short)port);
63+
std::vector<std::thread> clients;
64+
for (int i = 0; i < 4; ++i)
65+
{
66+
clients.emplace_back([&]
67+
{
68+
#if BOOST_VERSION >= 106600
69+
boost::asio::io_context service;
70+
#else
71+
boost::asio::io_service service;
72+
#endif
73+
while (!done)
74+
{
75+
boost::asio::ip::tcp::socket socket(service);
76+
boost::system::error_code ec;
77+
// the listener port may be in the ephemeral port range, and a client given it as its local port
78+
// connects to itself while the listener is closed, which stops the listener reopening
79+
socket.open(endpoint.protocol(), ec);
80+
socket.bind({ endpoint.address(), 0 }, ec);
81+
if (!ec && socket.local_endpoint(ec) != endpoint)
82+
{
83+
socket.connect(endpoint, ec);
84+
}
85+
// close abortively, so that TIME_WAIT sockets don't use up the ephemeral port range
86+
socket.set_option(boost::asio::socket_base::linger(true, 0), ec);
87+
socket.close(ec);
88+
}
89+
});
90+
}
91+
92+
try
93+
{
94+
const auto until = std::chrono::steady_clock::now() + std::chrono::seconds(2);
95+
for (int cycle = 0; std::chrono::steady_clock::now() < until; ++cycle)
96+
{
97+
// how long the listener needs to be open for a connection to be accepted varies by machine
98+
std::this_thread::sleep_for(std::chrono::microseconds(100 * (cycle % 5)));
99+
ws.close().wait();
100+
ws.open().wait();
101+
}
102+
ws.close().wait();
103+
}
104+
catch (...)
105+
{
106+
done = true;
107+
for (auto& client : clients) client.join();
108+
throw;
109+
}
110+
111+
done = true;
112+
for (auto& client : clients) client.join();
113+
return;
114+
}
115+
// hmm, ran out of dynamic ports?!
116+
BST_REQUIRE(false);
117+
}

‎Development/cpprest/ws_listener_impl.cpp‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cpprest/ws_listener.h"
22

3+
#include <future>
34
#include <mutex>
45
#include <set>
56
#include "detail/pragma_warnings.h"
@@ -429,9 +430,34 @@ namespace web
429430
// Unless there is specific problem I believe that this is the currently expected behavior."
430431
// See https://github.com/zaphoyd/websocketpp/issues/556#issuecomment-221563661
431432

432-
if (server.is_listening())
433+
// the accept loop runs on the listener thread, and Asio objects are not thread-safe,
434+
// so stop listening on that thread, which keeps running until stop_perpetual below
435+
if (thread.joinable())
433436
{
434-
server.stop_listening();
437+
std::promise<websocketpp::lib::error_code> stopped;
438+
const auto stop_listening = [&]
439+
{
440+
try
441+
{
442+
websocketpp::lib::error_code ec;
443+
if (server.is_listening())
444+
{
445+
server.stop_listening(ec);
446+
}
447+
stopped.set_value(ec);
448+
}
449+
catch (...)
450+
{
451+
stopped.set_exception(std::current_exception());
452+
}
453+
};
454+
#if BOOST_VERSION >= 106600
455+
websocketpp::lib::asio::post(server.get_io_service(), stop_listening);
456+
#else
457+
server.get_io_service().post(stop_listening);
458+
#endif
459+
const auto ec = stopped.get_future().get();
460+
if (ec) throw websocketpp::exception(ec);
435461
}
436462

437463
connections_t cons;

0 commit comments

Comments
 (0)