Files
llama.cpp/tests/test-rpc-unreachable.cpp
T
figkcoci-debugandfigkcoci-debug 6122b566b0 ggml-rpc : fail gracefully on unreachable endpoints and failed handshakes (#30415)
* ggml-rpc: fail gracefully on unreachable endpoints and failed handshakes

rpc_dispatcher::start() aborted the whole process (GGML_ABORT -> SIGABRT) when
the endpoint could not be parsed, the transport failed to initialise, the socket
could not connect, or the HELLO handshake failed. A typo in --rpc or a
temporarily down node killed the host application, and the reason went to a log
stream the host may never see.

Make the connect-time failure reportable instead of fatal: start() returns bool
(LOG_ERROR + false; the dispatcher thread only starts after a successful
handshake), negotiate_hello() returns false on a half-open socket,
get_dispatcher() returns nullptr and does not cache failed attempts, and every
consumer treats nullptr as "endpoint did not answer" with the conservative
behaviour of its domain (allocation -> nullptr, alloc-size -> lower bound,
device memory -> 0/0, events -> no-op, device count -> 0, and add_server()
already maps 0 devices to nullptr).

Mid-stream failures keep the existing RPC_STATUS_ASSERT by design (fail fast
against silent corruption) and are intentionally out of scope.

Field motivation: on Android (llama.rn + phone RPC cluster) an unreachable
worker crashed the app; the crash buffer showed
abort <- ggml_abort <- rpc_dispatcher::start <- ggml_backend_rpc_add_server.

* tests : add a regression test for unreachable rpc endpoints

Calls ggml_backend_rpc_add_server with a socket that nothing listens on and with a
malformed endpoint, and expects nullptr from both. Without the fix in this branch
the first call aborts the process, so the test cannot even fail cleanly.

* ggml-rpc remove low-value comments

* tests : drop the internal device-count check after the rpc registry refactor

---------

Co-authored-by: figkcoci-debug <figkcoci-debug@users.noreply.github.com>
2026-11-10 09:40:35 +02:00

34 lines
1.1 KiB
C++

#include "ggml-backend.h"
#include "ggml-rpc.h"
#include "ggml.h"
#include <cstdio>
// Regression test: an unreachable or malformed RPC endpoint must be reported as an
// error instead of aborting the calling process.
//
// Before the fix, ggml_backend_rpc_add_server() called GGML_ABORT (SIGABRT) when the
// endpoint could not be parsed, the transport failed to initialize, the socket could
// not connect, or the HELLO handshake failed - so a typo in an endpoint or a node
// that was still booting killed the whole application, and this test would die
// instead of failing cleanly.
//
// After the fix it returns nullptr.
//
// ref: https://github.com/ggml-org/llama.cpp/pull/30415
int main(void) {
ggml_backend_load_all();
// 1) nothing listens on this port: the connect must fail, not abort
ggml_backend_reg_t reg = ggml_backend_rpc_add_server("127.0.0.1:1");
GGML_ASSERT(reg == nullptr);
// 2) a malformed endpoint must take the parse-failure path and also return nullptr
reg = ggml_backend_rpc_add_server("this-is-not-an-endpoint");
GGML_ASSERT(reg == nullptr);
printf("ok\n");
return 0;
}