mirror of
https://github.com/logos-co/logos-protocol.git
synced 2026-08-30 13:31:12 +00:00
* fix: make isConnected() mean connected, and stop the log claiming it QRemoteObjectNode::connectToNode() returns false only when the URL SCHEME is unregistered -- it never contacts the peer. Our registry URLs are COMPUTED rather than discovered (logos_instance.h: local:logos_<module>_<instanceId>), so they are identical whether or not the module exists. Latching m_connected from that return therefore made isConnected() answer "yes" for modules that were never loaded, which made every `if (!client->isConnected()) return;` guard in the codebase DEAD CODE. Callers then paid a 20 s waitForSource per call, twice over, because the token handshake tries capability_module first. Measured in Basecamp with package_manager absent: ~417 s of blocked GUI thread on macOS and 361 s on Linux before the window appeared, and over 900 s under load. Not a Windows bug -- the Windows port merely exposed it. isConnected() now also requires a listener at the endpoint. For `local:` that is a direct socket / named-pipe probe, which costs microseconds precisely in the case that used to cost 20 seconds; any other scheme keeps its previous behaviour. Two logging changes, because the diagnostics cost more than the defect: "Successfully connected to registry" asserted a connection that often did not exist and sent three separate investigations to the wrong place -- it now says a connect attempt started and makes no claim about the peer. And requestObject warns BEFORE a doomed wait instead of going silent for 20 s and then reporting failure. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: let event subscriptions survive a module that is not reachable yet requestObject() answers "is the module there RIGHT NOW", and every subscriber in this codebase asks at the one moment the answer is no: a module's init(), a UI backend's onContextReady(), a QML view's Component.onCompleted. All of those run while the dependency's host process has been spawned but has not called listen() yet. The subscriber then gave up permanently -- lp_subscribe returned nullptr with no log at all, and callers turned that into a `false` the documented example discards. Method calls kept working through the same window because acquireCachedObject() reaches the replica by a path that never asks, so the symptom was "events are broken", not "the subscription never happened".1238316(isConnected() means connected) is what made this deterministic rather than lucky, and it must not be reverted -- it removed ~417 s (macOS) / 361 s (Linux) of blocked GUI thread at Basecamp startup. So the subscription becomes deferrable instead. - LogosTransportAsyncAcquire: a sibling interface (dynamic_cast, like LogosObjectErrorChannel) so LogosTransportConnection's installed vtable is unchanged. requestObjectWhenAvailable() registers interest and returns; it never blocks and never spins a nested event loop. - qt_remote implements it by acquiring a dynamic replica before the peer exists -- legal, free, and armed by the node's existing 250 ms reconnect loop, so it adds no polling. Delivery is deferred one event-loop turn because stateChanged fires from inside onClientRead (the refresh_balances re-entrancy SIGSEGV). - LogosAPIConsumer::onEventWhenAvailable() holds the pending subscriptions, arms them when the object appears, shares ONE handle per object (separate from the call cache, so a call re-acquiring a stale handle cannot silently kill a live subscription), and re-arms them after reconnect(). Unbounded in time on purpose -- a module can be installed mid-session -- but bounded in noise: one warning at 3 s, one at 60 s, a log line when it arms, and a loud abandon when the transport proves it impossible. - lp_subscribe routes through it, which fixes the same defect for every C++/Nim/Rust module and UI backend without touching qt-sdk or any generated code. tests/protocol/test_deferred_subscription.cpp pins all three layers, each with a published-first control so a red case cannot be a mis-wired fixture. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: close the remaining silent-failure holes in deferred event subscriptions The deferred-subscription registry from the previous commit fixed the reported defect, but review found six ways it could still lose a subscription without saying so — five in the registry itself, one in the plain transport's host — and every one of them lived in a cell with no test. All of its tests ran in Remote mode; three of the four transports had none at all. Registry (cpp/logos_api_consumer.cpp): * An already-present module was deferred to the first 250 ms tick on every transport without a deferred acquire, and every event emitted in that window was dropped. lp_subscribe used to attach synchronously and deliver them, so this relocated the silent event loss rather than removing it. startAcquire() now reports which of three answers the transport gave, and only an Unsupported answer takes the one synchronous requestObject() — which is also what keeps that call structurally away from qt_remote, whose requestObject() enters waitForSource()'s nested event loop even at timeout 0. Previously that invariant lived in a comment, and tick() could reach it whenever acquireDynamic() returned null. * reconnected() put every armed subscription back in the pending set but never restarted the timer, which takeMatching() had stopped when they armed. Since tick() is the sole driver of both the retry and the watchdog, a reconnect left the subscription dead AND silent — quieter than the "not connected" warning it replaced. * armAgainst() released a stale handle while entries were still attached to its event helper. Those entries stayed in m_armed, never fired again, and reported as healthy. They are now revived and re-armed against the new handle. * The retry timer ran forever at the 5 s cap with nothing to do. It now stops once every pending entry has an acquire in flight and has said everything it will say, and restarts when that changes. * A cancelled subscription had no way to leave the registry, so lp_unsubscribe left it holding the timer up and warning about a subscription nobody wanted. onEventWhenAvailable() now returns an id; cancelEventSubscription() and eventSubscriptionState() are its counterparts, and lp_unsubscribe uses them. Plain transport (cpp/implementations/plain/plain_transport_host.cpp): * onSubscribe() dropped a Subscribe for an object that was not published YET — which is exactly when consumers subscribe — and the consumer could not know, because requestObject() had already succeeded. Publishing also overwrote the sink table wholesale, so a republish took every subscriber down with it. The sinks now live in a table keyed independently of publication. Also adds lp_pending_subscriptions() to the C ABI. The Qt consumer has had this visibility all along and the C ABI had none, which is why a subscription that silently never armed was undetectable from Rust, Nim or a universal C++ module. tests/protocol/test_event_delivery_matrix.cpp pins the product rather than a sample of it: 3 transports x 2 provider kinds (Qt-native and universal/std, which reach the wire by different conversions) x 2 consumer paths (onEventWhenAvailable and lp_subscribe) x 6 timings, plus mock and the non-blocking guard. Every delivery case has a control that is green independently of these fixes. One thing that is NOT fixed and is now stated in the contract: arming is not retroactive and no transport buffers, so a module that emits a one-shot "ready" event synchronously inside its own init() can still be missed. That window is inherent to the transport — the blocking requestObject() this replaced had it too — but "subscriptions survive a late module" is not "no event can be missed". Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs: name the QtRO invariant the stale-handle revive rests on * test(events): state what the non-blocking guard can and cannot catch The acquireCount assertion catches a retry that polls qt_remote's blocking requestObject() in the ordinary case. It cannot reach the narrow one -- the poll is only reachable when the transport declines a deferred acquire while still reporting connected, which needs acquireDynamic() to return null and is not forcible from outside. That case is held shut by control flow instead, and saying so is better than leaving a reader to assume the test covers it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: make the async-acquire contract and lp_subscribe's return honest Both from review on #47, both real. The LogosTransportAsyncAcquire contract promised that a true return means onReady "WILL be invoked exactly once". It will not: RemoteTransportConnection parents every in-flight PendingAcquire to m_pendingAcquires, which is reset at the top of the destructor and rebuilt on reconnect, so an accepted request is cancelled silently with no callback whenever the connection it belongs to goes away. The contract now says AT MOST once, names both cancellation triggers, and states what a caller has to do about them — re-issue after a reconnect, or carry its own deadline. It also records that the layer above already does the first, which is why a subscription made through onEventWhenAvailable() survives something the raw transport call does not. That asymmetry is the reason to prefer the consumer API, and it was previously implicit. lp_subscribe returned a non-null lp_subscription even when onEventWhenAvailable refused and returned 0, leaving the caller with a handle that can never fire while the ABI documents NULL as the one signal that the arguments were refused. It now checks sub->id and returns nullptr. That second one is defensive rather than a live bug, and the code says so: the guard at the top of lp_subscribe already rejects an empty event name and a null callback, and lp_client_create rejects an empty target, so the three inputs that make onEventWhenAvailable() return 0 cannot all arrive there today. No test drives it. The two contracts simply have to agree, and one of them changing is how they would stop agreeing. 374/374 green. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: stop lp_unsubscribe deadlocking, without dereferencing a freed client lp_unsubscribe took ownerGuard->mutex and, while holding it, called cancelEventSubscription(), which marshals to the owner thread with a BLOCKING queued connection. The delivery callback lp_subscribe installs runs ON that thread and takes subGuard->mutex then clientGuard->mutex — and clientGuard IS ownerGuard, both assigned from client->guard. Lock-order inversion. It also hung outright once the owner's event loop had stopped, which is exactly when a language binding drops its subscription handle. The first attempt at this dropped the guard entirely and checked `alive` inside the posted lambda. That was a use-after-free: QMetaObject::invokeMethod dereferences the target (it reads object->thread()) before the lambda can run, and lp_client_destroy sets alive=false and deletes the client synchronously — so the check was unreachable on the exact ordering lp_subscription's own comment documents as supported. Proven rather than argued: with MallocScribble=1, a test that destroys the client before unsubscribing segfaulted 6/6 with the guard removed and passed 6/6 with it restored. So the guard is held across the POST and not across the cancel. Both halves are load-bearing, and the distinction is the whole fix: posting never waits on the owner thread, so holding the mutex across it cannot invert; only the blocking marshal ever had to move. Consequence, now stated in the ABI header: un-registration is EVENTUAL. The callback-will-not-fire guarantee stays synchronous and unconditional, but lp_pending_subscriptions() may still list a just-cancelled subscription until the owner thread runs, and if the client is destroyed first the cancellation never runs at all — correct, since the registry died with it. The matrix test now pumps for the drain instead of asserting it happened synchronously. 374/374 green. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: arm a subscription immediately when the module is already reachable Deferral introduced a narrower version of the loss it removed. The common consumer shape is a call followed by a subscription in the same function -- wallet-ui's backend calls get_chains() and subscribes on the next line, the tutorial's C++ UI backend does the same. Before deferral the generated Qt wrapper acquired synchronously, so the subscription was live before on() returned and an event emitted straight after was delivered. Holding it until the next event-loop turn silently drops that event. Measured on the generated-wrapper harness: 1/1 delivered pre-migration, 0/1 after, over 3 runs. LogosTransportAsyncAcquire gains tryAcquireNow(): hand back a handle ONLY if that costs nothing -- for qt_remote, a replica that is already Valid, which is exactly the state a prior call leaves behind since QtRO shares one replica implementation per object name on a node. It must never block, never spin a nested event loop and never wait on a peer; "not immediately available" is an answer and the caller falls back to the deferred path. Default returns nullptr, so a transport that cannot answer cheaply simply does not. Delivering inline here is safe for the reason the never-synchronous rule exists: that rule protects against re-entering the transport's READ stack from a stateChanged callback. tryAcquireNow runs on the subscriber's own stack. The new matrix case fires ONCE, synchronously, with no pumping in between -- re-firing would hide the exact gap under test -- and states the transport difference rather than papering over it. Subscription registration is local on qt_remote (attach to a held replica) and qt_local (connect an in-process signal), so delivery there must be instant. On plain it is a wire frame to the host, so instant delivery was never on offer and never was before this change either; that leg asserts it still arms and delivers. Also de-flaked EventDeliveryNonBlocking: its heartbeat COUNT over a fixed wall-clock window measures the machine, not the code. The gap assertion is the one that means something; the count is now only a floor. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: stop tryAcquireNow leaving a dangling facade in QtRO's connect liste9f82acintroduced a use-after-free. tryAcquireNow() acquired a dynamic replica and, when it was not already Valid, deleted it. That is not safe: QtRO shares one replica IMPLEMENTATION per object name per node, and while that implementation is still waiting for the source's metaobject it records every facade built on it as a RAW pointer in QConnectedReplicaImplementation::m_parentsNeedingConnect. ~QRemoteObjectReplica is an empty body, so destroying a facade never deregisters it, and the implementation dereferences the whole list when the class definition arrives. So each probe of an unreachable module left one dangling pointer behind. WHY IT HID. The first probe owns the only implementation and takes it down with itself, so a single subscription is harmless. It needs a second subscription whose implementation is pinned by an in-flight PendingAcquire before a freed facade can outlive its implementation. A consumer subscribing once sees nothing; the QML plugin shape -- a view registering every event it cares about up front -- dies. REPRODUCED, 4 runs of 4, serially as well as in parallel, in logos-view-module-runtime's existing suite (unchanged from master, and green there against this same protocol checkout): LogosQmlBridge: subscription accepted for "echo_module" :: "ev13" Received signal 10 (SIGBUS), code 1, for address 0x5a SIGBUS code 1 is BUS_ADRALN -- a misaligned atomic access on a garbage base read out of a recycled heap block, in the event loop rather than at the call site, which is why it reads as a mystery crash rather than as a subscription bug. PROVEN, before writing this fix, by commenting out that single `delete replica`: the same suite went 4 failures -> 6/6 with no other change. With this fix: 6/6. THE FIX IS TO PARK, NOT TO FREE. One probe per object name, parented to m_pendingAcquires -- which both the destructor and reconnect() already destroy BEFORE the node, so the implementations die in the same breath and freeing them there is safe. Ownership transfers out only when the replica reaches Valid, by which point the implementation is configured and is no longer holding the facade. It costs one idle replica per name until it goes Valid or the connection dies. AND REMOVE THE MULTIPLIER: beginAcquire() probed on EVERY add(), ahead of startAcquire() and therefore ahead of the m_acquiring one-acquire-per-object guard. tick() already applies that filter; beginAcquire() was the one caller that did not, which is what turned one probe per module into one per subscription. While an acquire is in flight its PendingAcquire already holds a replica and will arm every waiting entry at once, so the probe buys nothing there. Not QML-specific: lp_subscribe reaches the same entry point. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
521 lines
24 KiB
C++
521 lines
24 KiB
C++
#include "logos_api_client.h"
|
|
#include "logos_api_consumer.h"
|
|
#include "logos_object.h"
|
|
#include "logos_types.h"
|
|
#include "logos_json_convert.h"
|
|
#include "logos_thread_marshal.h"
|
|
#include "logos_rpc_status.h"
|
|
#include "token_manager.h"
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
#include <QJsonValue>
|
|
#include <QMetaObject>
|
|
#include <QMetaType>
|
|
#include <QPointer>
|
|
#include <string>
|
|
|
|
using logos::qvariantToNlohmann;
|
|
using logos::nlohmannArgsToQVariantList;
|
|
|
|
LogosAPIClient::LogosAPIClient(const QString& module_to_talk_to,
|
|
const QString& origin_module,
|
|
TokenManager* token_manager,
|
|
const LogosTransportConfig& target_transport,
|
|
const LogosTransportConfig& capability_transport,
|
|
QObject *parent)
|
|
: QObject(parent)
|
|
, m_consumer(new LogosAPIConsumer(module_to_talk_to, origin_module,
|
|
token_manager, target_transport, this))
|
|
, m_token_manager(token_manager)
|
|
, m_origin_module(origin_module)
|
|
// Pre-build the capability_module consumer once. We skip it for
|
|
// the capability_module client itself — the auto-`requestModule`
|
|
// path is gated by `objectName != "capability_module"` so we'd
|
|
// never use it, and constructing one would be a redundant
|
|
// self-connection. Init-list order matches the declaration order
|
|
// in the header — `m_capability_consumer` is appended at the end
|
|
// for ABI stability (see header comment).
|
|
, m_capability_consumer(module_to_talk_to == QStringLiteral("capability_module")
|
|
? nullptr
|
|
: new LogosAPIConsumer(QStringLiteral("capability_module"),
|
|
origin_module, token_manager,
|
|
capability_transport, this))
|
|
{
|
|
}
|
|
|
|
LogosAPIClient::LogosAPIClient(const QString& module_to_talk_to,
|
|
const QString& origin_module,
|
|
TokenManager* token_manager,
|
|
QObject *parent)
|
|
: LogosAPIClient(module_to_talk_to, origin_module, token_manager,
|
|
LogosTransportConfigGlobal::getDefault(),
|
|
LogosTransportConfigGlobal::getDefault(), parent)
|
|
{
|
|
}
|
|
|
|
LogosAPIClient::~LogosAPIClient()
|
|
{
|
|
}
|
|
|
|
LogosObject* LogosAPIClient::requestObject(const QString& objectName, Timeout timeout)
|
|
{
|
|
// Marshal to the owner thread: the replica is acquired and lives there.
|
|
return logos::runOnOwnerThread(this, [&]() -> LogosObject* {
|
|
return m_consumer->requestObject(objectName, timeout);
|
|
});
|
|
}
|
|
|
|
bool LogosAPIClient::isConnected() const
|
|
{
|
|
return m_consumer->isConnected();
|
|
}
|
|
|
|
QString LogosAPIClient::registryUrl() const
|
|
{
|
|
return m_consumer->registryUrl();
|
|
}
|
|
|
|
bool LogosAPIClient::reconnect()
|
|
{
|
|
return m_consumer->reconnect();
|
|
}
|
|
|
|
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
|
|
const QVariantList& args, Timeout timeout)
|
|
{
|
|
return invokeRemoteMethod(objectName, methodName, args, timeout, nullptr);
|
|
}
|
|
|
|
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
|
|
const QVariantList& args, Timeout timeout, logos::CallError* err)
|
|
{
|
|
if (err) err->clear();
|
|
// Marshal the whole operation (capability/token fetch + the call) onto the
|
|
// owner thread so a worker thread (e.g. an HTTP handler) can call other
|
|
// modules. Same-thread callers run directly. See logos_thread_marshal.h.
|
|
return logos::runOnOwnerThread(this, [&]() -> QVariant {
|
|
qDebug() << "LogosAPIClient: invoking remote method" << objectName << methodName << "args_count:" << args.size();
|
|
|
|
const bool eligible = objectName != QStringLiteral("capability_module") && m_capability_consumer;
|
|
|
|
QString token = getToken(objectName);
|
|
if (token.isEmpty() && eligible)
|
|
token = mintAndCacheToken(objectName); // first exchange (cached for later calls)
|
|
|
|
QVariant result = m_consumer->invokeRemoteMethod(token, objectName, methodName, args, timeout, err);
|
|
|
|
// Re-exchange on rejection, once. The provider rejected our (stale) token —
|
|
// drop it, mint a fresh one via capability_module, and retry the call. Gated
|
|
// on the explicit provider sentinel (never a plain empty result), so it can't
|
|
// loop, can't misfire on a legitimately-empty return, and never fires against
|
|
// an old provider (which returns a bare QVariant() we don't match). This also
|
|
// lazily recovers the common provider-reload case. See logos_rpc_status.h.
|
|
if (eligible && logos::isUnauthorizedSentinel(result)) {
|
|
qWarning() << "LogosAPIClient: token for" << objectName
|
|
<< "rejected by provider; re-exchanging and retrying once";
|
|
m_token_manager->removeToken(objectName);
|
|
const QString fresh = mintAndCacheToken(objectName);
|
|
if (!fresh.isEmpty())
|
|
result = m_consumer->invokeRemoteMethod(fresh, objectName, methodName, args, timeout, err);
|
|
}
|
|
|
|
// Never surface the sentinel to the typed wrapper. If we still hold it the
|
|
// retry failed (capability down / provider truly gone): collapse to today's
|
|
// empty result, and for NEW callers set a distinguishable CallError.
|
|
if (logos::isUnauthorizedSentinel(result)) {
|
|
if (err) {
|
|
err->code = "unauthorized";
|
|
err->message = "call to '" + objectName.toStdString()
|
|
+ "' rejected: token not recognized (re-exchange failed)";
|
|
err->origin = objectName.toStdString();
|
|
}
|
|
return QVariant();
|
|
}
|
|
return result;
|
|
});
|
|
}
|
|
|
|
QString LogosAPIClient::mintAndCacheToken(const QString& objectName)
|
|
{
|
|
qDebug() << "LogosAPIClient: calling requestModule for" << objectName;
|
|
const QString capabilityToken = getToken(QStringLiteral("capability_module"));
|
|
const QString token = QString::fromStdString(
|
|
m_capability_consumer->requestModule(capabilityToken.toStdString(),
|
|
m_origin_module.toStdString(),
|
|
objectName.toStdString()));
|
|
qDebug() << "LogosAPIClient: requestModule result for" << objectName << ":" << token;
|
|
// Cache the minted token so subsequent calls skip the handshake — closes the
|
|
// token-rotation race where overlapping requestModule calls mint fresh tokens
|
|
// that overwrite each other at the target (e.g. QtRO's sync wait reentering
|
|
// via a nested event loop).
|
|
if (!token.isEmpty())
|
|
m_token_manager->saveToken(objectName, token);
|
|
return token;
|
|
}
|
|
|
|
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg, Timeout timeout)
|
|
{
|
|
return invokeRemoteMethod(objectName, methodName, QVariantList() << arg, timeout);
|
|
}
|
|
|
|
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2, Timeout timeout)
|
|
{
|
|
return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2, timeout);
|
|
}
|
|
|
|
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, Timeout timeout)
|
|
{
|
|
return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3, timeout);
|
|
}
|
|
|
|
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2, const QVariant& arg3,
|
|
const QVariant& arg4, Timeout timeout)
|
|
{
|
|
return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4, timeout);
|
|
}
|
|
|
|
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2, const QVariant& arg3,
|
|
const QVariant& arg4, const QVariant& arg5, Timeout timeout)
|
|
{
|
|
return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4 << arg5, timeout);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QString& methodName,
|
|
const QVariantList& args, AsyncResultCallback callback,
|
|
Timeout timeout)
|
|
{
|
|
// Delegate to the CallError-aware overload; legacy callers just drop the
|
|
// error field. Keeps the handshake-coalescing logic single-sourced.
|
|
invokeRemoteMethodAsync(objectName, methodName, args,
|
|
[cb = std::move(callback)](QVariant r, const logos::CallError&) mutable {
|
|
if (cb) cb(std::move(r));
|
|
},
|
|
timeout);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QString& methodName,
|
|
const QVariantList& args, AsyncResultErrorCallback callback,
|
|
Timeout timeout)
|
|
{
|
|
// Public entry: grant one retry for the rejection-driven re-exchange.
|
|
invokeRemoteMethodAsyncImpl(objectName, methodName, args, std::move(callback), timeout, /*retriesLeft=*/1);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsyncImpl(const QString& objectName, const QString& methodName,
|
|
const QVariantList& args, AsyncResultErrorCallback callback,
|
|
Timeout timeout, int retriesLeft)
|
|
{
|
|
if (!callback) return;
|
|
|
|
// The async path acquires a replica too, so it must also run on the owner
|
|
// thread. Unlike the sync path we post non-blocking (QueuedConnection): the
|
|
// worker caller returns immediately and the result callback fires on the
|
|
// owner thread when the reply arrives. Preserve retriesLeft across the hop.
|
|
if (QThread::currentThread() != this->thread()) {
|
|
QMetaObject::invokeMethod(this,
|
|
[this, objectName, methodName, args,
|
|
callback = std::move(callback), timeout, retriesLeft]() mutable {
|
|
invokeRemoteMethodAsyncImpl(objectName, methodName, args,
|
|
std::move(callback), timeout, retriesLeft);
|
|
},
|
|
Qt::QueuedConnection);
|
|
return;
|
|
}
|
|
|
|
const bool eligible = objectName != QStringLiteral("capability_module") && m_capability_consumer;
|
|
|
|
// Wrap the user callback so a provider rejection sentinel triggers one
|
|
// re-exchange + retry, and the sentinel is never surfaced to the caller.
|
|
// Mirrors the sync path's retry in logos_api_client.cpp's invokeRemoteMethod.
|
|
QPointer<LogosAPIClient> selfGuard(this);
|
|
AsyncResultErrorCallback onResult =
|
|
[this, selfGuard, objectName, methodName, args, timeout, retriesLeft, cb = std::move(callback)]
|
|
(QVariant result, const logos::CallError& err) mutable {
|
|
if (!selfGuard) return; // client destroyed mid-flight: drop
|
|
if (retriesLeft > 0 && objectName != QStringLiteral("capability_module")
|
|
&& m_capability_consumer && logos::isUnauthorizedSentinel(result)) {
|
|
qWarning() << "LogosAPIClient: token for" << objectName
|
|
<< "rejected by provider (async); re-exchanging and retrying once";
|
|
m_token_manager->removeToken(objectName);
|
|
// Token is empty now → the re-entry coalesces the retry through the
|
|
// same m_pendingHandshakes machinery, so a burst of concurrent
|
|
// rejections doesn't restorm capability_module with N handshakes.
|
|
invokeRemoteMethodAsyncImpl(objectName, methodName, args,
|
|
std::move(cb), timeout, retriesLeft - 1);
|
|
return;
|
|
}
|
|
if (logos::isUnauthorizedSentinel(result)) {
|
|
logos::CallError e;
|
|
e.code = "unauthorized";
|
|
e.message = "call to '" + objectName.toStdString()
|
|
+ "' rejected: token not recognized (re-exchange failed)";
|
|
e.origin = objectName.toStdString();
|
|
cb(QVariant(), e);
|
|
return;
|
|
}
|
|
cb(std::move(result), err);
|
|
};
|
|
|
|
QString token = getToken(objectName);
|
|
|
|
if (token.isEmpty() && eligible) {
|
|
// Async-chain: dispatch the requestModule call asynchronously, and only
|
|
// fire the real method's invokeRemoteMethodAsync from its callback. The
|
|
// previous version called `requestModule` synchronously here, which made
|
|
// the "async" entry point block its caller for the full round-trip.
|
|
//
|
|
// COALESCE concurrent first-calls behind ONE handshake. A driver that
|
|
// fans out N async calls to an un-tokened target before any completes
|
|
// would otherwise fire N separate requestModule handshakes; each mints a
|
|
// distinct token and informs the target, and the later inform OVERWRITES
|
|
// the earlier token there (the target stores one token per caller). The
|
|
// already-dispatched calls then carry a superseded token and the target
|
|
// rejects them as unauthorized. So only the first caller starts the
|
|
// handshake; the rest queue and all drain with the single minted token.
|
|
// (The sync path can't hit this — it blocks per call, so handshakes
|
|
// never overlap.) m_pendingHandshakes is touched only on the owner
|
|
// thread, reached above, so no lock is needed.
|
|
m_pendingHandshakes[objectName].push_back(
|
|
[this, objectName, methodName, args, timeout, cb = std::move(onResult)]
|
|
(const QString& tok) mutable {
|
|
m_consumer->invokeRemoteMethodAsync(tok, objectName, methodName, args,
|
|
std::move(cb), timeout);
|
|
});
|
|
if (m_pendingHandshakes[objectName].size() > 1)
|
|
return; // a handshake for this target is already in flight
|
|
|
|
const QString capabilityToken = getToken("capability_module");
|
|
const QString origin = m_origin_module;
|
|
// Lifetime: capture the client through a QPointer guard. If it (and its
|
|
// QObject-parented consumers + the pending queue) is destroyed while the
|
|
// requestModule round-trip is in flight, the guard goes null and we drop
|
|
// the queued continuations instead of dereferencing dangling memory.
|
|
QPointer<LogosAPIClient> self(this);
|
|
m_capability_consumer->invokeRemoteMethodAsync(
|
|
capabilityToken,
|
|
QStringLiteral("capability_module"),
|
|
QStringLiteral("requestModule"),
|
|
QVariantList() << origin << objectName,
|
|
[self, objectName](const QVariant& tokenResult) mutable {
|
|
if (!self) return; // client destroyed mid-flight
|
|
const QString tok = tokenResult.toString();
|
|
// Cache the minted token before draining so future calls skip the handshake — m_pendingHandshakes only coalesces the first burst, the cache stops a second burst from racing the same rotation.
|
|
if (!tok.isEmpty()) self->m_token_manager->saveToken(objectName, tok);
|
|
// Drain every continuation queued for this target with the one
|
|
// minted token — the target was informed of exactly this token.
|
|
// An empty tok (handshake failed) still flows through: the
|
|
// consumer call is then rejected and each callback fires with an
|
|
// invalid QVariant, so callers never hang.
|
|
auto it = self->m_pendingHandshakes.find(objectName);
|
|
if (it == self->m_pendingHandshakes.end()) return;
|
|
std::vector<std::function<void(const QString&)>> calls = std::move(it.value());
|
|
self->m_pendingHandshakes.erase(it);
|
|
for (auto& c : calls) c(tok);
|
|
},
|
|
timeout);
|
|
return;
|
|
}
|
|
|
|
m_consumer->invokeRemoteMethodAsync(token, objectName, methodName, args, std::move(onResult), timeout);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg, AsyncResultCallback callback,
|
|
Timeout timeout)
|
|
{
|
|
invokeRemoteMethodAsync(objectName, methodName, QVariantList() << arg, std::move(callback), timeout);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2,
|
|
AsyncResultCallback callback, Timeout timeout)
|
|
{
|
|
invokeRemoteMethodAsync(objectName, methodName, QVariantList() << arg1 << arg2, std::move(callback), timeout);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2, const QVariant& arg3,
|
|
AsyncResultCallback callback, Timeout timeout)
|
|
{
|
|
invokeRemoteMethodAsync(objectName, methodName, QVariantList() << arg1 << arg2 << arg3, std::move(callback), timeout);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2, const QVariant& arg3,
|
|
const QVariant& arg4, AsyncResultCallback callback,
|
|
Timeout timeout)
|
|
{
|
|
invokeRemoteMethodAsync(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4, std::move(callback), timeout);
|
|
}
|
|
|
|
void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QString& methodName,
|
|
const QVariant& arg1, const QVariant& arg2, const QVariant& arg3,
|
|
const QVariant& arg4, const QVariant& arg5,
|
|
AsyncResultCallback callback, Timeout timeout)
|
|
{
|
|
invokeRemoteMethodAsync(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4 << arg5, std::move(callback), timeout);
|
|
}
|
|
|
|
void LogosAPIClient::onEvent(LogosObject* originObject, const QString& eventName, std::function<void(const QString&, const QVariantList&)> callback)
|
|
{
|
|
// Marshal to the owner thread: event registration touches the replica.
|
|
logos::runOnOwnerThread(this, [&]() {
|
|
m_consumer->onEvent(originObject, eventName, std::move(callback));
|
|
});
|
|
}
|
|
|
|
quint64 LogosAPIClient::onEventWhenAvailable(const QString& objectName, const QString& eventName,
|
|
std::function<void(const QString&, const QVariantList&)> callback,
|
|
std::function<void(bool)> onArmed)
|
|
{
|
|
// Marshal to the owner thread for the same reason onEvent() does: the
|
|
// registry touches (and later arms against) a QtRO replica, which only
|
|
// works on the thread that created the node.
|
|
return logos::runOnOwnerThread(this, [&]() -> quint64 {
|
|
return m_consumer->onEventWhenAvailable(objectName, eventName,
|
|
std::move(callback), std::move(onArmed));
|
|
});
|
|
}
|
|
|
|
bool LogosAPIClient::cancelEventSubscription(quint64 subscriptionId)
|
|
{
|
|
return logos::runOnOwnerThread(this, [&]() -> bool {
|
|
return m_consumer->cancelEventSubscription(subscriptionId);
|
|
});
|
|
}
|
|
|
|
LogosSubscriptionState LogosAPIClient::eventSubscriptionState(quint64 subscriptionId) const
|
|
{
|
|
return logos::runOnOwnerThread(const_cast<LogosAPIClient*>(this),
|
|
[&]() -> LogosSubscriptionState {
|
|
return m_consumer->eventSubscriptionState(subscriptionId);
|
|
});
|
|
}
|
|
|
|
QStringList LogosAPIClient::pendingEventSubscriptions() const
|
|
{
|
|
return logos::runOnOwnerThread(const_cast<LogosAPIClient*>(this), [&]() -> QStringList {
|
|
return m_consumer->pendingSubscriptions();
|
|
});
|
|
}
|
|
|
|
void LogosAPIClient::onEventResponse(LogosObject* object, const QString& eventName, const QVariantList& data)
|
|
{
|
|
qDebug() << "[LogosObject] LogosAPIClient::onEventResponse" << eventName << "-> LogosObject::emitEvent";
|
|
|
|
if (eventName.isEmpty()) {
|
|
qWarning() << "LogosAPIClient: Event name cannot be empty";
|
|
return;
|
|
}
|
|
|
|
if (!object) {
|
|
qWarning() << "LogosAPIClient: Cannot emit event on null object";
|
|
return;
|
|
}
|
|
|
|
object->emitEvent(eventName, data);
|
|
}
|
|
|
|
void LogosAPIClient::onEventResponse(QObject* object, const QString& eventName, const QVariantList& data)
|
|
{
|
|
qDebug() << "[LogosObject] LogosAPIClient::onEventResponse (QObject* compat)" << eventName;
|
|
|
|
if (eventName.isEmpty()) {
|
|
qWarning() << "LogosAPIClient: Event name cannot be empty";
|
|
return;
|
|
}
|
|
|
|
if (!object) {
|
|
qWarning() << "LogosAPIClient: Cannot emit event on null QObject";
|
|
return;
|
|
}
|
|
|
|
QMetaObject::invokeMethod(object, "eventResponse",
|
|
Qt::DirectConnection,
|
|
Q_ARG(QString, eventName),
|
|
Q_ARG(QVariantList, data));
|
|
}
|
|
|
|
bool LogosAPIClient::informModuleToken(const QString& authToken, const QString& moduleName, const QString& token)
|
|
{
|
|
return m_consumer->informModuleToken(authToken, moduleName, token);
|
|
}
|
|
|
|
bool LogosAPIClient::informModuleToken(const std::string& authToken, const std::string& moduleName, const std::string& token)
|
|
{
|
|
return informModuleToken(QString::fromStdString(authToken),
|
|
QString::fromStdString(moduleName),
|
|
QString::fromStdString(token));
|
|
}
|
|
|
|
bool LogosAPIClient::informModuleToken_module(const QString& authToken, const QString& originModule, const QString& moduleName, const QString& token, int timeoutMs)
|
|
{
|
|
// Marshal to the owner thread, exactly as requestObject/invokeRemoteMethod do.
|
|
// This path now goes through acquireCachedObject, so it reads and mutates
|
|
// m_objectCache — declared single-threaded, and holding thread-affine QtRO
|
|
// handles. Before the handshake surface existed this method used an
|
|
// uncached requestObject + release(), so it touched no shared state; routing
|
|
// it onto the cache is what made the missing marshal reachable.
|
|
//
|
|
// (LogosAPIClient::informModuleToken — the 3-arg form above — has the same
|
|
// missing marshal, but it still uses an uncached handle and predates this
|
|
// change, so it is left alone rather than widened into this fix.)
|
|
return logos::runOnOwnerThread(this, [&]() -> bool {
|
|
return m_consumer->informModuleToken_module(authToken, originModule, moduleName, token, timeoutMs);
|
|
});
|
|
}
|
|
|
|
TokenManager* LogosAPIClient::getTokenManager() const
|
|
{
|
|
return m_token_manager;
|
|
}
|
|
|
|
QString LogosAPIClient::getToken(const QString& module_name)
|
|
{
|
|
qDebug() << "LogosAPIClient: getToken for module:" << module_name;
|
|
|
|
QString token = m_token_manager->getToken(module_name);
|
|
if (!token.isEmpty()) {
|
|
qDebug() << "LogosAPIClient: Found token for module:" << module_name;
|
|
return token;
|
|
}
|
|
|
|
qDebug() << "LogosAPIClient: No token found for module:" << module_name;
|
|
return "";
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// nlohmann::json overloads
|
|
// ---------------------------------------------------------------------------
|
|
|
|
nlohmann::json LogosAPIClient::invokeRemoteMethod(const std::string& objectName,
|
|
const std::string& methodName,
|
|
const nlohmann::json& args,
|
|
Timeout timeout)
|
|
{
|
|
QVariantList qArgs = nlohmannArgsToQVariantList(args);
|
|
QVariant result = invokeRemoteMethod(
|
|
QString::fromStdString(objectName),
|
|
QString::fromStdString(methodName),
|
|
qArgs, timeout);
|
|
return qvariantToNlohmann(result);
|
|
}
|
|
|
|
void LogosAPIClient::onEvent(LogosObject* originObject, const std::string& eventName,
|
|
std::function<void(const std::string&, const nlohmann::json&)> callback)
|
|
{
|
|
onEvent(originObject, QString::fromStdString(eventName),
|
|
[cb = std::move(callback)](const QString& name, const QVariantList& data) {
|
|
nlohmann::json jData = nlohmann::json::array();
|
|
for (const QVariant& v : data)
|
|
jData.push_back(qvariantToNlohmann(v));
|
|
cb(name.toStdString(), jData);
|
|
});
|
|
}
|