denoland / deno

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

Bad Resource ID #22469

Closed leontastic closed 7 months ago

leontastic commented 8 months ago

I feel like this should work out of box, but for some reason it doesn't. Since I don't see any mentions of Bad Resource ID on oak or superoak repositories I suspect it is an issue with deno.

Deno version:

deno 1.40.5 (release, aarch64-apple-darwin)
v8 12.1.285.27
typescript 5.3.3

Reproduction:

// src/app.test.ts
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
import { superoak } from 'https://deno.land/x/superoak/mod.ts'

const router = new Router()

router.get('/', (context) => {
  context.response.body = 'OK'
  context.response.status = 200
})

const app = new Application()

app.use(router.routes())
app.use(router.allowedMethods())

Deno.test('it works', async () => {
  const request = await superoak(app)
  await request.get('/').expect(200, 'OK')
})

Output:

$ deno test --allow-net
~/src/app.test.ts
running 1 test from ./src/app.test.ts
it works ...
Uncaught error from ./src/app.test.ts FAILED
it works ... cancelled (0ms)

 ERRORS

./src/app.test.ts (uncaught error)
error: BadResource: Bad resource ID
              controller.abort();
                         ^
    at AbortSignal.CallbackContext.signal.addEventListener.once (ext:deno_http/00_serve.js:355:9)
    at innerInvokeEventListeners (ext:deno_web/02_event.js:754:7)
    at invokeEventListeners (ext:deno_web/02_event.js:801:5)
    at dispatch (ext:deno_web/02_event.js:658:9)
    at AbortSignal.dispatchEvent (ext:deno_web/02_event.js:1043:12)
    at AbortSignal.[[[signalAbort]]] (ext:deno_web/03_abort_signal.js:146:11)
    at AbortController.abort (ext:deno_web/03_abort_signal.js:280:30)
    at Object.close (https://deno.land/x/superoak@4.8.1/src/superoak.ts:72:26)
    at Object.close (https://deno.land/x/superdeno@4.9.0/src/test.ts:265:19)
    at close (https://deno.land/x/superdeno@4.9.0/src/close.ts:21:20)
This error was not caught from a test and caused the test runner to fail on the referenced module.
It most likely originated from a dangling promise, event/timeout handler or top-level code.

 FAILURES

./src/app.test.ts (uncaught error)

FAILED | 0 passed | 2 failed (9ms)

error: Test failed
petruki commented 8 months ago

It doesn't seem to be Deno. I ran a regression using Deno 1.4x.x versions and Oak v12 with the latest Superoak without issues. However, the problem starts to happen when I bump Oak to v13.

I couldn't find yet any change in Oak that would cause the API AbortController to throw an error.

petruki commented 8 months ago

Just an update on this. I opened a PR to fix this issue on the Oak v14.0.0 module and it looks good now.

leontastic commented 7 months ago

The fix submitted by @petruki for this: https://github.com/oakserver/oak/pull/639

Landed in version 14.0.0 https://github.com/oakserver/oak/blob/main/CHANGELOG.md#version-1400

Thank you sir 🙏

punarinta commented 6 months ago

It doesn't seem to be Deno. I ran a regression using Deno 1.4x.x versions and Oak v12 with the latest Superoak without issues. However, the problem starts to happen when I bump Oak to v13.

It would still be very good if Deno could handle it, as not everyone is using Oak. In my project I had to implement an ugly hack to prevent this "Bad Resource ID" crash. Actually Deno should allow to catch this exception similar to Node to prevent process restart.