Skip to content
New issue

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

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

Already on GitHub? # to your account

AsyncLocalStorage not working with dynamic imports #25275

Closed
caleblloyd opened this issue Aug 28, 2024 · 2 comments · Fixed by #25593
Closed

AsyncLocalStorage not working with dynamic imports #25275

caleblloyd opened this issue Aug 28, 2024 · 2 comments · Fixed by #25593
Assignees

Comments

@caleblloyd
Copy link
Contributor

Version: Deno 1.46.1

AsyncLocalStorage does not seem to work inside of a dynamic import in Deno, but it does in Node:

als.mjs

import { AsyncLocalStorage } from "node:async_hooks";

const als = new AsyncLocalStorage();

export function getAls() {
  return als.getStore();
}

function print() {
  console.log("ALS inside als.mjs is", getAls());
  return Promise.resolve();
}

async function run() {
  als.run("set", async () => {
    await import("./dynamic.mjs");
    await print();
  });
}

await run();

dynamic.mjs

import { getAls } from "./als.mjs";

console.log("ALS inside dynamic.mjs is", getAls());

not working in deno

$ deno run als.mjs
ALS inside dynamic.mjs is undefined
ALS inside als.mjs is set

working in node

$ node als.mjs 
ALS inside dynamic.mjs is set
ALS inside als.mjs is set
@devsnek devsnek self-assigned this Aug 28, 2024
@devsnek
Copy link
Member

devsnek commented Aug 28, 2024

I think #25140 should fix this.

@caleblloyd
Copy link
Contributor Author

caleblloyd commented Aug 29, 2024

That is a nice improvement! Unfortunately I do not think that fixes this particular issue though 🥲 I rebased that PR on deno_core 0.307.0 and added this test case to tests/unit_node/async_hooks_test.ts:

Deno.test(async function worksWithDynamicImports() {
  const store = new AsyncLocalStorage();
  (globalThis as any).alsDynamicImport = () => store.getStore();
  const dataUrl =
    `data:application/javascript,export const data = alsDynamicImport()`;
  await store.run("data", async () => {
    const { data } = await import(dataUrl);
    assertEquals(data, "data");
  });
});

and it fails:

[async_hooks_test 005.03]     [Diff] Actual / Expected
[async_hooks_test 005.03] -   undefined
[async_hooks_test 005.03] +   "data"

devsnek added a commit to denoland/deno_core that referenced this issue Sep 10, 2024
Fix for denoland/deno#25275

This code is like hacks on top of hacks, but basically save & restore
cped in the dynamic import flow.
devsnek added a commit that referenced this issue Sep 12, 2024
Fixes: #25275

Signed-off-by: snek <snek@deno.com>
kuruk-mm pushed a commit to dclexplorer/deno_core that referenced this issue Oct 11, 2024
Fix for denoland/deno#25275

This code is like hacks on top of hacks, but basically save & restore
cped in the dynamic import flow.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants