Skip to content

Commit 8ed67a6

Browse files
committed
refactor(websocket): clarify executor ownership and detach session runtime
1 parent f13bcf6 commit 8ed67a6

12 files changed

Lines changed: 236 additions & 115 deletions

‎include/vix/websocket/App.hpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ namespace vix::websocket
5555
* @brief Construct a WebSocket app.
5656
*
5757
* @param configPath Path to the configuration file.
58-
* @param executor Shared runtime executor used by the WebSocket stack.
58+
* @param executor Borrowed shared runtime executor used by the WebSocket
59+
* stack. The caller retains executor lifecycle authority.
5960
*/
6061
App(
6162
const std::string &configPath,
@@ -83,9 +84,9 @@ namespace vix::websocket
8384
void run_blocking();
8485

8586
/**
86-
* @brief Stop the WebSocket app and shared executor.
87+
* @brief Stop WebSocket-owned server resources.
8788
*
88-
* Safe to call multiple times.
89+
* Safe to call multiple times. This does not stop the borrowed executor.
8990
*/
9091
void stop() noexcept;
9192

@@ -112,7 +113,7 @@ namespace vix::websocket
112113
/**
113114
* @brief Access the executor used by the app.
114115
*
115-
* @return Shared runtime executor.
116+
* @return Borrowed shared runtime executor.
116117
*/
117118
[[nodiscard]] std::shared_ptr<vix::executor::RuntimeExecutor> executor() noexcept
118119
{

‎include/vix/websocket/AttachedRuntime.hpp‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace vix::websocket
3737
* @brief Runs a WebSocket server alongside an HTTP app, with shared lifecycle.
3838
*
3939
* Starts the WebSocket server immediately and coordinates shutdown with the attached HTTP
40-
* application.
40+
* application. The supplied runtime executor is borrowed: this object never stops it.
4141
*
4242
* Important lifecycle rule:
4343
* - The HTTP shutdown callback only requests an asynchronous stop.
@@ -54,7 +54,7 @@ namespace vix::websocket
5454
*
5555
* @param app HTTP application instance.
5656
* @param ws WebSocket server instance.
57-
* @param exec Shared runtime executor.
57+
* @param exec Borrowed shared runtime executor. Its caller retains lifecycle authority.
5858
*/
5959
AttachedRuntime(
6060
vix::App &app,
@@ -150,8 +150,8 @@ namespace vix::websocket
150150
/**
151151
* @brief Perform the final blocking shutdown exactly once.
152152
*
153-
* Finalization order:
154-
* 1. stop websocket blocking
153+
* Finalization stops the WebSocket server blocking. The supplied executor
154+
* is borrowed and remains under caller lifecycle control.
155155
*/
156156
void finalize_shutdown() noexcept
157157
{
@@ -175,16 +175,6 @@ namespace vix::websocket
175175
{
176176
}
177177

178-
try
179-
{
180-
if (exec_)
181-
{
182-
exec_->stop();
183-
}
184-
}
185-
catch (...)
186-
{
187-
}
188178
}
189179

190180
private:
@@ -213,7 +203,7 @@ namespace vix::websocket
213203
/** @brief Attached WebSocket server. */
214204
vix::websocket::Server &ws_;
215205

216-
/** @brief Shared runtime executor. */
206+
/** @brief Borrowed runtime executor retained for the attached server graph. */
217207
std::shared_ptr<vix::executor::RuntimeExecutor> exec_;
218208

219209
/** @brief Shared shutdown state used by callbacks and finalization. */
@@ -253,7 +243,7 @@ namespace vix
253243
*
254244
* @param app HTTP application instance.
255245
* @param ws WebSocket server instance.
256-
* @param exec Shared runtime executor.
246+
* @param exec Borrowed shared runtime executor. This function does not stop it.
257247
* @param cfg Core application configuration.
258248
*/
259249
inline void run_http_and_ws(
@@ -306,7 +296,7 @@ namespace vix
306296
*
307297
* @param app HTTP application instance.
308298
* @param ws WebSocket server instance.
309-
* @param exec Shared runtime executor.
299+
* @param exec Borrowed shared runtime executor. This function does not stop it.
310300
* @param port HTTP listening port.
311301
*/
312302
inline void run_http_and_ws(
@@ -324,9 +314,10 @@ namespace vix
324314
/**
325315
* @brief Build and serve a combined HTTP + WebSocket runtime from a config path.
326316
*
327-
* Creates a shared runtime executor, constructs the HTTP app and WebSocket
328-
* server, lets the caller configure routes and handlers, then runs both
329-
* servers together.
317+
* Creates and owns a shared runtime executor, constructs the HTTP app and
318+
* WebSocket server, lets the caller configure routes and handlers, then runs
319+
* both servers together. The helper stops its executor when orchestration
320+
* completes or exits through its configuration/orchestration exception path.
330321
*
331322
* @tparam ConfigureFn Callable type.
332323
* @param configPath Path to the configuration file.
@@ -349,9 +340,18 @@ namespace vix
349340
vix::App app{exec};
350341
vix::websocket::Server ws{cfg, exec};
351342

352-
fn(app, ws);
343+
try
344+
{
345+
fn(app, ws);
346+
run_http_and_ws(app, ws, exec, cfg);
347+
}
348+
catch (...)
349+
{
350+
exec->stop();
351+
throw;
352+
}
353353

354-
run_http_and_ws(app, ws, exec, cfg);
354+
exec->stop();
355355
}
356356

357357
/**

‎include/vix/websocket/session.hpp‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <vix/async/core/io_context.hpp>
3131
#include <vix/async/core/task.hpp>
3232
#include <vix/async/net/tcp.hpp>
33-
#include <vix/executor/RuntimeExecutor.hpp>
3433
#include <vix/websocket/config.hpp>
3534
#include <vix/websocket/protocol.hpp>
3635
#include <vix/websocket/router.hpp>
@@ -52,9 +51,8 @@ namespace vix::websocket
5251
* - heartbeat lifecycle
5352
* - dispatch to the WebSocket router
5453
*
55-
* This implementation is native to Vix and independent of Boost.
56-
* It is runtime-based and uses RuntimeExecutor instead of the old
57-
* generic threadpool-oriented RuntimeExecutor abstraction.
54+
* This implementation is native to Vix and independent of Boost. Its
55+
* asynchronous execution is provided by the supplied Async I/O context.
5856
*/
5957
class Session : public std::enable_shared_from_this<Session>
6058
{
@@ -65,13 +63,12 @@ namespace vix::websocket
6563
* @param stream Accepted native TCP stream.
6664
* @param cfg WebSocket runtime configuration.
6765
* @param router Event router for open, close, message, and error callbacks.
68-
* @param executor Runtime executor used for async scheduling and continuations.
66+
* @param ioc Async I/O context used for scheduling and continuations.
6967
*/
7068
Session(
7169
std::unique_ptr<tcp_stream> stream,
7270
const Config &cfg,
7371
std::shared_ptr<Router> router,
74-
std::shared_ptr<vix::executor::RuntimeExecutor> executor,
7572
std::shared_ptr<io_context> ioc);
7673

7774
~Session() = default;
@@ -243,9 +240,6 @@ namespace vix::websocket
243240
/** @brief Shared router used for lifecycle and message callbacks. */
244241
std::shared_ptr<Router> router_;
245242

246-
/** @brief Runtime executor used by this session. */
247-
std::shared_ptr<vix::executor::RuntimeExecutor> executor_;
248-
249243
/** @brief Shared async IO context associated with the session runtime. */
250244
std::shared_ptr<io_context> ioc_{};
251245

‎src/App.cpp‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,5 @@ namespace vix::websocket
9595
{
9696
}
9797

98-
try
99-
{
100-
if (executor_)
101-
{
102-
executor_->stop();
103-
}
104-
}
105-
catch (...)
106-
{
107-
}
10898
}
10999
} // namespace vix::websocket

‎src/session.cpp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,10 @@ namespace vix::websocket
333333
std::unique_ptr<tcp_stream> stream,
334334
const Config &cfg,
335335
std::shared_ptr<Router> router,
336-
std::shared_ptr<vix::executor::RuntimeExecutor> executor,
337336
std::shared_ptr<io_context> ioc)
338337
: stream_(std::move(stream)),
339338
cfg_(cfg),
340339
router_(std::move(router)),
341-
executor_(std::move(executor)),
342340
ioc_(std::move(ioc))
343341
{
344342
if (!ioc_)

‎src/websocket.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ namespace vix::websocket
354354
std::move(stream),
355355
wsConfig_,
356356
router_,
357-
executor_,
358357
ioContext_);
359358

360359
co_await session->run();

‎tests/app/app_shutdown_test.cpp‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @file app_shutdown_test.cpp
3+
* @brief Verifies that WebSocket App borrows an injected executor.
4+
*/
5+
6+
#include <atomic>
7+
#include <cassert>
8+
#include <memory>
9+
10+
#include <vix/executor/RuntimeExecutor.hpp>
11+
#include <vix/websocket/App.hpp>
12+
13+
namespace
14+
{
15+
using RuntimeExecutor = vix::executor::RuntimeExecutor;
16+
using WebSocketApp = vix::websocket::App;
17+
18+
static void test_stop_preserves_the_injected_executor()
19+
{
20+
auto executor = std::make_shared<RuntimeExecutor>(1u);
21+
22+
executor->start();
23+
24+
assert(executor->started() == true);
25+
assert(executor->running() == true);
26+
assert(executor->accepting() == true);
27+
28+
WebSocketApp app{"", executor};
29+
30+
assert(app.executor() == executor);
31+
32+
std::atomic<bool> executed_before_stop{false};
33+
34+
assert(executor->post(
35+
[&executed_before_stop]()
36+
{
37+
executed_before_stop.store(true, std::memory_order_release);
38+
}) == true);
39+
40+
executor->wait_idle();
41+
42+
assert(executed_before_stop.load(std::memory_order_acquire) == true);
43+
44+
app.stop();
45+
46+
assert(executor->started() == true);
47+
assert(executor->running() == true);
48+
assert(executor->accepting() == true);
49+
50+
std::atomic<bool> executed_after_stop{false};
51+
52+
assert(executor->post(
53+
[&executed_after_stop]()
54+
{
55+
executed_after_stop.store(true, std::memory_order_release);
56+
}) == true);
57+
58+
executor->wait_idle();
59+
60+
assert(executed_after_stop.load(std::memory_order_acquire) == true);
61+
62+
app.stop();
63+
64+
assert(executor->started() == true);
65+
assert(executor->running() == true);
66+
assert(executor->accepting() == true);
67+
68+
std::atomic<bool> executed_after_repeated_stop{false};
69+
70+
assert(executor->post(
71+
[&executed_after_repeated_stop]()
72+
{
73+
executed_after_repeated_stop.store(true, std::memory_order_release);
74+
}) == true);
75+
76+
executor->wait_idle();
77+
78+
assert(executed_after_repeated_stop.load(std::memory_order_acquire) == true);
79+
80+
executor->stop();
81+
}
82+
} // namespace
83+
84+
int main()
85+
{
86+
test_stop_preserves_the_injected_executor();
87+
return 0;
88+
}

‎tests/router/router_dispatch_test.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ namespace
5252
std::unique_ptr<TcpStream>{},
5353
config,
5454
router,
55-
nullptr,
5655
ioContext)};
5756
};
5857

‎tests/router/router_error_test.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ namespace
5252
std::unique_ptr<TcpStream>{},
5353
config,
5454
router,
55-
nullptr,
5655
ioContext)};
5756
};
5857

‎tests/router/router_handlers_test.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ namespace
5050
std::unique_ptr<TcpStream>{},
5151
config,
5252
router,
53-
nullptr,
5453
ioContext)};
5554
};
5655

0 commit comments

Comments
 (0)