middie icon indicating copy to clipboard operation
middie copied to clipboard

Process crashes with TypeError: Cannot read properties of null (reading 'originalUrl')

Open mohd-akram opened this issue 3 years ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.2.0

Plugin version

8.0.0

Node.js version

18.5.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

13.1

Description

The operating system is FreeBSD. Every once in a while, I get this error and the process crashes:

TypeError: Cannot read properties of null (reading 'originalUrl')
    at Holder.done (/usr/local/www/cinemaseats/node_modules/@fastify/middie/engine.js:78:21)
    at Domain.emit (node:events:537:28)
    at Domain.emit (node:domain:482:12)
    at emit (node:internal/process/promises:147:33)
    at processPromiseRejections (node:internal/process/promises:283:27)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)

Steps to Reproduce

I'm only using the Sentry middleware with middie:

await fastify.register(middie);
fastify.use(Sentry.Handlers.requestHandler());

Expected Behavior

No response

mohd-akram avatar Jul 20 '22 06:07 mohd-akram

It's likely that one of your middlewares is calling done() more than once.

mcollina avatar Jul 20 '22 06:07 mcollina

Do you mean both Fastify (.register()) and express (.use()) middlewares or just express?

mohd-akram avatar Aug 22 '22 17:08 mohd-akram

If you call the callback multiple times... things will execute more than one time.

mcollina avatar Aug 22 '22 17:08 mcollina

I get that, I'm just not familiar with the inner workings of middie. Do I need to investigate all my middlewares for this issue or just the Sentry one since that's the only express middleware I'm using. It will help me narrow down where to look.

mohd-akram avatar Aug 22 '22 17:08 mohd-akram

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

mcollina avatar Aug 22 '22 22:08 mcollina

As you said (thanks for the hint), the issue was one of my request handlers had an unawaited promise running without a .catch which caused this line in the Sentry request middleware to trigger due to an error in that promise, after it had already called next here. I also noticed, more recently, that sometimes the process doesn't crash and just lingers forever but is unable to handle any requests. I wonder if there's something to be improved in the Sentry library to not call next when there's an error in the domain since according to the Node.js docs it seems that this is equivalent to a process crash (if you have any thoughts on this as a Node.js dev I'd appreciate it as I might open an issue there). Anyway, this doesn't seem to be a middie issue and I'm not sure if any improvements can be made there.

mohd-akram avatar Aug 23 '22 08:08 mohd-akram

No worries, I'm glad I've been of help!

mcollina avatar Aug 23 '22 08:08 mcollina