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

Deno hangs after succesfully executing npm-package library call #23727

Closed
aukeroorda opened this issue May 7, 2024 · 8 comments · Fixed by #24637
Closed

Deno hangs after succesfully executing npm-package library call #23727

aukeroorda opened this issue May 7, 2024 · 8 comments · Fixed by #24637
Assignees
Labels
bug Something isn't working correctly needs investigation requires further investigation before determining if it is an issue or not

Comments

@aukeroorda
Copy link

> deno --version
deno 1.41.3 (release, x86_64-apple-darwin)
v8 12.3.219.9
typescript 5.3.3

MRE

Can be found at: https://github.com/aukeroorda/mre-deno-hang/tree/main

  1. Verify the contents of the repo
  2. Install the paper-js library as specified in the package file:
    npm install
  3. Run deno:
    deno run mre.ts
  4. Allow reading of node_modules: y
  5. Observe that code executes to the end of the mre.ts script, but Deno hangs.

Note:
Deno doesn't hang anymore if the following lines 20 and 21 are commented out: https://github.com/aukeroorda/mre-deno-hang/blob/6c3d8d0f313141bff39ffc0906780189668b055d/mre.ts#L20-L21

Inspecting using chromium

I've tried to inspect what is going on with this library call using the chromium debugger, but I can't really see much, as the call gets 'hijacked' by a call to node:timers setInterval() function, and I lose the trace of the library function call here.

@aukeroorda aukeroorda changed the title Deno hangs after specific library call Deno hangs after succesfully executing npm-package library call May 7, 2024
@bartlomieju bartlomieju added bug Something isn't working correctly node compat labels May 7, 2024
@lucacasonato
Copy link
Member

lucacasonato commented Jun 8, 2024

I presume this is related to NAPI, as paper uses node-canvas. I have not tested that though - so it's just a theory :)

@lucacasonato lucacasonato added the node native extension related to the node-api (.node) label Jun 8, 2024
@devsnek devsnek added needs investigation requires further investigation before determining if it is an issue or not and removed node compat node native extension related to the node-api (.node) labels Jul 2, 2024
@devsnek
Copy link
Member

devsnek commented Jul 2, 2024

According to tokio-console this is getting stuck waiting for a timer but more investigation is needed to figure out exactly what timer.

@kt3k
Copy link
Member

kt3k commented Jul 18, 2024

Deno fails to simulate Node.js in paper.js because it uses self global variable to detect Node.js https://github.com/paperjs/paper.js/blob/92775f5279c05fb7f0a743e9e7fa02cd40ec1e70/src/init.js#L32

(In Deno, paper.agent becomes { platform: undefined }, in Node.js it is:

{
  platform: 'mac',
  mac: true,
  version: '20.15.0',
  versionNumber: 20.15,
  name: 'node',
  node: true
}

If the platform is not Node.js, view._autoUpdate becomes true:

https://github.com/paperjs/paper.js/blob/92775f5279c05fb7f0a743e9e7fa02cd40ec1e70/src/view/View.js#L120

If view._autoUpdate is true, it calls view.requestUpdate method:

https://github.com/paperjs/paper.js/blob/92775f5279c05fb7f0a743e9e7fa02cd40ec1e70/src/item/Project.js#L97

Which calls polyfilled version of requestAnimationFrame in Deno:

https://github.com/paperjs/paper.js/blob/92775f5279c05fb7f0a743e9e7fa02cd40ec1e70/src/view/View.js#L225

Which internally calls setInterval, which is never cleared.

https://github.com/paperjs/paper.js/blob/92775f5279c05fb7f0a743e9e7fa02cd40ec1e70/src/dom/DomEvent.js#L120

One of workarounds is deleting globalThis.self before importing paper:

delete globalThis.self;
const paper_import = await import("paper");
const paper = paper_import.default;
globalThis.paper = paper;
paper.setup();
const pathdata = "M 0 0 C 0 10 10 10 10 0 z"; 
const path = new paper.Path(pathdata);
console.log({ path }); 
console.log(paper.agent);

The above script exits immediately and prints paper.agent with node field set to true.

@kt3k
Copy link
Member

kt3k commented Jul 18, 2024

One solution might be hiding globalThis.self while evaluating npm packages. WDYT?

@bartlomieju
Copy link
Member

Seems like a reasonable solution, self is not defined in Node.js

@aukeroorda
Copy link
Author

Hi -
Just running this again, but on my end it still hangs after execution, using the same MRE as stated earlier. This is using the updated deno version:

> deno --version
deno 1.43.5 (release, x86_64-apple-darwin)
v8 12.4.254.13
typescript 5.4.5

so I'm not sure if the PR to fix this issue actually fixes it.

Using delete globalThis.self; before the import does fix it in the MRE though!

@marvinhagemeister
Copy link
Contributor

@aukeroorda Deno v1.43.5 is a bit older. The latest version at the time of this writing is v1.45.5

@aukeroorda
Copy link
Author

Thanks for informing me! I thought I updated recently, but apparently not that recently! It is indeed solved.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly needs investigation requires further investigation before determining if it is an issue or not
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants