Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a check if the res is destroyed before sending response #10106

Merged
merged 5 commits into from
Aug 16, 2022

Conversation

jmcdo29
Copy link
Member

@jmcdo29 jmcdo29 commented Aug 12, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

If a client currently closes the request before the stream finishes we get an error about not being able to send headers to a response that is already sent. It should be noted that this is express only, fastify does not have this issue

Issue Number: #10105

What is the new behavior?

In the case of using curl or a similar tool on the command line,
or if the client decides to end the request for a streamed file early
without the check we would end up causing a server crash. Now, we will
just not send the response as the client has already decided how to
move on.

Does this PR introduce a breaking change?

  • Yes
  • No

Technically yes due to the fact that the server now no longer dies, but that is the expected case, so I believe this should be fine as a patch rather than a major bump. The fact that we actually now prevent a crash is the good thing 🐱

Other information

In the case of using `curl` or a similar tool on the command line,
or if the client decides to end the request for a streamed file early
without the check we would end up causing a server crash. Now, we will
just not send the response as the client has already decided how to
move on.

fix: nestjs#10105
I had to use node's `http` module for this because
I could not get `supertest`s `abort` method to
work properly. With the raw `http` module I was able
to use `req.destroy()` to cancel the request early.
We now see the error from a premature closure, but are
still able to make extra requests afterwards
@coveralls
Copy link

coveralls commented Aug 12, 2022

Pull Request Test Coverage Report for Build 6bd812e5-d78a-48a1-b3ea-7496b7f1d761

  • 0 of 3 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 93.812%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/common/file-stream/streamable-file.ts 0 3 0.0%
Totals Coverage Status
Change from base Build 7429007a-3efb-492d-a2a1-84f706a41b5f: -0.01%
Covered Lines: 6094
Relevant Lines: 6496

💛 - Coveralls

Copy link
Contributor

@thiagomini thiagomini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some suggestions, awesome work Jay!

integration/send-files/e2e/express.spec.ts Outdated Show resolved Hide resolved
integration/send-files/e2e/express.spec.ts Outdated Show resolved Hide resolved
integration/send-files/e2e/express.spec.ts Outdated Show resolved Hide resolved
integration/send-files/src/app.service.ts Outdated Show resolved Hide resolved
As we make use of the `http` module instead of axios or supertest
we have tomake use of some rather low level code. Using this utils
file we can have clearer names of what is happening in each request
to make it easier to follow what's happening. I've also added in some
comments about why each part is the way it is for clarity.
Copy link
Contributor

@thiagomini thiagomini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice explanation of the details Jay, this is awesome :)

@kamilmysliwiec
Copy link
Member

LGTM

@Ryuytyuu-Greseven
Copy link

Hi, I am on NestJs cli v9.0.0..

I am getting this issue while streaming video files. Any solutions to fix this issue..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants