question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending after request end doesn't throw error, hangs response instead

See original GitHub issue

Hi, I’ve noticed that when including express-session, the res.end behavior (from what I’m assuming is due to the proxied res.end) deviates from what I would expect in handling errors.

Generally if I were to call res.end and then later inadvertently call res.send after that (perhaps in another middleware handler), I would receive an ERR_HTTP_HEADERS_SENT. That’s what I was expecting to see—however, instead, if I include the session middleware, the headers send but it waits for 5s (which I think is the default keepAlive timeout) until erroring out with a content length mismatch (since it is setting Content-Length but the body doesn’t go through). In my opinion, it would be helpful to have that original error bubble through if possible, since it was not clear that including this middleware would cause this API change to res.end as a side effect.

var express = require("express");
var app = express();

// Uncommenting the middleware usage leads to different functionality in handling the duplicate response sending
app.use(
  require("express-session")({
    secret: "keyboard cat",
    resave: true,
    saveUninitialized: true,
  })
);

app.get("/", function (req, res) {
  res.end();
  // Doesn't throw error! Waits for keepAlive duration
  res.send("This is a test!");
});

app.listen(5000);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Jul 8, 2020

Hi @josh-byster there is a PR #767 that should fix it if you are interested in giving it a test.

0reactions
josh-bystercommented, Jul 8, 2020

Completely understandable, and thank you for the clarification 😃 I just wanted to give an example of a concrete use case in addition, as I realized my original issue report lacked a concrete way for which this behavior may surface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can I execute code after "res.send"? - node.js
My understanding is that res.send doesn't return the function, but does close the connection / end the request. This could explain why I...
Read more >
A Guide to Error Handling in Express.js | Scout APM Blog
A Guide to Error Handling in Express.js. Error handling often doesn't get the attention and prioritization it deserves.
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
If an uncaughtException happens, the recommendation is to always crash your process, let it crash. Don't try to recover from an ...
Read more >
Node.js Error Handling Best Practices: Ship With Confidence
Want to ship your Node.js applications with confidence? Learn Node.js error handling best practices, tips and tricks with our simple guide.
Read more >
HTTP | Node.js v19.3.0 Documentation
The actual header will be sent along with the first data chunk or when calling request.end() . To get the response, add a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found