Commit Graph
23 Commits
Author SHA1 Message Date
Matt Arant d058322cc6 Move TCPSession lifetime to shared_ptr ownership, fix the whole double-delete/UAF class of crash
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).
2026-09-08 02:24:09 +00:00
Brad Arant 57f47fdecb Convert Subscription and SubscriptionManager to MString. 2023-10-26 18:28:51 -07:00
Matt Arant 3a75e920d9 Servercore Changes 2023-10-06 18:31:22 +00:00
Brad 9a39b6224c *NEXT/*DIE 2023-06-19 23:22:46 +00:00
Brad Arant cb2a61bed9 Added a method to send to all subscribers on subscription. 2023-06-12 12:41:14 -07:00
Brad Arant e78f0cd4f8 Fixed subscription handler logic. 2023-06-12 08:52:44 -07:00
Brad Arant b4fd28e41a Repaired subscription functions with new Subscription object parameters. 2023-06-11 20:21:49 -07:00
Brad Arant 8c9f4117ae Cleaned up the subscription handler stuff.' 2023-06-10 19:27:22 -07:00
Brad Arant b40cb9f5a4 Added support for the SubscriptionHandlerFactory object. 2023-06-10 16:01:56 -07:00
Brad Arant 0ef782a11c Added subscription to handler list so subscription can be accessed from handler. 2023-06-09 20:16:04 -07:00
Brad Arant cfa70c5863 Fixed multithread sync issue and duplicate thread on termination. 2023-03-11 21:55:33 -08:00
Brad Arant b0118cfc56 subscriber work 2023-03-10 12:55:46 -08:00
Brad Arant d8eb3d7bb2 merged in publish handlers. 2023-02-09 11:38:09 -08:00
Brad 1e76a7f49a setup ignore update for .history files. 2023-02-08 03:31:51 +00:00
Brad Arant 0dc5d5738e Fixed writing issue during write to another socket. 2022-10-25 20:33:50 -07:00
Brad Arant db199eaec3 ? 2022-10-18 19:06:28 -07:00
Brad 001cd8c06c Added SubscriptionHandler capability to Subscriptions. 2022-10-18 02:37:46 +00:00
Brad Arant 3a6883e77f Cleaned up socket scheduling. Very clean now. 2022-03-15 21:02:35 +00:00
Brad Arant 03a3799888 Mods to improve stability for Zephora. 2022-03-14 18:48:58 +00:00
Brad Arant d13e3f9fb5 Got the subscription event scope parameter added. 2022-02-13 00:23:56 +00:00
Brad Arant 16ffd60e75 Subscription system improvements. 2022-01-29 15:52:04 +00:00
Brad Arant 3ef82e0fce Compiles 2022-01-24 18:36:47 -08:00
Brad Arant ec39c1df1a Added subscription service capability. 2021-08-31 17:57:21 -07:00