From dc5148c856a1744cc93c84c1e206829be901ff34 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sat, 4 Mar 2023 18:59:55 -0800 Subject: [PATCH] rpc: dont throw on oneway methods even if the peer is closed --- server/src/rpc.ts | 15 ++++++++++----- server/src/threading.ts | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/server/src/rpc.ts b/server/src/rpc.ts index b31293222..630e3f276 100644 --- a/server/src/rpc.ts +++ b/server/src/rpc.ts @@ -159,13 +159,18 @@ class RpcProxy implements PrimitiveProxyHandler { } apply(target: any, thisArg: any, argArray?: any): any { - if (Object.isFrozen(this.peer.pendingResults)) - return Promise.reject(new RPCResultError(this.peer, 'RpcPeer has been killed')); + const method = target() || null; + const oneway = this.proxyOneWayMethods?.includes?.(method); + + if (Object.isFrozen(this.peer.pendingResults)) { + if (oneway) + return Promise.resolve(); + return Promise.reject(new RPCResultError(this.peer, 'RpcPeer has been killed (apply) ' + target())); + } // rpc objects can be functions. if the function is a oneway method, // it will have a null in the oneway method list. this is because // undefined is not JSON serializable. - const method = target() || null; const args: any[] = []; const serializationContext: any = {}; for (const arg of (argArray || [])) { @@ -180,7 +185,7 @@ class RpcProxy implements PrimitiveProxyHandler { method, }; - if (this.proxyOneWayMethods?.includes?.(method)) { + if (oneway) { rpcApply.oneway = true; // a oneway callable object doesn't need to be in the JSON payload. if (method === null) @@ -390,7 +395,7 @@ export class RpcPeer { createPendingResult(cb: (id: string, reject: (e: Error) => void) => void): Promise { if (Object.isFrozen(this.pendingResults)) - return Promise.reject(new RPCResultError(this, 'RpcPeer has been killed')); + return Promise.reject(new RPCResultError(this, 'RpcPeer has been killed (createPendingResult)')); const promise = new Promise((resolve, reject) => { const id = (this.idCounter++).toString(); diff --git a/server/src/threading.ts b/server/src/threading.ts index b8415dfd1..17caa0580 100644 --- a/server/src/threading.ts +++ b/server/src/threading.ts @@ -1,3 +1,5 @@ +// WARNING: threading.ts does not work because RpcPeer transpilation does not include readonly static properties in the class definition + import worker_threads from 'worker_threads'; import { getEvalSource, RpcPeer } from './rpc'; import v8 from 'v8';