From f90f3a5106cd0111ff86f8388f520c7ed42a6315 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Tue, 1 Sep 2026 11:19:43 +0200 Subject: [PATCH] server: ensure onClose() called if channel create fails Setting onClose() alone is no longer enough to claim a channel Surprise. std::function(function&& o) does not fully specify the post-condition of 'o'. move() from std::function members of client Builders is ok to leave in "valid but unspecified" state. --- src/clientdiscover.cpp | 2 +- src/clientget.cpp | 6 ++--- src/clientintrospect.cpp | 2 +- src/clientmon.cpp | 9 ++++--- src/pvxs/source.h | 3 ++- src/serverchan.cpp | 9 ++++--- src/serverconn.cpp | 12 +++++---- test/testget.cpp | 57 +++++++++++++++++++++++++++++++++------- 8 files changed, 71 insertions(+), 29 deletions(-) diff --git a/src/clientdiscover.cpp b/src/clientdiscover.cpp index d40f792..8275818 100644 --- a/src/clientdiscover.cpp +++ b/src/clientdiscover.cpp @@ -53,7 +53,7 @@ bool Discovery::cancel() loop.call([this, &junk, &ret](){ ret = _cancel(false); if(!notify_busy) - junk = std::move(notify); + junk.swap(notify); // leave opByIOID for GC }); return ret; diff --git a/src/clientget.cpp b/src/clientget.cpp index d167acc..7c4c426 100644 --- a/src/clientget.cpp +++ b/src/clientget.cpp @@ -184,11 +184,11 @@ struct GPROp : public OperationBase (void)loop.tryCall([&, this](){ ret = _cancel(false); if(!builder_busy) - junkB = std::move(builder); + junkB.swap(builder); if(!done_busy) - junkD = std::move(done); + junkD.swap(done); if(!onInit_busy) - junkI = std::move(onInit); + junkI.swap(onInit); // leave opByIOID for GC }); return ret; diff --git a/src/clientintrospect.cpp b/src/clientintrospect.cpp index a0ccb11..b02bbc3 100644 --- a/src/clientintrospect.cpp +++ b/src/clientintrospect.cpp @@ -47,7 +47,7 @@ struct InfoOp : public OperationBase (void)loop.tryCall([this, &junk, &ret](){ ret = _cancel(false); if(!done_busy) - junk = std::move(done); + junk.swap(done); // leave opByIOID for GC }); return ret; diff --git a/src/clientmon.cpp b/src/clientmon.cpp index c8d3022..e63a252 100644 --- a/src/clientmon.cpp +++ b/src/clientmon.cpp @@ -260,7 +260,8 @@ struct SubscriptionImpl final : public OperationBase, public Subscription ret.reset(strong.get(), [strong](Subscription*) mutable { // on worker? - auto junk(std::move(strong)); + decltype(strong) junk; + junk.swap(strong); // need to do cleanup on worker if running auto loop(junk->loop); loop.tryCall(std::bind([](std::shared_ptr& junk) noexcept { @@ -282,7 +283,7 @@ struct SubscriptionImpl final : public OperationBase, public Subscription loop.call([this, &junk, &fn]() { if(event_busy) throw std::logic_error("Must not replace Subscription::onEvent() while callback in progress"); - junk = std::move(event); + junk.swap(event); this->event = std::move(fn); }); } @@ -294,9 +295,9 @@ struct SubscriptionImpl final : public OperationBase, public Subscription (void)loop.tryCall([this, &junk, &junkI, &ret](){ ret = _cancel(false); if(!event_busy) - junk = std::move(event); // trash when cancelled from app. worker + junk.swap(event); // trash when cancelled from app. worker if(!onInit_busy) - junkI = std::move(onInit); + junkI.swap(onInit); // leave opByIOID for GC }); return ret; diff --git a/src/pvxs/source.h b/src/pvxs/source.h index f899604..3042ffb 100644 --- a/src/pvxs/source.h +++ b/src/pvxs/source.h @@ -264,7 +264,8 @@ struct PVXS_API Source { * * - Do nothing, allowing some other Source with higher/later order a chance to create. * - Call ChannelControl::close() to explicitly reject the channel. - * - std::move() the op and/or call ChannelControl::setHandler() to accept the new channel. + * - std::move() the op and/or call ChannelControl::onOp(), ChannelControl::onSubscribe(), + * and/or ChannelControl::onRPC() to accept the new channel. * - std::move() the op and allow ChannelControl to be destroyed to implicitly reject the channel. */ virtual void onCreate(std::unique_ptr&& op) =0; diff --git a/src/serverchan.cpp b/src/serverchan.cpp index 6a35640..77a0102 100644 --- a/src/serverchan.cpp +++ b/src/serverchan.cpp @@ -54,7 +54,8 @@ void ServerChan::cleanup() } } - auto fn(std::move(onClose)); + decltype(onClose) fn; + fn.swap(onClose); if(fn) fn(""); } @@ -303,7 +304,7 @@ void ServerConn::handle_CREATE_CHANNEL() if(chan->state!=ServerChan::Creating) { msg = "rejected"; - } else if(chan->onOp || chan->onRPC || chan->onSubscribe || chan->onClose) { + } else if(chan->onOp || chan->onRPC || chan->onSubscribe) { msg = "accepted"; claimed = true; @@ -333,12 +334,12 @@ void ServerConn::handle_CREATE_CHANNEL() sts.code = Status::Fatal; sts.msg = "Refused to create Channel"; sts.trace = "pvx:serv:refusechan:"; - chan->state = ServerChan::Destroy; + chan->cleanup(); sid = -1; } - // ServerChannelControl destroyed it not saved by claiming Source + // ServerChannelControl destroyed if not saved by claiming Source } diff --git a/src/serverconn.cpp b/src/serverconn.cpp index c4e2711..e01ea87 100644 --- a/src/serverconn.cpp +++ b/src/serverconn.cpp @@ -489,15 +489,17 @@ void ServerOp::cleanup() if(state==ServerOp::Dead) return; - if(state==ServerOp::Executing && onCancel) { - auto fn(std::move(onCancel)); - fn(); + if(onCancel) { + decltype(onCancel) fn; + fn.swap(onCancel); + if(state==ServerOp::Executing) + fn(); } state = ServerOp::Dead; - onCancel = nullptr; - auto closer(std::move(onClose)); + decltype(onClose) closer; + closer.swap(onClose); bool notify = closer.operator bool(); if(auto ch = chan.lock()) { diff --git a/test/testget.cpp b/test/testget.cpp index dd35293..9c69ea1 100644 --- a/test/testget.cpp +++ b/test/testget.cpp @@ -468,11 +468,19 @@ struct Tester { struct ErrorSource : public server::Source { - const bool phase = false; + const enum phase_t { + phaseCreate, + phaseOp, + phaseGet, + } phase; const Value type; - explicit ErrorSource(bool phase) - :phase(phase) + epicsEvent& closedChan; + epicsEvent& closedOp; + explicit ErrorSource(phase_t pha, epicsEvent& clCh, epicsEvent& clOp) + :phase(pha) ,type(nt::NTScalar{TypeCode::Int32}.create()) + ,closedChan(clCh) + ,closedOp(clOp) {} virtual void onSearch(Search &op) override final @@ -485,8 +493,20 @@ struct ErrorSource : public server::Source { auto chan = std::move(op); + auto& clo(closedChan); + chan->onClose([&clo](const std::string&) { + clo.signal(); + }); + + if(phase==phaseCreate) + throw std::runtime_error("Refuse create"); + chan->onOp([this](std::unique_ptr&& op) { - if(!phase) { + auto& clo(closedOp); + op->onClose([&clo](const std::string&) { + clo.signal(); + }); + if(phase==phaseOp) { op->error("haha"); return; } @@ -498,13 +518,14 @@ struct ErrorSource : public server::Source } }; -void testError(bool phase) +void testError(ErrorSource::phase_t phase) { testShow()<<__func__<<" phase="<(phase)) + .addSource("err", std::make_shared(phase, closedChan, closedOp)) .start(); auto cli = serv.clientConfig().build(); @@ -521,7 +542,11 @@ void testError(bool phase) cli.hurryUp(); - if(testOk1(done.wait(5.0))) { + if(phase==ErrorSource::phaseCreate) { + // client will silently re-try until cancelled + testSkip(2, "client will silently re-try create until cancelled"); + + } else if(testOk1(done.wait(5.0))) { testThrows([&actual]() { auto val = actual(); testShow()<<"unexpected result\n"<cancel(); + cli.cacheClear("", client::Context::Disconnect); // force close channel + } + testOk(closedChan.wait(2.0), "wait for channel onClose phase=%d", phase); } struct DisconnSource : public server::Source @@ -602,7 +638,7 @@ void testServerClose() MAIN(testget) { - testPlan(68); + testPlan(79); testSetup(); logger_config_env(); const bool canIPv6 = pvxs::impl::evsocket::canIPv6; @@ -625,8 +661,9 @@ MAIN(testget) Tester().badRequest(); Tester().delayExec(); Tester().ordering(); - testError(false); - testError(true); + testError(ErrorSource::phaseCreate); + testError(ErrorSource::phaseOp); + testError(ErrorSource::phaseGet); testServerClose(); cleanup_for_valgrind(); return testDone();