Created
September 20, 2021 22:26
-
-
Save owenallenaz/bc66547842e87a649b554b7c168a7314 to your computer and use it in GitHub Desktop.
res.on("finish") not called on client-side timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require("http"); | |
let callCount = 0; | |
function call() { | |
return new Promise(function(resolve, reject) { | |
console.log("new call", ++callCount); | |
const req = http.request("http://127.0.0.1:12345/large/", { | |
timeout: 100, | |
}, function(res) { | |
res.on("data", function() { | |
console.log("got data"); | |
}); | |
res.on("end", function() { | |
console.log("res end"); | |
resolve(); | |
}); | |
}); | |
req.on("timeout", function() { | |
req.destroy(new Error("timeout")); | |
}); | |
req.on("error", function(e) { | |
console.log("Got an error", e); | |
return resolve() | |
}); | |
req.end(); | |
}); | |
} | |
async function run() { | |
while(true) { | |
await call(); | |
} | |
} | |
run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM node:14.16.0 | |
WORKDIR /app | |
COPY . /app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker build -t pm2-test:local . | |
docker run --rm -it -v $PWD/server.js:/app/server.js -p 12345:80 pm2-test:local /bin/bash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require("http"); | |
let count = 0; | |
let closeCount = 0; | |
let finishCount = 0; | |
function makeLargeString(length) { | |
var result = ''; | |
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
var charactersLength = characters.length; | |
for ( var i = 0; i < length; i++ ) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
const server = http.createServer(function(req, res) { | |
console.log("REQUEST", req.url); | |
count++; | |
res.on("finish", function() { | |
finishCount++; | |
}); | |
res.on("close", function() { | |
closeCount++; | |
}); | |
if (req.url === "/status/") { | |
res.setHeader("content-type", "application/json"); | |
return res.end(JSON.stringify({ | |
count, | |
closeCount, | |
finishCount, | |
memory : process.memoryUsage() | |
}, null, "\t")); | |
} | |
if (req.url === "/large/") { | |
return setTimeout(function() { | |
const largeString = makeLargeString(.5 * 1024 * 1024); | |
return res.end(largeString); | |
}, 1000); | |
} | |
return res.end("done"); | |
}); | |
server.listen(80, function() { | |
console.log("booted"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment