From cfcdff463c7ecc521b64a3602384a7c0fe2c6c0a Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Tue, 15 Sep 2026 17:02:58 -0700 Subject: [PATCH] server: contain exceptions thrown by user callbacks A throw from onSubscribe(), onStart(), onCancel() or onClose() reached ConnBase::bevRead(), which resets the circuit and so drops every other channel on it. MonitorSetupOp::connect() throws for a pvRequest which selects no fields, so a Source not repeating SharedPV's own try/catch lets any client do that. Escaping ServerOp::cleanup() also leaves the op short of Dead, so ~ServerOp() asserts. --- src/serverchan.cpp | 10 ++++++++-- src/serverconn.cpp | 32 ++++++++++++++++++++++++++------ src/servermon.cpp | 18 +++++++++++++++--- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/serverchan.cpp b/src/serverchan.cpp index 77a0102..15cffc7 100644 --- a/src/serverchan.cpp +++ b/src/serverchan.cpp @@ -56,8 +56,14 @@ void ServerChan::cleanup() decltype(onClose) fn; fn.swap(onClose); - if(fn) - fn(""); + if(fn) { + try { + fn(""); + }catch(std::exception& e){ + log_err_printf(connsetup, "Channel \"%s\" onClose() error: %s\n", + name.c_str(), e.what()); + } + } } ServerChannelControl::ServerChannelControl(const std::shared_ptr &conn, const std::shared_ptr& channel) diff --git a/src/serverconn.cpp b/src/serverconn.cpp index e01ea87..c00159f 100644 --- a/src/serverconn.cpp +++ b/src/serverconn.cpp @@ -285,8 +285,14 @@ void ServerConn::handle_CANCEL_REQUEST() if(op->state==ServerOp::Executing) { op->state = ServerOp::Idle; - if(op->onCancel) - op->onCancel(); + if(op->onCancel) { + try { + op->onCancel(); + }catch(std::exception& e){ + log_err_printf(connsetup, "IOID %u to \"%s\" remote onCancel() error: %s\n", + unsigned(ioid), chan->name.c_str(), e.what()); + } + } } else { // an allowed race @@ -492,8 +498,15 @@ void ServerOp::cleanup() if(onCancel) { decltype(onCancel) fn; fn.swap(onCancel); - if(state==ServerOp::Executing) - fn(); + if(state==ServerOp::Executing) { + try { + fn(); + }catch(std::exception& e){ + auto ch(chan.lock()); + log_err_printf(connsetup, "IOID %u to \"%s\" cleanup onCancel() error: %s\n", + unsigned(ioid), ch ? ch->name.c_str() : "", e.what()); + } + } } state = ServerOp::Dead; @@ -517,8 +530,15 @@ void ServerOp::cleanup() } } - if(notify) - closer(""); + if(notify) { + // no loop to dispatch to, so this one is not caught by evhelper + try { + closer(""); + }catch(std::exception& e){ + log_err_printf(connsetup, "IOID %u onClose() error: %s\n", + unsigned(ioid), e.what()); + } + } } }} // namespace pvxs::impl diff --git a/src/servermon.cpp b/src/servermon.cpp index 25ec512..ce4d637 100644 --- a/src/servermon.cpp +++ b/src/servermon.cpp @@ -591,7 +591,13 @@ void ServerConn::handle_MONITOR() std::string(SB()<onSubscribe) { - chan->onSubscribe(std::move(ctrl)); + try { + chan->onSubscribe(std::move(ctrl)); + }catch(std::exception& e){ + // a remote error will be signaled from ~ServerMonitorSetup + log_err_printf(connsetup, "Client %s Monitor \"%s\" onSubscribe() error: %s\n", + peerName.c_str(), chan->name.c_str(), e.what()); + } } else { ctrl->error("Monitor operation not implemented by this PV"); } @@ -681,8 +687,14 @@ void ServerConn::handle_MONITOR() op->state = start ? ServerOp::Executing : ServerOp::Idle; } - if(op->onStart) - op->onStart(start); + if(op->onStart) { + try { + op->onStart(start); + }catch(std::exception& e){ + log_err_printf(connsetup, "Client %s IOID %u \"%s\" onStart() error: %s\n", + peerName.c_str(), unsigned(ioid), chan->name.c_str(), e.what()); + } + } { Guard G(op->lock);