Auke
Auke2mo ago

`deno run` not returning

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");
}
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");
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?
17 Replies
Deno AI Helper
Deno AI Helper2mo ago
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
Deno AI Helper
Deno AI Helper2mo ago
dependencies and possibly identify where the issue might be occurring. 5. Simplify Your Code: Try to simplify your code to the smallest version that reproduces the issue. This can often help pinpoint the exact cause. 6. 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. 🦕
Auke
Auke2mo ago
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
marvinh.
marvinh.2mo ago
@Auke That sounds like a bug in Deno. Can you file an issue here with the steps to reproduce it? https://github.com/denoland/deno/issues/new/choose
Auke
Auke2mo ago
I will try to get an mre going
marvinh.
marvinh.2mo ago
The snippet you provided here sounds fine if that's enough to reproduce it 🙂
Auke
Auke2mo ago
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
Auke
Auke2mo ago
Could you evaluate whether this also hangs on your side? https://github.com/aukeroorda/mre-deno-hang
GitHub
GitHub - aukeroorda/mre-deno-hang
Contribute to aukeroorda/mre-deno-hang development by creating an account on GitHub.
Auke
Auke2mo ago
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
Auke
Auke2mo ago
GitHub
Deno hangs after succesfully executing npm-package library call · I...
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 Verify the contents of the r...
Auke
Auke2mo ago
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
marvinh.
marvinh.2mo ago
Thanks for creating an issue! It hangs for me too when running the steps. Something is wrong in Deno.
bartlomieju
bartlomieju2mo ago
Is that package using native add-ons?
Auke
Auke2mo ago
I'm not familiar with the term 'native add-ons'
Auke
Auke2mo ago
@bartlomieju I don't think so. From what I understand, they are non-js language addons; In the repository of this library I see just js+ts: https://github.com/paperjs/paper.js
GitHub
GitHub - paperjs/paper.js: The Swiss Army Knife of Vector Graphics ...
The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey - paperjs/paper.js
bartlomieju
bartlomieju2mo ago
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?
Auke
Auke2mo ago
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
> 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
Changing window to globalThis causes it to run again, but also hang again, with DENO_FUTURE=1 @bartlomieju