deno icon indicating copy to clipboard operation
deno copied to clipboard

Fresh Web Server dies with "Bad resource ID" in Dev Mode

Open qkniep opened this issue 1 year ago • 3 comments

Version: Deno 1.38.2

I am maintaining a relatively simple web app centered around a single WebSocket endpoint. Recently the server crashed due to an internal panic within the Deno Rust code. This happened during moderate load (~100 concurrent connections). Later I was able to reproduce this in a low load environment (3 clients) without any communication apart from ping messages and connects/disconnects. I do not remember encountering the same problem in previous versions of Deno and haven't materially altered the code base in between.

Not sure if this helps in debugging, but this is some debugging output I produced based on the socket state

ERROR WebSocket errored: Unexpected EOF.
WARNING WebSocket closed.
ERROR WebSocket errored: Unexpected EOF.
WARNING WebSocket closed.
ERROR WebSocket errored: No response from ping frame..
WARNING WebSocket closed.
WARNING Socket not ready (state 3)
WARNING WebSocket closed.
WARNING Socket not ready (state 3)
[...]
WARNING Socket not ready (state 3)
ERROR WebSocket errored: No response from ping frame..
WARNING WebSocket closed.
WARNING Socket not ready (state 3)

This is the entire error output Deno produced:

============================================================
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: linux x86_64
Version: 1.38.2
Args: ["/local/home/qkniep/.deno/bin/deno", "run", "-A", "--watch=static/,routes/", "dev.ts"]

thread 'main' panicked at ext/websocket/lib.rs:733:67:
called `Result::unwrap()` on an `Err` value: Bad resource ID
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

qkniep avatar Nov 29 '23 09:11 qkniep

I was able to reproduce the same bug in the most recent version of Deno (1.38.3) as well and was able to run it with the RUST_BACKTRACE=1 flag (see output below). The basic procedure on the client is: (1) disconnect from internet (2) close website (3) wait for a minute (4) reconnect to the internet (5) reopen website.

By the way, I am also using the fresh framework for this app.

Importantly, I also now noticed that the request to open the _frsh/alive WebSocket fails in step (5). Also, I was so far not able to reproduce the same bug in a production build. So, I now suspect that the crash originates in something related to the _frsh/alive WebSocket and not the WebSockets I am spawning myself.

This is the output when the problem happens while running with RUST_BACKTRACE=1:

============================================================
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: linux x86_64
Version: 1.38.3
Args: ["/local/home/qkniep/.deno/bin/deno", "run", "-A", "--watch=static/,routes/", "dev.ts"]

thread 'main' panicked at ext/websocket/lib.rs:733:67:
called `Result::unwrap()` on an `Err` value: Bad resource ID
stack backtrace:
   0: rust_begin_unwind
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
   1: core::panicking::panic_fmt
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1652:5
   3: deno_websocket::op_ws_get_buffer_as_string::v8_fn_ptr
   4: Builtins_CallApiCallbackGeneric
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

qkniep avatar Nov 29 '23 11:11 qkniep

Panic is fixed in 1.39. It should throw a catchable error.

littledivy avatar Dec 14 '23 05:12 littledivy

Now (in Deno 1.39.0) the error is indeed thrown as an exception within the JS code. However, I encountered the case where it is thrown inside the WebSocket event loop:

ERROR WebSocket errored: Unexpected EOF.
WARNING WebSocket closed.
error: Uncaught (in promise) BadResource: Bad resource ID
    at WebSocket.[[[eventLoop]]] (ext:deno_websocket/01_websocket.js:424:19)
    at eventLoopTick (ext:core/01_core.js:181:11)

It might be due to my lack of understanding of either Deno or Async JS, but it seems to me that this is not catchable from user-level code and would instead need to be caught within the library code.

qkniep avatar Dec 18 '23 09:12 qkniep