slaclab/rogue

UDP Server and Client internal thread may cause std::bad_weak_ptr exception to be thrown

Closed this issue · 2 comments

shared_from_this() may be called before the object and shared_ptr are fully initialized. This causes std::bad_weak_ptr exception and crashes the application.

Trace Server:
rpu::Server::runThread()
-- ris::Pool::acceptReq(maxPayload(),false)
---- allocBuffer(size,&frSize)
------ shared_from_this()

Trace Client:
rpu::Client::runThread()
-- ris::Pool::acceptReq(maxPayload(),false)
---- allocBuffer(size,&frSize)
------ shared_from_this()

Proposed fix:

auto weakPtr = weak_from_this();
auto sharedPtr = weakPtr.lock();

while (!sharedPtr) {
    std::this_thread::yield();
    sharedPtr = weakPtr.lock();
}

This might have been fixed by #836

Yes, that indeed fixes the issue.