Skip to content

Commit

Permalink
Fix AlreadyDestroyedError not being actually thrown (#438)
Browse files Browse the repository at this point in the history
* Fix AlreadyDestroyedError not being actually thrown

* PR link

* Fix sendJsonRpc
  • Loading branch information
tomaka authored Apr 19, 2023
1 parent bfe29cf commit e92d1b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`. ([#438](https://github.com/smol-dot/smoldot/pull/438))

## 1.0.2 - 2023-04-12

### Changed
Expand Down
26 changes: 13 additions & 13 deletions wasm-node/javascript/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chain, number> = new WeakMap();
const chainIds: WeakMap<Chain, number> = 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.
Expand All @@ -411,8 +411,8 @@ export function start(options: ClientOptions, platformBindings: PlatformBindings

return {
addChain: async (options: AddChainOptions): Promise<Chain> => {
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.
Expand Down Expand Up @@ -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)
Expand All @@ -455,17 +455,17 @@ 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)
return Promise.reject(new JsonRpcDisabledError());
return instance.nextJsonRpcResponse(chainId);
},
remove: () => {
if (alreadyDestroyedError)
throw alreadyDestroyedError;
if (alreadyDestroyedError.value)
throw alreadyDestroyedError.value;
if (wasDestroyed.destroyed)
throw new AlreadyDestroyedError();
wasDestroyed.destroyed = true;
Expand All @@ -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()
}
}
Expand Down

0 comments on commit e92d1b0

Please sign in to comment.