Skip to content

Commit

Permalink
benchmark: fix async-resource benchmark
Browse files Browse the repository at this point in the history
In the benchmark, because it performs asynchronous operations before
writing its HTTP replies, the underlying socket can be closed by the
peer before the response is written. Since 28e6626, that means
that attempting to `.end()` the HTTP response results in an uncaught
exception, breaking the benchmark.

Fix that by checking whether the response object has been destroyed
or not before attempting to call `.end()`.

nodejs#33591
  • Loading branch information
addaleax committed May 29, 2020
1 parent 9949a2e commit 73d01d8
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions benchmark/async_hooks/async-resource-vs-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function getServeAwait(getCLS, setCLS) {
setCLS(Math.random());
await sleep(10);
await read(__filename);
if (res.destroyed) return;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ cls: getCLS() }));
};
Expand All @@ -148,6 +149,7 @@ function getServeCallbacks(getCLS, setCLS) {
setCLS(Math.random());
setTimeout(() => {
readFile(__filename, () => {
if (res.destroyed) return;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ cls: getCLS() }));
});
Expand Down

0 comments on commit 73d01d8

Please sign in to comment.