Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close() only takes effect after closing browser #75

Closed
nodesocket opened this issue Jul 21, 2011 · 1 comment
Closed

close() only takes effect after closing browser #75

nodesocket opened this issue Jul 21, 2011 · 1 comment

Comments

@nodesocket
Copy link

The following code reproduces the problem:

var http = require('http');
var http_proxy = require('node-http-proxy');

var router = http_proxy.createServer({ router: { "/first":
"127.0.0.1:3000", "/second": "127.0.0.1:3001" }});
router.listen(80);

http.createServer(function (req, res) {
       res.writeHead(200, { 'Content-Type': 'text/plain' });
       res.write("Hello From First");
       res.end();
}).listen(3000);

http.createServer(function (req, res) {
       res.writeHead(200, { 'Content-Type': 'text/plain' });
       res.write("Hello From Second");
       res.end();
}).listen(3001);

setTimeout(function() { router.close() }, 10000);
setInterval(function() { console.log("hi") }, 3000);

So after 10 seconds, requesting /first should not resolve, yet it continues to resolve and respond with 'Hello From First' until you close the browser, then requests stop being routed. Confirmed in both Firefox 5 and Chrome.

@indexzero
Copy link
Contributor

@nodesocket This is actually by design in node.js core and has nothing to do with node-http-proxy. Here's a quick sample repro'ing the problem with the http module:

var http = require('http');

var closed = false;
var server = http.createServer(function (req, res) {
  if (!closed) {
    closed = true;
    server.close();
  }

  console.log('Incoming connection');
  console.dir(server);

  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write("Hello From Server");
  res.end();
})

server.on('close', function () {
  console.log('server emitted close') 
});

server.listen(3000);

It's actually good that the server does this because you want to keep serving all of your connections and not abruptly cut them off. What would be nice is if the close event didn't fire on the http.Server instance until all of the connections had closed gracefully. I've talked to @ry and opened an issue on node.js core: nodejs/node-v0.x-archive#1383

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants