Files
Dario LipicarandClaude Opus 5 3a31c91d13 fix(plain): close the RPC acceptor on the server's strand, not the caller's thread (#39)
RpcServerTcp::stop() and RpcServerSsl::stop() closed m_acceptor on whatever
thread called them — in practice the host thread, via ~PlainTransportHost —
while doAccept() re-armed async_accept from inside its own completion handler,
on the io worker. Nothing serialized the two.

This is the acceptor half of the race PR #38 fixed for RpcConnection, and it
fails identically: asio acceptors are "Shared objects: Unsafe", and close()
runs cleanup_descriptor_data(), which nulls the reactor's per-descriptor state
while reactive_socket_service_base::start_op() holds it by reference. It was
left out of #38 because every backtrace captured in the wild was a write
initiation, never an accept — but it reproduces on demand:

  EXC_BAD_ACCESS  KERN_INVALID_ADDRESS at 0x98
    logos::plain::RpcServerTcp::doAccept()
    ...reactive_socket_move_accept_op<...>::do_complete(...)
    logos::plain::IoContextPool::IoContextPool()::$_0    <- io worker thread

Both servers now own a strand. doAccept()'s completion handler is
bind_executor'd onto it (so the re-arm runs there) and stop() hands the close
to it with dispatch() — inline when already on the strand, queued and
non-blocking from anywhere else, exactly as RpcConnection::closeStreamOnStrand
does.

start() still runs open/bind/listen inline: callers read boundPort() the moment
it returns. That is safe because no async op on the acceptor exists yet, and
PlainTransportHost serializes start()/stop() under its own mutex. Only the
accept loop moves onto the strand, which is invisible to clients — listen() has
already run, so an early connect waits in the backlog.

Deferring the close leaves the listener open for the microseconds between
stop() returning and the strand running it, so a connection can still be
accepted in that gap. The accept path therefore tests m_stopped and publishes
the connection under one lock, and drops a late socket instead of wrapping it
in a connection and stop()ing it — conn->stop() would call
onConnectionClosed() on the IncomingCallHandler whose destructor started this
teardown. The TLS server gets the same guard, where it was already latent: an
async_handshake in flight was never aborted by closing the acceptor.

Adds RpcServerTeardownTest: a start/connect/stop stress loop shaped like
test_rpc_connection_teardown.cpp, plus a round-trip check that a client
connecting the instant start() returns is still served.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 15:48:17 -03:00

125 lines
4.5 KiB
C++

#ifndef LOGOS_PLAIN_RPC_SERVER_H
#define LOGOS_PLAIN_RPC_SERVER_H
#include "incoming_call_handler.h"
#include "rpc_connection.h"
#include "wire_codec.h"
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/context.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/strand.hpp>
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace logos::plain {
// -----------------------------------------------------------------------------
// RpcServer (TCP) — accepts TCP connections, wraps each in an
// RpcConnection, keeps them alive until they drop.
//
// Every accepted connection uses the same shared IncomingCallHandler so
// the provider layer can dispatch regardless of which client is talking.
// The server doesn't multiplex objects by itself; it's the handler's job
// to look up the target object for each incoming CallMessage.
// -----------------------------------------------------------------------------
using TcpStream = boost::asio::ip::tcp::socket;
using TcpConnection = RpcConnection<TcpStream>;
class RpcServerTcp : public std::enable_shared_from_this<RpcServerTcp> {
public:
RpcServerTcp(boost::asio::io_context& ioc,
const std::string& host,
uint16_t port,
std::shared_ptr<IWireCodec> codec,
IncomingCallHandler* handler);
// Start accepting. Returns false if bind fails. Synchronous through
// listen(), so boundPort() is valid the moment it returns true; the accept
// loop itself is armed on m_strand (see closeAcceptorOnStrand). Callers
// must not overlap start() with stop() — PlainTransportHost serializes
// them under its own mutex.
bool start();
// Actual bound port (useful when the caller requested port=0).
uint16_t boundPort() const { return m_boundPort; }
void stop();
private:
// MUST run on m_strand — see closeAcceptorOnStrand().
void doAccept();
void closeAcceptor();
void closeAcceptorOnStrand();
boost::asio::ip::tcp::acceptor m_acceptor;
// Serializes every operation on m_acceptor after listen(): the accept
// initiations and the close. Same reason RpcConnection has one for its
// stream — asio acceptors are "Shared objects: Unsafe".
boost::asio::strand<boost::asio::any_io_executor> m_strand;
std::shared_ptr<IWireCodec> m_codec;
IncomingCallHandler* m_handler;
std::string m_host;
uint16_t m_port;
uint16_t m_boundPort = 0;
std::mutex m_mu;
std::vector<std::shared_ptr<TcpConnection>> m_conns;
bool m_stopped = false;
};
// -----------------------------------------------------------------------------
// RpcServer (TLS) — same as TCP but wraps every accepted socket in an
// asio::ssl::stream and completes the handshake before spinning up the
// RpcConnection.
// -----------------------------------------------------------------------------
using SslStream = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
using SslConnection = RpcConnection<SslStream>;
class RpcServerSsl : public std::enable_shared_from_this<RpcServerSsl> {
public:
RpcServerSsl(boost::asio::io_context& ioc,
const std::string& host,
uint16_t port,
boost::asio::ssl::context sslCtx,
std::shared_ptr<IWireCodec> codec,
IncomingCallHandler* handler);
// See RpcServerTcp::start().
bool start();
uint16_t boundPort() const { return m_boundPort; }
void stop();
private:
// MUST run on m_strand — see closeAcceptorOnStrand().
void doAccept();
void closeAcceptor();
void closeAcceptorOnStrand();
boost::asio::ip::tcp::acceptor m_acceptor;
// See RpcServerTcp::m_strand.
boost::asio::strand<boost::asio::any_io_executor> m_strand;
boost::asio::ssl::context m_sslCtx;
std::shared_ptr<IWireCodec> m_codec;
IncomingCallHandler* m_handler;
std::string m_host;
uint16_t m_port;
uint16_t m_boundPort = 0;
std::mutex m_mu;
std::vector<std::shared_ptr<SslConnection>> m_conns;
bool m_stopped = false;
};
} // namespace logos::plain
#endif // LOGOS_PLAIN_RPC_SERVER_H