Files
ServerCore/Thread.cpp
T
Brad Arant c3e8d70774 Fix numerous thread/socket lifecycle crashes and TLS/epoll write-stall busy-loop
Thread/EPoll lifecycle:
- Thread::~Thread() now actually joins/frees its worker thread (was
  empty - the OS thread kept running against already-freed EPoll/Socket
  objects on any early teardown, a confirmed "double free" crash)
- EPoll::~EPoll() calls stop() if never explicitly stopped, for the
  same reason
- epoll_wait() now uses a 1s timeout instead of blocking forever, so a
  worker thread can actually notice isStopping() and let itself be
  joined
- EPoll::processCommand() iterated threads by value, silently
  double-freeing the real vector's Thread the moment each copy went
  out of scope
- ThreadScope gained onThreadStart()/onThreadEnd() hooks (actually
  wired up now - the constructor that was supposed to store the scope
  discarded it) for per-thread C-library init (e.g. MySQL client)

Socket/TCPSocket/TLS:
- Socket::writeSocket(): give up and shut down a connection after 50
  consecutive zero-progress sends instead of retrying forever -
  SSL_ERROR_WANT_READ on a write needs a READ to make progress, but
  resetSocket() re-arms EPOLLWRNORM regardless, which an
  already-writable TCP socket satisfies immediately - a real stuck
  connection generated 55GB, then 12.9GB of debug log this way before
  being caught
- Socket::writeSocket() now honors sendData()'s actual return value
  (previously discarded, entry popped unconditionally after one call) -
  a large response could get silently truncated past what fit in one
  write()/SSL_write() call
- TLS::registerSocket()/sendData()/receiveData(): `initialized` now
  only goes true once SSL_set_fd() has actually succeeded, and
  sendData()/receiveData() check it first - fixes a real SIGSEGV where
  a new session's own queued greeting could reach SSL_write() on a
  different epoll thread before this session's handshake setup had run
- TCPServer::connectionRequest() now performs the TLS handshake ON THE
  ACCEPTED SESSION, not the listening server itself (was building SSL
  state on the wrong object, leaving every session's own `ssl`
  uninitialized)
- TCPServer::receiveData() always uses the plain (non-TLS) read path -
  the listening socket is never itself a TLS peer
- TCPSocket's block-mode literal reading used an unset `blockLength`
  field instead of `blockSize`, and could busy-spin forever on a
  multi-packet literal
- TCPSocket line scanning replaced an unbounded strcspn() (assumes a
  NUL-terminated buffer; read past a non-NUL-terminated receive buffer
  into adjacent heap memory) with a bounds-checked memchr() scan

Protocol/session handling:
- CommandList::processRequest() now case-folds the command keyword
  before lookup - SMTP/POP3/IMAP commands are spec-required to be
  case-insensitive; a client sending "Mail From:" instead of
  "MAIL FROM:" previously failed to dispatch at all
- TCPSession::protocol() no longer drops blank lines while a handler
  has grabbed input (e.g. __SMTP_DATA reading a message body, an IMAP
  APPEND literal) - those handlers depend on seeing blank lines as
  real content, not commands; this silently corrupted any message body
  containing a blank line
- TCPSession's uuid alias fixed: streaming a raw uuid_t via operator<<
  triggered libstdc++'s unsigned-char* overload (strlen() on 16 binary
  bytes, a real stack-buffer-overflow read), and `alias` pointed at a
  stack-local that was already destroyed by the time the constructor
  returned
- INotify::processEvent() only treats event->name as a NUL-terminated
  string when event->len > 0 (per inotify(7)) instead of unconditional
  strlen() on a field the kernel doesn't guarantee is even present -
  a confirmed heap-buffer-overflow read that had been misdiagnosed as
  a MySQL thread-safety issue
2026-09-03 05:17:41 +00:00

122 lines
4.2 KiB
C++

#include "Thread.h"
#include "EPoll.h"
#include <sys/epoll.h>
namespace core {
Thread::Thread(EPoll &ePoll) : ePoll(ePoll), _thread(NULL), thread(NULL) {}
// Previously discarded `scope` entirely (never assigned to the `thread`
// member) - a real, live bug found while adding the first actual caller
// of this constructor (APIServer's MysqlThreadScope): the whole point
// of passing a scope in was to have it invoked from run() below, which
// silently never happened.
Thread::Thread(EPoll &ePoll, ThreadScope *scope) : ePoll(ePoll), _thread(NULL), thread(scope) {}
// Was empty - _thread (heap-allocated in start()) was never joined or
// freed here. A joinable std::thread destroyed without join()/detach()
// calls std::terminate() immediately, which this leaked-pointer
// approach silently sidestepped: the wrapped OS thread just kept
// running - polling this Thread's EPoll reference, touching Socket
// objects registered with it - even after this object, its EPoll, and
// every one of those sockets had already been destroyed out from under
// it (confirmed live: an exception thrown during BARANTMail startup,
// after EPoll::start() had already spun up its worker pool, unwound
// the stack destructing EPoll and its registered sockets while those
// still-running threads kept dereferencing them, producing a real
// "double free or corruption" crash). join() first (skipped if already
// joined via EPoll::stop() having run normally) so the OS thread is
// actually gone before anything it might touch gets freed.
Thread::~Thread() {
if (_thread) {
if (_thread->joinable())
_thread->join();
delete _thread;
}
}
void Thread::start() {
_thread = new std::thread(&Thread::run, this);
}
void Thread::join() {
_thread->join();
}
std::string Thread::getStatus() {
return status;
}
pid_t Thread::getThreadId() {
return threadId;
}
int Thread::getCount() {
return count;
}
void Thread::output(std::stringstream &data) {
data << "|" << getThreadId();
data << "|" << getStatus();
data << "|" << getCount();
}
void Thread::run() {
threadId = syscall(SYS_gettid);
coreutils::Log(coreutils::LOG_DEBUG_1) << "Thread started with thread id " << threadId << ".";
if(thread)
thread->onThreadStart();
count = 0;
struct epoll_event events[50];
while(1) {
if(ePoll.isStopping())
break;
status = "WAITING";
// A finite timeout, not the previous -1 (wait forever), is what
// makes EPoll::stop() able to return at all: a thread idly
// blocked here with no socket activity would otherwise never
// re-check isStopping() above, so join()ing it (both
// EPoll::stop() and, now, ~Thread() itself) would hang forever.
// Costs up to a one-second shutdown latency.
int rc = epoll_wait(ePoll.getDescriptor(), events, 50, 1000);
status = "RUNNING";
if(rc < 0) {
// TODO: Make log entry indicating status received and ignore for now.
} else if(rc == 0) {
// Plain timeout, no socket activity - not itself a signal to
// stop (isStopping() above already decides that). Previously
// broke out of the loop unconditionally, which the old
// infinite wait never actually triggered in practice, but
// would have silently ended this thread after any single
// idle second once the wait became finite.
} else if(rc > 0) {
for(int ix = 0; ix < rc; ++ix) {
++count;
if(((Socket *)events[ix].data.ptr)->eventReceived(events[ix], ++ePoll.eventId)) {
// coreutils::Log(coreutils::LOG_DEBUG_4) << "return true";
// ePoll.resetSocket((Socket *)events[ix].data.ptr);
} else {
((Socket *)events[ix].data.ptr)->shutDown = true;
// coreutils::Log(coreutils::LOG_DEBUG_4) << "return false";
delete (Socket *)events[ix].data.ptr;
}
}
}
}
if(thread)
thread->onThreadEnd();
coreutils::Log(coreutils::LOG_DEBUG_1) << "Thread ending with thread id " << threadId << ".";
}
}