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.
This commit is contained in:
Sang Woo Kim authored and Michael Davidsaver committed 2026-09-15 19:47:06 -07:00
1 parent a0422ec511
commit cfcdff463c
3 files changed
+49 -11

No files matched your search

+8 -2
View File
@@ -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<ServerConn> &conn, const std::shared_ptr<ServerChan>& channel)
+26 -6
View File
@@ -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() : "<defunct>", 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
+15 -3
View File
@@ -591,7 +591,13 @@ void ServerConn::handle_MONITOR()
std::string(SB()<<pvRequest).c_str());
if(chan->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);