From 09d64051bfa9b41f7ba06d7f064bd85370849b52 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Wed, 9 Sep 2026 18:59:44 -0700 Subject: [PATCH] server: update/fix connect()/error() blocking Switch connect() and error to consistently not sync. with server worker thread. --- src/pvxs/source.h | 2 +- src/serverget.cpp | 37 +++++++++++++++++++++++++++++-------- src/serverintrospect.cpp | 13 +++++++------ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/pvxs/source.h b/src/pvxs/source.h index 0eefe06..f49ab27 100644 --- a/src/pvxs/source.h +++ b/src/pvxs/source.h @@ -28,7 +28,7 @@ public: //! For GET_FIELD, GET, or PUT. Inform peer of our data-type. //! @throws std::runtime_error if the client pvRequest() field mask does not select any fields of prototype. - //! @note Synchronizes with server worker thread + //! @since UNRELEASED Does not synchronize with server worker thread virtual void connect(const Value& prototype) =0; //! Indicate that this operation can not be setup //! @since 1.2.3 Does not synchronize with server worker thread diff --git a/src/serverget.cpp b/src/serverget.cpp index 230d423..03e55db 100644 --- a/src/serverget.cpp +++ b/src/serverget.cpp @@ -181,28 +181,48 @@ struct ServerGPRConnect : public server::ConnectOp virtual void connect(const Value& prototype) override final { + if(!prototype && _op!=RPC) + throw std::invalid_argument("Must provide prototype"); + + if(didConnect) + throw std::logic_error("Operation already connected (has a type)"); + auto serv = server.lock(); if(!serv) return; - serv->acceptor_loop.call([this, &prototype](){ + + auto op(this->op); + auto pvR(_pvRequest); + serv->acceptor_loop.dispatch([prototype, op, pvR](){ if(auto oper = op.lock()) { - if(oper->state!=ServerOp::Creating) + auto chan(oper->chan.lock()); + if(!chan) + return; + auto conn(chan->conn.lock()); + if(oper->state!=ServerOp::Creating || !conn) return; - if(!prototype && oper->cmd!=CMD_RPC) - throw std::invalid_argument("Must provide prototype"); - - if(oper->type) - throw std::logic_error("Operation already connected (has a type)"); + if(oper->type) { + // condition should be detected prior to dispatch, paranoia double-check here + log_err_printf(connsetup, "connect() from %s for %s: Operation already connected (has a type)", + conn->peerName.c_str(), chan->name.c_str()); + return; + } if(prototype) { oper->type = Value::Helper::type(prototype); - oper->pvMask = request2mask(oper->type.get(), _pvRequest); + try { + oper->pvMask = request2mask(oper->type.get(), pvR); + } catch(std::exception& e) { // eg. bad pvRequest mask + oper->doReply(Value(), e.what()); + return; + } } oper->doReply(Value(), std::string()); } }); + didConnect = true; } virtual void error(const std::string& msg) override final { @@ -260,6 +280,7 @@ struct ServerGPRConnect : public server::ConnectOp const std::weak_ptr server; const std::weak_ptr op; + bool didConnect = false; INST_COUNTER(ServerGPRConnect); }; diff --git a/src/serverintrospect.cpp b/src/serverintrospect.cpp index 1c49069..55c7f90 100644 --- a/src/serverintrospect.cpp +++ b/src/serverintrospect.cpp @@ -73,26 +73,27 @@ struct ServerIntrospectControl final : public server::ConnectOp virtual void connect(const Value& prototype) override final { - auto desc = Value::Helper::desc(prototype); - if(!desc) + if(!prototype) throw std::logic_error("Can't reply to GET_FIELD with Null prototype"); Status sts{Status::Ok}; - doReply(desc, sts); + doReply(prototype, sts); } virtual void error(const std::string &msg) override final { Status sts{Status::Error, msg}; - doReply(nullptr, sts); + doReply(Value(), sts); } - void doReply(const FieldDesc* type, const Status& sts) + void doReply(const Value& prototype, const Status& sts) { auto serv = server.lock(); if(!serv) return; // soft fail if already completed, canceled, disconnected, .... - serv->acceptor_loop.call([this, type, &sts](){ + auto op(this->op); + serv->acceptor_loop.dispatch([op, prototype, sts](){ + auto type = Value::Helper::desc(prototype); if(auto oper = op.lock()) oper->doReply(type, sts); });