Skip to content

When use the realtime database in Deno, it never finishes running #5783

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

Closed
ayame113 opened this issue Dec 3, 2021 · 6 comments
Closed

When use the realtime database in Deno, it never finishes running #5783

ayame113 opened this issue Dec 3, 2021 · 6 comments

Comments

@ayame113
Copy link

ayame113 commented Dec 3, 2021

[REQUIRED] Describe your environment

  • Operating System version: Microsoft Windows [Version 10.0.19042.1348]
  • Browser version: Deno 1.16.3
  • Firebase SDK version: 9.6.0
  • Firebase Product: realtime database

[REQUIRED] Describe the problem

Steps to reproduce:

Running the following code with deno run -A --location http://foo.com test.ts will hang forever after the finish is outputted, even though I'm calling deleteApp.

// test.ts

// @deno-types="https://cdn.esm.sh/v58/[email protected]/app/dist/app/index.d.ts"
import { deleteApp, FirebaseOptions, initializeApp } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js";
// @deno-types="https://cdn.esm.sh/v58/[email protected]/database/dist/database/index.d.ts"
import { getDatabase, ref, set } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-database.js";

const app = initializeApp({...});
const db = getDatabase(app);
await set(ref(db), 1);
deleteApp(app);
console.log("finish");

I changed it to log the setTimeout call using the following code and found that setTimeout called the reportStats_ method forever.

const originalSetTimeout = globalThis.setTimeout;
globalThis.setTimeout = (...args) => {
  const timeoutId = originalSetTimeout(...args);
  console.log({ timeoutId }, ...args);
  return timeoutId;
};

The reason for this may be that the implementation of setTimeout in Deno is web compatible and there is no unref method.
We probably need to stop using the unref method and call crearTimeout() when deleteApp() is called.

const timeout: number | object = setTimeout(fn, time);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (typeof timeout === 'object' && (timeout as any)['unref']) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(timeout as any)['unref']();
}

@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@jbalidiong
Copy link
Contributor

Hi @ayame113, thanks for the report. I was able to reproduce the behavior. Let me check what we can do for this issue or bring someone here that can provide more context about it. I’ll update this thread if I have any information to share.

@maneesht
Copy link
Contributor

@ayame113 - we use unref to signal to the runtime that if the program wishes to exit earlier, do not wait on this setTimeout to finish. If we just clear the timeout right then and there, the stats will never get reported.

I have an experimental PR created that uses Deno's unrefTimer as opposed to NodeJS's unref. It's experimental since the API that it requires Deno.unrefTimer is considered an "unstable" API, and in order for this to work, you need to run your app with --unstable to allow for unstable APIs to be used.

@maneesht
Copy link
Contributor

Also, thank you for spending the time to root cause this issue. You made this very easy to debug and test.

@maneesht
Copy link
Contributor

maneesht commented Aug 25, 2022

@ayame113 - I have another PR incoming to allow you to use Deno with local node module dependencies, if you choose. But this issue has been resolved.

@ayame113
Copy link
Author

But this issue has been resolved.

nice!!
thank you!!

@firebase firebase locked and limited conversation to collaborators Sep 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants