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
617BST_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+ }
0 commit comments