Skip to content

Agent Mesh

sdk/js: an aborted top-up tries no router it has not dialed yet

Saurav147baf91e7e2
diff --git a/sdk/js/src/session.test.ts b/sdk/js/src/session.test.ts
index fb6204b9..153728b8 100644
--- a/sdk/js/src/session.test.ts
+++ b/sdk/js/src/session.test.ts
@@ -668,6 +668,43 @@ test("a join bounded by its signal says when the control plane never answered",
}
});
+test("a top-up whose signal has aborted tries no router and shuns none", async () => {
+ const second = await createLibp2p({
+ addresses: { listen: ["/ip4/127.0.0.1/tcp/0"] },
+ transports: [tcp()],
+ connectionEncrypters: [tls()],
+ streamMuxers: [yamux()],
+ services: { identify: identify(), relay: circuitRelayServer() },
+ connectionManager: { inboundConnectionThreshold: Infinity },
+ });
+ const secondId = second.peerId.toString();
+ const secondBiscuit = mint(secondId, ROLE_ROUTER);
+ await second.handle(AUTH_PROTOCOL, authStreamHandler({ ownBiscuit: () => secondBiscuit, trustedKeys: () => [cpKey] }));
+ const secondAddr = (second.getMultiaddrs()[0] as ReturnType<typeof multiaddr>).toString();
+ const listed = [routerAddr];
+ try {
+ const mesh = await AgentMesh.enroll({ controlPlaneUrl: "http://127.0.0.1:1", bootstrapToken: "sbt", fetch: fakeControlPlane(listed) });
+ const session = await mesh.join({ refreshLeadMs: 0, reserveRelay: false, controlPlaneSyncIntervalMs: 0 });
+ try {
+ // The control plane now lists a router the session has not tried.
+ listed.push(secondAddr);
+ await mesh.syncControlPlane();
+
+ const failures = await session.topUpRouters(AbortSignal.abort(new Error("join over")));
+ assert.deepEqual(failures, [], "an aborted top-up reports no router it did not try");
+ assert.equal(session.shunnedRouters.has(secondId), false, `${secondId} was shunned by a top-up that never tried it`);
+
+ // The router was eligible all along: the same top-up without the signal holds it.
+ assert.deepEqual(await session.topUpRouters(), []);
+ assert.ok(session.isHeldRouter(secondId), `held routers ${session.routers.map((r) => r.peerId).join(",")}, want ${secondId} among them`);
+ } finally {
+ await session.close();
+ }
+ } finally {
+ await second.stop();
+ }
+});
+
test("a join without a signal of its own still has a deadline", async () => {
const given = AbortSignal.timeout(60_000);
const own = joinDeadline(given, 20);
diff --git a/sdk/js/src/session.ts b/sdk/js/src/session.ts
index d7476c03..cdafda10 100644
--- a/sdk/js/src/session.ts
+++ b/sdk/js/src/session.ts
@@ -691,6 +691,8 @@ export class MeshSession {
*/
async topUpRouters(signal?: AbortSignal): Promise<string[]> {
const failures: string[] = [];
+ // A function, not a narrowed value: the signal aborts across the awaits below.
+ const aborted = () => signal?.aborted === true;
const want = this.#wantRouters();
const candidates = this.#candidatesForHolding();
const tried = new Set<string>();
@@ -708,6 +710,11 @@ export class MeshSession {
}
let progress = false;
for (const cand of picks) {
+ // A router not dialed yet is not tried under a signal that has
+ // already aborted: it would fail at once and be shunned for nothing.
+ if (aborted()) {
+ return failures;
+ }
tried.add(cand.peerId);
// Each step has a timeout of its own; the failure names the one the
// router was in, so a join that hangs says where.
@@ -728,7 +735,7 @@ export class MeshSession {
this.#shunRouter(cand.peerId, ROUTER_SHUN_DURATION_MS);
await this.node.hangUp(peerIdFromString(cand.peerId)).catch(() => {});
failures.push(`${cand.peerId}: ${step}: ${errorText(err)}`);
- if (signal?.aborted === true) {
+ if (aborted()) {
return failures;
}
}