Marshal inter-module calls to the owner thread (#79)

* Marshal inter-module calls to the owner thread

Logos inter-module calls go over Qt Remote Objects, whose replicas only
work on the thread that created them (the module's main/event-loop thread).
A module that makes calls from a worker thread — e.g. an embedded HTTP
server serving /metrics — would otherwise hang on replica acquisition.

Make LogosAPIClient transparently marshal to its owner thread when called
off-thread (guarded so same-thread calls run directly with no overhead):
- LogosAPI::getClient creates the client/consumer/replicas on the owner thread
- LogosAPIClient::invokeRemoteMethod / requestObject / onEvent run there too

New header logos_thread_marshal.h (runOnOwnerThread). No new data members —
ABI-safe for statically-linked plugins.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Add regression test for worker-thread inter-module calls

A provider records the thread its method runs on; a consumer calls it from
a worker thread via LogosAPIClient::invokeRemoteMethod. The call must execute
on the owner (main/event-loop) thread, not the worker thread.

Fails without the marshaling change (the call runs on the worker thread —
0x..d80d0 vs owner 0x..c53e0, "executed on the worker thread instead of the
owner thread"); passes with it (511/511).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Address review: async marshaling, helper constraints, test ownership

- invokeRemoteMethodAsync now also marshals to the owner thread (non-blocking
  QueuedConnection) — the async path acquires a replica too, so calling it from
  a worker thread previously re-introduced the off-thread bug.
- runOnOwnerThread: document the return-type constraints (void or
  default-constructible, non-reference) and static_assert against references.
- test: declare the provider before its LogosAPI so the ModuleProxy (which
  holds a raw pointer to it) is torn down first — removes the leak and the
  inaccurate comment.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* doctest: call a module from a worker thread (HTTP server)

Adds cpp-sdk-worker-thread-http.test.yaml: builds a sensor_module callee and
an http_module caller that embeds a libmicrohttpd server, runs them in
logoscore, starts the server, and curls it. The HTTP handler calls
sensor_module.readTemperature() from the server's worker thread — which only
works because the SDK marshals the cross-module call onto the module's owner
thread. The module stays pure C++.

Wired into doctests/run.sh and the doctests CI workflow. Validated locally
(23/23 steps pass): `curl` returns `temperature 42`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dario Lipicar
2026-06-08 16:47:24 -03:00
committed by GitHub
co-authored by Claude Opus 4.8
parent d142262436
commit 40e7631402
9 changed files with 1386 additions and 3 deletions
+29 -2
View File
@@ -4,6 +4,7 @@
#include "logos_object.h"
#include "logos_types.h"
#include "logos_json_convert.h"
#include "logos_thread_marshal.h"
#include "token_manager.h"
#include <QJsonDocument>
#include <QJsonObject>
@@ -59,7 +60,10 @@ LogosAPIClient::~LogosAPIClient()
LogosObject* LogosAPIClient::requestObject(const QString& objectName, Timeout timeout)
{
return m_consumer->requestObject(objectName, 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
@@ -80,6 +84,10 @@ bool LogosAPIClient::reconnect()
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
const QVariantList& args, Timeout timeout)
{
// 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();
QString token = getToken(objectName);
@@ -95,6 +103,7 @@ QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QSt
}
return m_consumer->invokeRemoteMethod(token, objectName, methodName, args, timeout);
});
}
QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName,
@@ -135,6 +144,21 @@ void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QS
{
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.
if (QThread::currentThread() != this->thread()) {
QMetaObject::invokeMethod(this,
[this, objectName, methodName, args,
callback = std::move(callback), timeout]() mutable {
invokeRemoteMethodAsync(objectName, methodName, args,
std::move(callback), timeout);
},
Qt::QueuedConnection);
return;
}
QString token = getToken(objectName);
if (token.isEmpty() && objectName != "capability_module" && m_capability_consumer) {
@@ -222,7 +246,10 @@ void LogosAPIClient::invokeRemoteMethodAsync(const QString& objectName, const QS
void LogosAPIClient::onEvent(LogosObject* originObject, const QString& eventName, std::function<void(const QString&, const QVariantList&)> callback)
{
m_consumer->onEvent(originObject, eventName, std::move(callback));
// Marshal to the owner thread: event registration touches the replica.
logos::runOnOwnerThread(this, [&]() {
m_consumer->onEvent(originObject, eventName, std::move(callback));
});
}
void LogosAPIClient::onEventResponse(LogosObject* object, const QString& eventName, const QVariantList& data)