Skip to content
Open
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/tokio-fetch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ crate-type = ["cdylib"]
neon.workspace = true
reqwest = { version = "0.12.24", features = ["json"] }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["macros"] }
tokio-util = "0.7"
23 changes: 23 additions & 0 deletions examples/tokio-fetch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,26 @@ fn current_node_release_date(
// This task is executed asynchronously on the tokio thread pool
Ok(node_release_date(version))
}

#[neon::export]
async fn node_release_date_with_abort(
version: String,
token: CancellationToken,
) -> Result<String, Error> {
tokio::select! {
release = node_release_date(version) => release,
_ = token.token.cancelled() => Err(Error::new("Request aborted")),
}
}

#[derive(Clone, Debug, Default)]
struct CancellationToken {
token: tokio_util::sync::CancellationToken,
}

#[neon::export(class)]
impl CancellationToken {
fn cancel(&self) {
self.token.cancel();
}
}
18 changes: 18 additions & 0 deletions examples/tokio-fetch/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { CancellationToken, nodeReleaseDateWithAbort } = require(".");

const timeout = Number(process.argv[2]) || 10000;

async function main() {

@vadimpiven vadimpiven Jun 8, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be

async function callAbortable<T>(
  callback: (token: CancellationToken) => Promise<T>,
  signal: AbortSignal,
): Promise<T> {
  const token = new CancellationToken();
  const abort = () => token.cancel();
  try {
    signal.addEventListener('abort', abort, { once: true });
    signal.throwIfAborted();
    return await callback(token);
  } finally {
    signal.removeEventListener('abort', abort);
  }
}

async function main() {
  const version = process.version;
  const date = await callAbortable(
    (token) => nodeReleaseDateWithAbort(version, token),
    AbortSignal.timeout(timeout),
  );
  console.log(date);
}

to avoid calling addon if the signal is already aborted and clean-up listener to avoid memory leaks

const version = process.version;
const ctrl = new AbortController();
const token = new CancellationToken();

ctrl.signal.addEventListener("abort", () => token.cancel());
setTimeout(() => ctrl.abort(), timeout).unref();

const date = await nodeReleaseDateWithAbort(version, token);

console.log(date);
}

main();
Loading