denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.66k stars 5.25k forks source link

Too many open files #25941

Closed kewp closed 6 days ago

kewp commented 1 week ago

I just got the following panic in my server:

Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.

Platform: macos aarch64
Version: 1.46.3
Args: ["deno", "run", "--allow-read", "--unstable-cron", "--unstable-kv", "--allow-net", "--allow-env", "--watch", "./lib/server.ts"]

thread 'sw-0' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/denokv_sqlite-0.8.2/lib.rs:241:14:
called `Result::unwrap()` on an `Err` value: Os { code: 24, kind: Uncategorized, message: "Too many open files" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I did an lsof on the running process and I'm seeing a ton of these entries:

deno    35404 karl   30u  KQUEUE                                       count=0, state=0xa
deno    35404 karl   31u  KQUEUE                                       count=0, state=0xa
deno    35404 karl   32u  KQUEUE                                       count=0, state=0xa
deno    35404 karl   33u    unix 0xa8fc1bef99fd8d7d       0t0          ->0x6da6b28e6fd93df8
deno    35404 karl   34u     REG               1,16    356352 26844929 /Users/karl/Library/Caches/deno/location_data/0d2f21655f61bad4e6b2eac60e4a08256dcd29c6161d12a97e70e7cd5354a07b/kv.sqlite3
deno    35404 karl   35u     REG               1,16   4206552 28602556 /Users/karl/Library/Caches/deno/location_data/0d2f21655f61bad4e6b2eac60e4a08256dcd29c6161d12a97e70e7cd5354a07b/kv.sqlite3-wal

I'm guessing this is ... kv ?

lucacasonato commented 1 week ago

Is there anything else you are doing in your JS application code that is opening files?

kewp commented 1 week ago

I have this complex caching mechanism that uses KV. Maybe it's going awol? I will check

lucacasonato commented 1 week ago

Are you spawning a bunch of workers? Or are you calling Deno.openKv a lot?

kewp commented 1 week ago

To be honest I can't remember... let me get back to you

kewp commented 1 week ago

No spawning that I can see... just a lot of kv requests. Maybe I'm not closing them? I have a bunch of crons as well that use kv a lot

lucacasonato commented 1 week ago

So particularly I am asking if you are doing a lot of this:

async function foo() {
  const kv = await Deno.openKv();
  await kv.set(); // or get, or atomic().commit(), or delete, etc
}

or if you are doing:

const kv = await Deno.openKv();
async function foo() {
  await kv.set(); // or get, or atomic().commit(), or delete, etc
}

Are the crons registered at the global scope of the application?

kewp commented 1 week ago

I think they're global? I have a main server and a bunch of crons which are declared immediately at the top of the server code. I think I open kv once for each cron and use that. I'm driving I'll confirm in a few hours

kewp commented 6 days ago
Screenshot 2024-10-02 at 17 55 31

is this bad @lucacasonato ?

lucacasonato commented 6 days ago

Yes - you should close your kv instances after use (kv.close()). Or just open a single one globally.

kewp commented 6 days ago

Aren't crons run in a separate process? I mean like on Deno Deploy? So can I use a global kv instance there?

I've closed it at the end ... going to monitor how many open files it has ...

kewp commented 6 days ago

Yup I think that was it. Open files holding at around 100

lucacasonato commented 6 days ago

You can open it at the top - they can close over JS values like any other function.

kewp commented 6 days ago

Hmm that's too high level javascript for me but thanks, I'll read up on that ...

Should I close the issue?

kewp commented 5 days ago

@lucacasonato I'm getting errors now on deno deploy ...

Image

kewp commented 5 days ago

I'm guessing it's because ... when you close kv ... it closes it in other places too?

Image