Skip to content

Instantly share code, notes, and snippets.

@alexhornbake
Created August 20, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhornbake/514feb55af360907cccbe64491b519ba to your computer and use it in GitHub Desktop.
Save alexhornbake/514feb55af360907cccbe64491b519ba to your computer and use it in GitHub Desktop.
reproduce nodejs keep-alive bug
// Client that can reproduce https://github.com/nodejs/node/issues/27363
// modified from https://gist.github.com/shuhei/4098a648a969deb38aad2164bc387148
const http = require("http");
const net = require("net");
function startClient(port) {
const socket = net.createConnection({ port });
let responseCount = 0;
socket.on("data", (chunk) => {
console.log("client data:", chunk.toString("utf8").split("\r\n")[0]);
responseCount += 1;
if (responseCount === 2) {
process.exit(0);
}
});
socket.on("error", (err) => {
console.log("client error:", err);
process.exit(1);
});
socket.write("GET / HTTP/1.1\r\n");
socket.write("Host: localhost\r\n");
socket.write("Connection: keep-alive\r\n");
socket.write("Agent: node\r\n");
socket.write("\r\n");
setTimeout(() => {
socket.write("GET / HTTP/1.1\r\n");
socket.write("Host: localhost\r\n");
socket.write("Connection: keep-alive\r\n");
socket.write("Agent: node\r\n");
// `headersTimeout` doesn't seem to fire if request headers
// are sent in one packet.
setTimeout(() => {
socket.write("\r\n");
}, 10);
}, 5000);
}
var args = process.argv.slice(2);
var port = 8080
if (args[0]) {
port = args[0]
}
startClient(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment