From b501f8b1a34e57ec9597ffd9b56aaf869e93bc5e Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Tue, 18 Apr 2023 10:41:58 +0200 Subject: [PATCH 1/3] Fix AlreadyDestroyedError not being actually thrown --- wasm-node/CHANGELOG.md | 4 ++++ wasm-node/javascript/src/client.ts | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/wasm-node/CHANGELOG.md b/wasm-node/CHANGELOG.md index 0861686df2..45f43ca988 100644 --- a/wasm-node/CHANGELOG.md +++ b/wasm-node/CHANGELOG.md @@ -7,6 +7,10 @@ - As NodeJS v14 reaches its end of life on April 30th 2023, the minimum NodeJS version required to run smoldot is now v16. The smoldot Wasm binary now has SIMD enabled, meaning that the minimum Deno version required to run smoldot is now v1.9. - When receiving an identify request through the libp2p protocol, smoldot now sends back `smoldot-light-wasm vX.X.X` (with proper version numbers) as its agent name and version, instead of previously just `smoldot`. ([#417](https://github.com/smol-dot/smoldot/pull/417)) +### Fixed + +- Fix `AlreadyDestroyedError` not being properly thrown if a function is called after `terminate()`. + ## 1.0.2 - 2023-04-12 ### Changed diff --git a/wasm-node/javascript/src/client.ts b/wasm-node/javascript/src/client.ts index 7b3aa10e14..c0005cf6a5 100644 --- a/wasm-node/javascript/src/client.ts +++ b/wasm-node/javascript/src/client.ts @@ -391,11 +391,11 @@ export function start(options: ClientOptions, platformBindings: PlatformBindings // For each chain object returned by `addChain`, the associated internal chain id. // // Immediately cleared when `remove()` is called on a chain. - let chainIds: WeakMap = new WeakMap(); + const chainIds: WeakMap = new WeakMap(); // If `Client.terminate()̀ is called, this error is set to a value. // All the functions of the public API check if this contains a value. - let alreadyDestroyedError: null | AlreadyDestroyedError = null; + const alreadyDestroyedError: { value: null | AlreadyDestroyedError } = { value: null }; const instance = startInstance({ // Maximum level of log entries sent by the client. @@ -411,8 +411,8 @@ export function start(options: ClientOptions, platformBindings: PlatformBindings return { addChain: async (options: AddChainOptions): Promise => { - if (alreadyDestroyedError) - throw alreadyDestroyedError; + if (alreadyDestroyedError.value) + throw alreadyDestroyedError.value; // Passing a JSON object for the chain spec is an easy mistake, so we provide a more // readable error. @@ -455,8 +455,8 @@ export function start(options: ClientOptions, platformBindings: PlatformBindings instance.request(request, chainId); }, nextJsonRpcResponse: () => { - if (alreadyDestroyedError) - return Promise.reject(alreadyDestroyedError); + if (alreadyDestroyedError.value) + return Promise.reject(alreadyDestroyedError.value); if (wasDestroyed.destroyed) return Promise.reject(new AlreadyDestroyedError()); if (options.disableJsonRpc) @@ -464,8 +464,8 @@ export function start(options: ClientOptions, platformBindings: PlatformBindings return instance.nextJsonRpcResponse(chainId); }, remove: () => { - if (alreadyDestroyedError) - throw alreadyDestroyedError; + if (alreadyDestroyedError.value) + throw alreadyDestroyedError.value; if (wasDestroyed.destroyed) throw new AlreadyDestroyedError(); wasDestroyed.destroyed = true; @@ -479,9 +479,9 @@ export function start(options: ClientOptions, platformBindings: PlatformBindings return newChain; }, terminate: async () => { - if (alreadyDestroyedError) - throw alreadyDestroyedError - alreadyDestroyedError = new AlreadyDestroyedError(); + if (alreadyDestroyedError.value) + throw alreadyDestroyedError.value + alreadyDestroyedError.value = new AlreadyDestroyedError(); instance.startShutdown() } } From b6d2a556b447580e9fe0aac8bca60e5ca08a8b12 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Tue, 18 Apr 2023 10:42:22 +0200 Subject: [PATCH 2/3] PR link --- wasm-node/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasm-node/CHANGELOG.md b/wasm-node/CHANGELOG.md index 45f43ca988..1b2d570cb5 100644 --- a/wasm-node/CHANGELOG.md +++ b/wasm-node/CHANGELOG.md @@ -9,7 +9,7 @@ ### Fixed -- Fix `AlreadyDestroyedError` not being properly thrown if a function is called after `terminate()`. +- Fix `AlreadyDestroyedError` not being properly thrown if a function is called after `terminate()`. ([#438](https://github.com/smol-dot/smoldot/pull/438)) ## 1.0.2 - 2023-04-12 From eca48c47b4d91f329531c734208759b3e503ac32 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Tue, 18 Apr 2023 11:24:06 +0200 Subject: [PATCH 3/3] Fix sendJsonRpc --- wasm-node/javascript/src/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wasm-node/javascript/src/client.ts b/wasm-node/javascript/src/client.ts index c0005cf6a5..eaddbbbc41 100644 --- a/wasm-node/javascript/src/client.ts +++ b/wasm-node/javascript/src/client.ts @@ -443,8 +443,8 @@ export function start(options: ClientOptions, platformBindings: PlatformBindings // Resolve the promise that `addChain` returned to the user. const newChain: Chain = { sendJsonRpc: (request) => { - if (alreadyDestroyedError) - throw alreadyDestroyedError; + if (alreadyDestroyedError.value) + throw alreadyDestroyedError.value; if (wasDestroyed.destroyed) throw new AlreadyDestroyedError(); if (options.disableJsonRpc)