Every crash root-caused earlier tonight (Subscription::subscribers, TCPServer::sessions, the Socket::write()/eventReceived() re-arm race) was the same underlying shape: some thread calling a method - or Thread::run() calling delete - on a TCPSession* while another thread concurrently freed that same object, because nothing actually guaranteed the object stayed alive for the duration of a call. Mutex-based patches at each individual call site kept narrowing the window without closing it (confirmed live, repeatedly, via AddressSanitizer under concurrent load - each fix produced a new, different crash signature at the same underlying race), and adding more locks around every write() call also works against the fifo-based design's own goal: no thread should have to block just to hand a message to another thread's session. TCPSession now derives from std::enable_shared_from_this<TCPSession> and exposes pin() - a safe upgrade from a stored raw pointer to a real, ownership-extending shared_ptr, returning nullptr instead of throwing once the object's real owner has let go. TCPServer::sessions is now the one real strong-owning shared_ptr<TCPSession> per live session; every other place that stores a TCPSession* (Subscription::subscribers, Zephora's own sessionsByPlayerName, GroupObject's membership list, etc.) is just a non-owning observer and must pin() before touching the object. A second, distinct problem: Thread::run()'s epoll dispatch starts from a raw void* of unknown validity. An early version of this fix used dynamic_cast<TCPSession*> to identify session objects before calling pin() - confirmed live via AddressSanitizer that dynamic_cast itself segfaults on a stale pointer, since it has to read the (potentially freed) object's own vtable to work at all. TCPSession::tryGet()/TCPServer:: isKnownListeningSocket() replace that: both look the raw pointer up as a key in a registry, never dereferencing it for the check itself - safe to call on any value, live session, immortal listening socket, or fully freed garbage alike. Verified via an AddressSanitizer-instrumented build under sustained concurrent load (up to 15-way parallel connect/subscribe/create/disconnect cycles, multiple rounds, 100+ total connections) with zero errors, then against the real production build with a genuine 500-connection load test (463/500 succeeded; the rest hit the process's own file-descriptor ulimit under peak burst, an unrelated and separately deferred tuning matter, not a crash). Files touched: TCPSession.h (enable_shared_from_this, pin(), the tryGet() registry), TCPServer.h/.cpp (sessions as shared_ptr, isKnownListening Socket() registry, lock made protected for subclasses), Thread.cpp (the whole dispatch rewrite), Subscription.cpp/SubscriptionManager.cpp (every subscriber write() site now pin()s first), ConsoleServer.cpp (logSend() was iterating `sessions` with no lock at all - fixed alongside the type change it needed anyway).
107 lines
3.8 KiB
C++
107 lines
3.8 KiB
C++
#include "Subscription.h"
|
|
#include "Log.h"
|
|
#include "MString.h"
|
|
#include "TCPSession.h"
|
|
#include "TCPSocket.h"
|
|
#include <algorithm>
|
|
|
|
namespace core {
|
|
|
|
Subscription::Subscription(coreutils::MString id, coreutils::MString mode)
|
|
: id(id), mode(mode), owner(""), handler(NULL) {}
|
|
|
|
Subscription::Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString mode)
|
|
: id(id), mode(mode), owner(owner), handler(NULL) {}
|
|
|
|
Subscription::Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString ownership, coreutils::MString mode, SubscriptionHandler *handler, coreutils::MString will, coreutils::MString timer)
|
|
: id(id), ownership(ownership), mode(mode), owner(owner), handler(handler), will(will), timer(timer) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_3) << owner;
|
|
coreutils::Log(coreutils::LOG_DEBUG_3) << will;
|
|
subscriptionOwner(owner, will);
|
|
}
|
|
|
|
Subscription::~Subscription() {
|
|
|
|
std::stringstream out;
|
|
|
|
out << "cancel:" << id << std::endl;
|
|
// subscribers holds plain observer pointers, not ownership (see
|
|
// TCPSession::pin()'s own doc comment) - a subscriber's session may
|
|
// have already started tearing down on another thread by the time
|
|
// this runs. pin() safely tells the two cases apart instead of
|
|
// blindly dereferencing a pointer that might already be mid-
|
|
// destruction or fully freed.
|
|
for (auto subscriber : subscribers) {
|
|
if (auto safe = subscriber->pin())
|
|
safe->write(out.str());
|
|
}
|
|
}
|
|
|
|
int Subscription::subscribe(TCPSession &session) {
|
|
if (handler)
|
|
handler->onSubscribe(session, this);
|
|
else
|
|
onSubscribe(session);
|
|
subscribers.push_back(&session);
|
|
return 1;
|
|
}
|
|
|
|
int Subscription::unsubscribe(TCPSession &session) {
|
|
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber)
|
|
if (*subscriber == &session) {
|
|
subscribers.erase(subscriber++);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool Subscription::subscriptionOwner(coreutils::MString alias, coreutils::MString ownerSelection) {
|
|
subscriptionOwners = alias;
|
|
selectionOfOwnerForSubscription = ownerSelection;
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << "Owner of Subscription: " << subscriptionOwners;
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << "Will of Subscription: " << selectionOfOwnerForSubscription;
|
|
return true;
|
|
}
|
|
|
|
int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) {
|
|
if (handler)
|
|
handler->process(request, out, session, this);
|
|
else
|
|
out << "event:" << request[1] << ":" << request[2] << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
int Subscription::event(std::stringstream &out) {
|
|
// See ~Subscription()'s comment above - same pin()-before-use
|
|
// reasoning applies to every subscriber write here. This is the
|
|
// broadcast path for every *SUBSCRIBERS-mode topic (chat, item
|
|
// database updates, etc.) - the exact crash pin() closes here was
|
|
// confirmed live under concurrent load this session.
|
|
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber)
|
|
if (auto safe = (*subscriber)->pin())
|
|
safe->write(out.str());
|
|
return 1;
|
|
}
|
|
|
|
bool Subscription::ifSubscriber(TCPSession &session) {
|
|
return (std::find(subscribers.begin(), subscribers.end(), &session) != subscribers.end());
|
|
}
|
|
|
|
int Subscription::onSubscribe(TCPSession &session) {
|
|
return 0;
|
|
}
|
|
|
|
bool Subscription::subInvite(TCPSession &session) {
|
|
return 0;
|
|
}
|
|
|
|
void Subscription::sendToAll(std::stringstream &data, TCPSession &sender) {
|
|
for (auto session : subscribers)
|
|
if (session != &sender)
|
|
if (auto safe = session->pin())
|
|
safe->write(data.str());
|
|
data.str("");
|
|
}
|
|
|
|
}
|