restify / restify/node-restify
Using return in Async handlers logs entire response object to stdout
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 975
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 5
Description
- Used appropriate template for the issue type
- Searched both open and closed issues for duplicates of this issue
- Title adequately and concisely reflects the feature or the bug
Restify Version: 11.1.0
Node.js Version: 19.7.0
Expected behaviour
We are upgrading from Restify 8.6.1 -> 11.1.0. In version 8.6.1, we were able to use a return before res.send in an async handler to end the request there and then:
server.get("/endpoint", async function get(req, res) {
return res.send({ });
});
This is especially useful in complex handlers to protect against accidental code running after a sent response.
However doing this in version 11.1.0 adds a very large console log to stdout with what looks like a response object.
I understand that calling next is not allowed when using async functions so the return value is being passed onto the handler chain, however I would expect using a return here would not perform this console log. The log does not happen when using a normal function handler, so I would expect an async handler to behave the same.
End the request and not perform a console log.
Actual behaviour
Logs a response object to stdout.
This message JSON ends with:
"msg": "Discarded returned value from async handler"
Logging this amount of data for each request would obviously add considerable load onto our logging systems.
Repro case
import * as restify from "restify";
const api = restify.createServer({
name: "API",
});
// ASYNC function results in a console log
api.get("/endpoint", async function get(req, res) {
return res.send({ });
});
// Normal function returns WITHOUT a console log as expected
api.get("/endpoint2", function get(req, res, next) {
return res.send({ });
});
// If not in test mode, start the server
api.listen(8080, function () {
console.log("ready on %s", api.url);
});
Cause
Are you willing and able to fix this?
Willing to help with guidance, but this probably needs someone who intimately understands the restify handler chain.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/chain.js around lines 188-198 and reproduce the behavior with the supplied async and normal handler examples. Trace how the async handler's returned value is handled, then verify that returning res.send ends the request without logging the response object or the discarded-value message to stdout.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100