#`deno run` not returning

1 messages · Page 1 of 1 (latest)

potent pollen
#

Hi - I have added the following code to my javascript file, and for some reason the code still executes fine, but it doesn't return anymore, leaving deno running and I have to ctrl-c out of it.

if ("Deno" in window) {
  // Import paper_js, as it is not loaded by the index.html script include
  const paper_import = await import("npm:paper");
  const paper = paper_import.default;
  await import("npm:paper-jsdom");
  globalThis.paper = paper;
  paper.setup();
  console.log("setup paper");
}

This prints out the setup paper. I have added some prints surrounding my entry point main, and those all print as well:

console.log("doing");
try {
  await main();
  console.log("done!");
} catch (error) {
  console.error("An error occursed:", error);
} finally {
  // Keep the program running until all asynchronous operations are complete
  await new Promise((resolve) => setTimeout(resolve, 0));
  console.log("now done");
}
console.log("really done");

So what could be keeping the deno instance from returning/finishing?

edgy surgeBOT
#

Based on the code snippets you've provided, it seems like your Deno program is not exiting because there might be some unresolved promises or ongoing asynchronous operations that are preventing the event loop from becoming empty, which is a requirement for the program to exit naturally.

In the first snippet, you're dynamically importing modules and setting up paper. If paper.setup() initiates some asynchronous operations that do not resolve or if there are event listeners that are still active, Deno will not exit as it waits for all asynchronous operations to complete.

In the second snippet, you're using a try...catch...finally block to execute main() and then wait for all asynchronous operations to complete with a setTimeout. However, the setTimeout in the finally block is unnecessary if you want the program to exit after main() has completed, as it introduces an additional asynchronous operation into the event loop.

Here are a few steps you can take to debug this issue:

  1. Check for Active Timers or Intervals: Ensure that there are no active setTimeout or setInterval calls that are keeping the event loop busy. If you have any, make sure to clear them with clearTimeout or clearInterval once they are no longer needed.

  2. Check for Active Listeners: If you have set up any event listeners (like paper.on('event', handler)), ensure that they are removed when they are no longer needed.

  3. Check for Unresolved Promises: If there are promises in your code (or in the libraries you are using) that are not resolving, they can keep the event loop active. Make sure all promises are either resolved or rejected.

  4. Use Deno's Diagnostic Tools: Deno provides diagnostic commands like deno info that can help you understand the module

#

dependencies and possibly identify where the issue might be occurring.

  1. Simplify Your Code: Try to simplify your code to the smallest version that reproduces the issue. This can often help pinpoint the exact cause.

  2. Review Third-Party Modules: Since you are importing third-party modules, it's possible that they may have some cleanup code that needs to be run or they may be starting some background tasks that need to be stopped.

If after reviewing these areas you still cannot find the cause, you may need to add more logging to your code to trace the execution flow and identify where the program is getting stuck. Remember that Deno will not exit if there are unresolved promises, active timers, or event listeners that are still being watched in the event loop. 🦕

potent pollen
#

It seems to be something with the Node.js compat layer eventLoopTick() function. I don't encounter this when using the same code in the browser, but I also don't really know whether there are any remaining unresolved promises there, but I'm not constructing any. Now investigating whether this library does that

#

Hmm, with the chromium debugger I don't really see how to get past this. The paper and paper-jsdom node modules seem to have been replaced by symlinks as well. Going to reinstall those

#

Yeah, even without the npm specifiers in the import, the call stack gets highjacked by node:timers setInterval() function

open zealot
potent pollen
#

I will try to get an mre going

open zealot
#

The snippet you provided here sounds fine if that's enough to reproduce it 🙂

potent pollen
#

Yeah, will have to figure out if that is enough! But looks like it, I've reduced it to one library call that is causing this to hang

#

If it does, Ill create an issue on the repo!

#

I've added a second method of importing the library, and that also causes the hang. Going to create the issue

#

It is a blocker for my thesis, so would love to find a solution to this! In the worst case I will just have to run this using Node I guess, but that would be some rewriting

open zealot
#

Thanks for creating an issue! It hangs for me too when running the steps. Something is wrong in Deno.

round perch
#

Is that package using native add-ons?

potent pollen
#

I'm not familiar with the term 'native add-ons'

round perch
#

Interesting I wonder if it might have problems because Deno has window global and the library thinks it's in the browser

#

Can you try again with DENO_FUTURE=1 env var?

potent pollen
#

Ah, please reply on the github issue, as I don't visit discord often

#

Will have a look

#
> deno run mre.ts
error: Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'Deno' in undefined
if ("Deno" in window) {
    ^
    at file:///Users/auke/mre-deno-hang/mre.ts:1:5