Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing a chain causes smoldot to crash/panic #1024

Closed
kratico opened this issue Aug 11, 2023 · 0 comments · Fixed by #1025
Closed

Removing a chain causes smoldot to crash/panic #1024

kratico opened this issue Aug 11, 2023 · 0 comments · Fixed by #1025

Comments

@kratico
Copy link

kratico commented Aug 11, 2023

In NodeJS (or in a browser), the smoldot client crashes if a chain is removed while awaiting for chain.nextJsonRpcResponse().

The issue is reproduced in this stackblitz with the following code

import * as smoldot from 'smoldot';
import fs from 'node:fs';

// Load a string chain specification.
const chainSpec = fs.readFileSync('./westend.json', 'utf8');

// A single client can be used to initialize multiple chains.
const client = smoldot.start();

const chain = await client.addChain({ chainSpec });

chain.sendJsonRpc(
  '{"jsonrpc":"2.0","id":1,"method":"system_name","params":[]}'
);

(async () => {
  while (true) {
    // Wait for a JSON-RPC response to come back. This is typically done in a loop in the background.
    console.log({ jsonRpcResponse: await chain.nextJsonRpcResponse() });
  }
})();

setTimeout(() => {
  console.log('will remove chain');
  // notice that when the chain is removed,
  // smoldot panics on chain.nextJsonRpcResponse()
  // see wasn-node Rust code
  // https://github.com/smol-dot/smoldot/blob/48864e7810746a55ba8aa7eb12156708f8630cc0/wasm-node/rust/src/lib.rs#L258
  chain.remove();
}, 10_000);

And the output is

[smoldot] Smoldot v1.0.15
[smoldot] Chain initialization complete for westend2. Name: "Westend". Genesis hash: 0xe143…423e. State root hash: 0x7e92439a94f79671f9cade9dff96a094519b9001a7432244d46ab644bb6f746f. Network identity: 12D3KooWA5qfvRuf5WncnAk2CANCsnBzSjwLVeEnX5Wzc5yTU2kd. Chain specification or database starting at: 0x1deb…95c7 (#16847280)
[json-rpc-westend2] The JSON-RPC client has just called a JSON-RPC function from the legacy JSON-RPC API (system_name). Legacy JSON-RPC functions have loose semantics and cannot be properly implemented on a light client. You are encouraged to use the new JSON-RPC API <https://github.com/paritytech/json-rpc-interface-spec/> instead. The legacy JSON-RPC API functions will be deprecated and removed in the distant future.
{
  jsonRpcResponse: '{"jsonrpc":"2.0","id":1,"result":"smoldot-light-wasm"}'
}
[sync-service-westend2] GrandPa warp sync finished to #17001662 (0xbfb4…753e)
[runtime-westend2] Finalized block runtime ready. Spec version: 9430. Size of `:code`: 1.2 MiB.
will remove chain
[smoldot] Shutting down chain westend2
Smoldot has panicked. This is a bug in smoldot. Please open an issue at https://github.com/smol-dot/smoldot/issues with the following message:
panicked at 'called `Option::unwrap()` on a `None` value', wasm-node/rust/src/lib.rs:258:10
Error
    at panic (/home/projects/smoldot-panic/node_modules/smoldot/dist/mjs/internals/local-instance.js:76:23)

The wasm-node rust code seems to be

fn json_rpc_responses_peek(chain_id: u32) -> u32 {
let mut client_lock = CLIENT.lock().unwrap();
match client_lock
.chains
.get_mut(usize::try_from(chain_id).unwrap())
.unwrap()

I'm not sure if there is a better way to implement the loop to await for JSON RPC responses.

@kratico kratico changed the title Removing a chain causes smoldot to crash Removing a chain causes smoldot to crash/panic Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant