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

js(sdk): Crashes on a subscription with reusing a single Filter instance #568

Open
jiftechnify opened this issue Sep 8, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@jiftechnify
Copy link

jiftechnify commented Sep 8, 2024

Describe the bug
Script crashes when you subscribe to relay(s) with reusing a single Filter instance.

The stack trace:

/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:15841
    throw new Error(getStringFromWasm0(arg0, arg1));
          ^

Error: array contains a value of the wrong type
    at module.exports.__wbindgen_throw (/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:15841:11)
    at wasm://wasm/00f06292:wasm-function[5537]:0x245357
    at wasm://wasm/00f06292:wasm-function[860]:0x14ef4e
    at wasm://wasm/00f06292:wasm-function[3987]:0x2283bc
    at wasm://wasm/00f06292:wasm-function[267]:0x99834
    at wasm://wasm/00f06292:wasm-function[1033]:0x165e4b
    at wasm://wasm/00f06292:wasm-function[4844]:0x2376be
    at wasm://wasm/00f06292:wasm-function[5091]:0x23cb4b
    at __wbg_adapter_52 (/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:248:10)
    at real (/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:173:20)

To Reproduce

import {
  ClientBuilder,
  Filter,
  FilterOptions,
  loadWasmAsync,
  SubscribeAutoCloseOptions,
} from "@rust-nostr/nostr-sdk";

await loadWasmAsync();

const client = new ClientBuilder().build();
await client.addRelays([
  "wss://nos.lol",
  "wss://relay.nostr.band"
]);
await client.connect();

const filter = new Filter().kind(1);

console.log("sub to nos.lol");
await client.subscribeTo(
  ["wss://nos.lol"],
  [filter],
);

console.log("sub to relay.nostr.band");
await client.subscribeTo(
  ["wss://relay.nostr.band"],
  [filter], // reusing the filter here
);

Expected behavior
No crash.

Build environment

  • Library: nostr-sdk
  • Language: JavaScript
  • Language version: Node.js v22.8.0
  • Tag/commit: v0.34.0
  • OS+version: Arch Linux
@jiftechnify jiftechnify added the bug Something isn't working label Sep 8, 2024
@yukibtc
Copy link
Member

yukibtc commented Sep 8, 2024

Unfortunately it's an issue of wasm-bindgen: when an custom object is used in a list (Vec<T>) it's moved so can't be re-used.

Until this issue is solved on their side, I can temporary add a clone method on some objects (like Filter).

So would be:

const filter = new Filter().kind(1);

console.log("sub to nos.lol");
await client.subscribeTo(
  ["wss://nos.lol"],
  [filter.clone()], // Clone here
);

console.log("sub to relay.nostr.band");
await client.subscribeTo(
  ["wss://relay.nostr.band"],
  [filter], // Filter consumed here
);

yukibtc added a commit that referenced this issue Sep 8, 2024
Ref #568

Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
@jiftechnify
Copy link
Author

Fair enough. Thank you for looking into the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants