Skip to content

rpc: a peer client is unreachable after it reconnects #761

Open

Description

@lionelhorn

After the server drops a peer client's connection and the client reconnects, calls to its peer id never settle, and calling registerAsPeer again fails with "Already registered as a peer".

import { DirectClient, rpc, RpcKernel, RpcKernelBaseConnection, RpcKernelSecurity } from '@deepkit/rpc';

class PeerSecurity extends RpcKernelSecurity {
    async isAllowedToSendToPeer() {
        return true;
    }
    async isAllowedToRegisterAsPeer() {
        return true;
    }
}

class WorkerController {
    @rpc.action()
    work(job: string): string {
        return job + ' done';
    }
}

const kernel = new RpcKernel([{ provide: RpcKernelSecurity, useClass: PeerSecurity, scope: 'rpc' }]);
const serverConnections: RpcKernelBaseConnection[] = [];
kernel.onConnection(connection => serverConnections.push(connection));

const worker = new DirectClient(kernel);
await worker.registerAsPeer('mail-worker');
worker.registerPeerController(WorkerController, 'worker');

const caller = new DirectClient(kernel);
const workerApi = caller.peer('mail-worker').controller<WorkerController>('worker');
console.log(await workerApi.work('send')); // 'send done'

// the server drops the worker's connection (restart, load balancer, network), the worker reconnects
serverConnections[0].close();
await new Promise(resolve => setTimeout(resolve, 20));
await worker.connect();

const afterOneSecond = new Promise(resolve => setTimeout(() => resolve('pending after 1 s'), 1000));
console.log(await Promise.race([workerApi.work('send'), afterOneSecond]));
// actual: 'pending after 1 s', should be: 'send done'
console.log(await worker.registerAsPeer('mail-worker').then(() => 'registered', (error: Error) => error.message));
// actual: 'Already registered as a peer', should be: 'registered'

Related: #760 (the peer id stays registered after the client disconnects).

Master 0bb9fba, Node 26.13.0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions