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

Fix Blocking behavior in Node.js bindings #1156

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security -->

## 1.0.9 - 2023-09-06

### Fixed

- The main thread gets blocked when calling client or wallet methods;

## 1.0.8 - 2023-09-05

### Added
Expand Down
11 changes: 1 addition & 10 deletions bindings/nodejs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ pub fn call_client_method(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let method_handler = Arc::clone(&cx.argument::<JsBox<ClientMethodHandlerWrapper>>(1)?.0);
let callback = cx.argument::<JsFunction>(2)?.root(&mut cx);

let (sender, receiver) = std::sync::mpsc::channel();
crate::RUNTIME.spawn(async move {
if let Some(method_handler) = &*method_handler.read().await {
let (response, is_error) = method_handler.call_method(method).await;
Expand All @@ -108,18 +107,10 @@ pub fn call_client_method(mut cx: FunctionContext) -> JsResult<JsUndefined> {
Ok(())
});
} else {
// Notify that the client got destroyed
// Safe to unwrap because the receiver is waiting on it
sender.send(()).unwrap();
panic!("Client got destroyed")
}
});

if receiver.recv().is_ok() {
return cx.throw_error(
serde_json::to_string(&Response::Panic("Client got destroyed".to_string())).expect("json to string error"),
);
}

Ok(cx.undefined())
}

Expand Down
11 changes: 1 addition & 10 deletions bindings/nodejs/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ pub fn call_wallet_method(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let method_handler = Arc::clone(&cx.argument::<JsBox<WalletMethodHandlerWrapper>>(1)?.0);
let callback = cx.argument::<JsFunction>(2)?.root(&mut cx);

let (sender, receiver) = std::sync::mpsc::channel();
crate::RUNTIME.spawn(async move {
if let Some(method_handler) = &*method_handler.read().await {
let (response, is_error) = method_handler.call_method(method).await;
Expand All @@ -125,18 +124,10 @@ pub fn call_wallet_method(mut cx: FunctionContext) -> JsResult<JsUndefined> {
Ok(())
});
} else {
// Notify that the wallet got destroyed
// Safe to unwrap because the receiver is waiting on it
sender.send(()).unwrap();
panic!("Wallet got destroyed")
}
});

if receiver.recv().is_ok() {
return cx.throw_error(
serde_json::to_string(&Response::Panic("Wallet got destroyed".to_string())).expect("json to string error"),
);
}

Ok(cx.undefined())
}

Expand Down
47 changes: 0 additions & 47 deletions bindings/nodejs/tests/wallet/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,53 +132,6 @@ describe('Wallet', () => {
await recreatedWallet.destroy()
removeDir(storagePath)
}, 20000);

it('error after destroy', async () => {
let storagePath = 'test-error-after-destroy';
removeDir(storagePath);

const walletOptions = {
storagePath,
clientOptions: {
nodes: ['https://api.testnet.shimmer.network'],
},
coinType: CoinType.Shimmer,
secretManager: {
stronghold: {
snapshotPath: `./${storagePath}/wallet.stronghold`,
password: `A12345678*`,
},
},
};

const wallet = new Wallet(walletOptions);
await wallet.storeMnemonic(
'vital give early extra blind skin eight discover scissors there globe deal goat fat load robot return rate fragile recycle select live ordinary claim',
);

const account = await wallet.createAccount({
alias: 'Alice',
});

expect(account.getMetadata().index).toStrictEqual(0);

await wallet.destroy();

try {
const accounts = await wallet.getAccounts();
throw 'Should return an error because the wallet got destroyed';
} catch (err: any) {
expect(err).toContain('Wallet got destroyed');
}

try {
const client = await wallet.getClient();
throw 'Should return an error because the wallet got destroyed';
} catch (err: any) {
expect(err).toContain('Wallet got destroyed');
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
}
removeDir(storagePath)
}, 35000);
})

function removeDir(storagePath: string) {
Expand Down