Web socket close() call can do segmentation fault
Hello Nodepp dev. team! I would like to share simple web socked client test code which generates segmentation fault when trying to invoke cli.close(); while handling received message in onData:
auto client = ws::client( str_connect_url.c_str() );
client.onConnect( [=] ( ws_t cli ) {
console::log( "ws -> client side connected" );
cli.onData( [=] ( string_t data ) {
console::log( "ws -> on data -> client side received data ->", data );
cli.close(); // <<<<<<<<<<< CRASH IS HERE <<<<<<<<<<<
} );
cli.onDrain( [=] () {
console::log( "ws -> on drain -> client side connection drained" );
} );
cli.onClose( [=] () {
console::log( "ws -> on close -> client side connection closed" );
} );
cli.onError( [=] ( except_t err ) {
console::log( "ws -> on error -> client side connection error -> ", err.what() );
} );
//
cli.write( "This is WS message sent from client" );
} );
client.onError( [=] ( except_t err ){
bee::log::error( "ws -> on error -> client side error ->", err.what() );
} );
You can simply add cli.close(); into WS-Client example app which comes with Nodepp.
Please suggest correct way to close web socket if my code is incorrect.
Crash can be reproduced on both Mac OSX and Linux OSes I have and it looks like logical error. Here is the call stack:
1 nodepp::ptr_t<nodepp::queue_t<nodepp::function_t<bool>>::DONE, 16ul>::null ptr.h 360 0x5555557301ce
2 nodepp::queue_t<nodepp::function_t<bool>>::empty queue.h 121 0x555555721526
3 nodepp::queue_t<nodepp::function_t<bool>>::clear queue.h 284 0x555555714d2c
4 nodepp::event_t<>::clear() const event.h 111 0x555555702bf4
5 nodepp::socket_t::free socket.h 485 0x5555556f196f
6 nodepp::socket_t::close socket.h 402 0x5555556f0f5e
7 operator() test_basic.cpp 186 0x5555556d0f63
8 nodepp::function_t<void, nodepp::string_t>::func_impl<stat_ws_client(const char *, int, bool *)::<lambda(nodepp::ws_t)>::<lambda(nodepp::string_t)>>::invoke(const nodepp::string_t &) const function.h 62 0x5555556ded7d
9 nodepp::function_t<void, nodepp::string_t>::emit function.h 44 0x55555571994d
10 nodepp::function_t<void, nodepp::string_t>::operator() function.h 40 0x555555707801
11 nodepp::event_t<nodepp::string_t>::on(nodepp::function_t<void, nodepp::string_t>) const::{lambda(nodepp::string_t)#1}::operator()(nodepp::string_t) const event.h 85 0x55555571d3f0
12 nodepp::function_t<bool, nodepp::string_t>::func_impl<nodepp::event_t<nodepp::string_t>::on(nodepp::function_t<void, nodepp::string_t>) const::{lambda(nodepp::string_t)#1}>::invoke(nodepp::string_t const&) const function.h 62 0x55555576aa97
13 nodepp::function_t<bool, nodepp::string_t>::emit function.h 44 0x555555719d92
14 nodepp::event_t<nodepp::string_t>::emit event.h 122 0x555555707a1a
15 nodepp::generator::stream::pipe::operator()<nodepp::ws_t> generator.h 253 0x55555573b8d8
16 nodepp::kernel_t::poll_add<nodepp::ws_t const, nodepp::generator::stream::pipe, nodepp::ws_t>(nodepp::ws_t const&, int, nodepp::generator::stream::pipe, unsigned long, nodepp::ws_t const&)::{lambda()#1}::operator()() const kernel.h 205 0x55555572b9d3
17 nodepp::function_t<int>::func_impl<nodepp::kernel_t::poll_add<nodepp::ws_t const, nodepp::generator::stream::pipe, nodepp::ws_t>(nodepp::ws_t const&, int, nodepp::generator::stream::pipe, unsigned long, nodepp::ws_t const&)::{lambda()#1}>::invoke() const function.h 62 0x55555576a76e
18 nodepp::function_t<int>::emit function.h 44 0x55555570f0ab
19 nodepp::function_t<int>::operator() function.h 40 0x5555556fd82a
20 nodepp::kernel_t::next() const::{lambda(int&, unsigned long&)#1}::operator()(int&, unsigned long&) const kernel.h 296 0x5555556ec144
21 nodepp::function_t<int, int&, unsigned long&>::func_impl<nodepp::kernel_t::next() const::{lambda(int&, unsigned long&)#1}>::invoke(int&, unsigned long&) const function.h 62 0x55555576c982
22 nodepp::function_t<int, int&, unsigned long&>::emit function.h 44 0x55555570de5b
23 nodepp::function_t<int, int&, unsigned long&>::operator() function.h 40 0x5555556fbe8d
24 nodepp::coroutine_t::next coroutine.h 57 0x5555556e42bc
25 nodepp::coroutine_t::operator() coroutine.h 55 0x5555556e424c
26 nodepp::loop_t::add<nodepp::coroutine_t>(nodepp::coroutine_t) const::{lambda()#1}::operator()() const loop.h 169 0x555555702332
27 nodepp::function_t<int>::func_impl<nodepp::loop_t::add<nodepp::coroutine_t>(nodepp::coroutine_t) const::{lambda()#1}>::invoke() const function.h 62 0x55555576b076
28 nodepp::function_t<int>::emit function.h 44 0x55555570f0ab
29 nodepp::function_t<int>::operator() function.h 40 0x5555556fd82a
30 nodepp::loop_t::normal_queue_next() const::{lambda()#1}::operator()() const loop.h 81 0x5555556e6f32
31 nodepp::loop_t::normal_queue_next loop.h 114 0x5555556e72fc
32 nodepp::loop_t::next loop.h 154 0x5555556e74b0
33 nodepp::kernel_t::next kernel.h 259 0x5555556ec2d4
34 nodepp::process::next evloop.h 62 0x5555556ec95c
35 stat_ws_client test_basic.cpp 214 0x5555556d1816
I found workaround to avoid crash. I put call to cli.close() into delayed task executed soon in message loop of client thread:
console::log( "ws -> will delay task to close connection on client side" );
process::add( [=] () {
console::log( "ws -> will close connection on client side" );
cli.close();
return -1; // return -1 -> will kill the task
} );
Not sure Nodepp web sockets were designed to be closed with delay only. But I can leave with this few days more)))
Not sure Nodepp web sockets were designed to be closed with delay only. But I can leave with this few days more)))
- no, this is a real bug, and is caused by
event.h, I have to change few things up.
Thanks in advance
Hi Sergiy,
Thank you for catching this bug! You hit a classic Use-After-Free edge case. The issue occurred because calling cli.close() inside the onData() event was clearing the execution queue while the .emit() function was still iterating through it.
I have refactored the event.h logic in Nodepp V1.4.0_3 to implement a Deferred Deletion strategy using internal state flags. Instead of deleting the memory immediately, we now implemented a KILL flag to the event's state machine to allow poison pill mechanism:
EV_STATE_USED: We mark the event as 'busy' during execution.EV_STATE_KILL: If aclear()orclose()is requested while the event is busy, we simply mark it for death.
void clear() const noexcept {
if( obj->state & STATE::EV_STATE_USED )
{ obj->state |= STATE::EV_STATE_KILL; return; }
obj->state &=~STATE::EV_STATE_KILL;
obj->que.clear();
}
The safety logic now lives inside the emit() cycle: once the iteration finishes and it's safe to deallocate, the kernel checks the KILL flag and performs a clean wipe.
void emit( const A&... args ) const noexcept {
if( empty() || is_paused() || is_used() ){ return; }
obj->state |= STATE::EV_STATE_USED;
/*event iterator logic*/
/**/obj->state &=~ STATE::EV_STATE_USED;
if( obj->state & STATE::EV_STATE_KILL ){ clear(); }
}
I’ve attached a stress-test case that covers three different scenarios where nested event clearing or off-loading would typically crash the program. As you can see, the nodepp's architecture now handles these recursive deletions gracefully.
#include <nodepp/nodepp.h>
#include <nodepp/event.h>
using namespace nodepp;
void onMain(){
event_t<> ev;
event_t<> ov;
auto addr = ev.on([=](){ console::log( "-A-" ); });
/*-------*/ ev.on([=](){ console::log( "-B-" ); });
/* // CASE 1
ev.on([=](){
ev.clear(); // <- this must crash the programs | bug fixed :)
ev.on([=](){ console::log( "hello world" ); });
console::log( "<---->", ev.size() );
});
*/
/* // CASE 2
ev.on([=](){ ov.once([=](){
ev.clear(); // <- this must crash the programs | bug fixed :)
ev.on([=](){ console::log( "hello world" ); });
console::log( "<---->", ev.size() );
}); ov.emit(); });
*/
/**/ // CASE 3
ev.once([=](){ ov.emit(); }); // <- this must brake the event queue
ov.on ([=](){ ev.off( addr ); });
/**/
ev.on([=](){ console::log( "-C-" ); });
ev.on([=](){ console::log( "-D-" ); });
ev.emit();
ev.emit();
ev.emit();
ev.emit();
console::log( "-E-" );
}
now let's try to brake nodepp again!
Everything works! Thanks! Me is happy to help Nodepp to reach 100% ideal stable state.