fastify / fastify-static

Plugin for serving static files as fast as possible
MIT License
433 stars 97 forks source link

`FST_ERR_REP_ALREADY_SENT` when wrapping route handler #434

Closed 10xLaCroixDrinker closed 8 months ago

10xLaCroixDrinker commented 8 months ago

Prerequisites

Fastify version

4.26.0

Plugin version

7.0.0

Node.js version

20.10.0

Operating system

macOS

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

13.6.1

Description

When using with @autotelic/fastify-opentelemetry including the wrapRoutes option, requests for the statics result in an empty response and a FST_ERR_REP_ALREADY_SENT error.

Steps to Reproduce

I've pushed up a repo with the steps to reproduce and further details

https://github.com/10xLaCroixDrinker/fastify-static-issue-434

Expected Behavior

No response

climba03003 commented 8 months ago

I am closing this issue because nothing show the bug is exist inside this repository. Please minimize your reproducible snippet without any other plugins.

climba03003 commented 8 months ago

My suggestion is remove the async from @autotelic/fastify-opentelemetry. So, it will respect the original function.

10xLaCroixDrinker commented 8 months ago

I am closing this issue because nothing show the bug is exist inside this repository. Please minimize your reproducible snippet without any other plugins.

Does the proposed solution not show that?

climba03003 commented 8 months ago

Does the proposed solution not show that?

Yes, consider the follow code working properly without @autotelic/fastify-opentelemetry. You should not force everyone to use async or return Promise.

instance.get('/ok', function(request, reply) {
  setTimeout(function() {
    reply.send('ok')
  }, 1000)
})

The occur of FST_ERR_REP_ALREADY_SENT is you are forking the promise chain. The plugin you mention actually doing the following action.

// note the async here
instance.get('/ok', async function(request, reply) {
  setTimeout(function() {
    reply.send('ok') // you return to client inside timeout as well
  }, 1000)
  // you implicit return undefined which reply to client with empty string
})
10xLaCroixDrinker commented 8 months ago

Thanks for your help @climba03003. I opened a PR to @autotelic/fastify-opentelemetry.