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

NodeJS 5.1 http server unexpected Socket timeouts #3923

Closed
3axap4eHko opened this issue Nov 19, 2015 · 12 comments
Closed

NodeJS 5.1 http server unexpected Socket timeouts #3923

3axap4eHko opened this issue Nov 19, 2015 · 12 comments
Labels
http Issues or PRs related to the http subsystem.

Comments

@3axap4eHko
Copy link

Modified example from https://nodejs.org/en/about/

'use strict';
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
})
//Added timeout callback
.setTimeout(5000, () => console.log('Oops!'))
.listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

In 5 seconds after open url you will see

Server running at http://127.0.0.1:1337/
Oops!
Oops!

The first Oops! is for url and the another one is for favicon.ico

@mscdex mscdex added the http Issues or PRs related to the http subsystem. label Nov 19, 2015
@mscdex
Copy link
Contributor

mscdex commented Nov 19, 2015

By default Connection: keep-alive is used in responses. So the timer firing is indicating no activity on those connections that were being kept alive (for possible additional requests). Therefore the timeouts should be expected.

If you explicitly add 'Connection': 'close' to your response headers, you'll see that "Oops!" is no longer printed to the console.

@tflanagan
Copy link
Contributor

Just an FYI, the setTimeout method ignores activity.

Re: #3319

@3axap4eHko
Copy link
Author

@mscdex Doesn't work for me

@3axap4eHko
Copy link
Author

Also, I tried to setup by Server.timeout = 5000; and listen the event by Server.on('timeout, listener)

@bricss
Copy link

bricss commented Nov 24, 2015

It's happens because all modern browsers always asking for favicon.ico and essentially sending two GET requests, if you making yours requests ain't by AJAX.

@3axap4eHko
Copy link
Author

@bricss Did you try it?

@bricss
Copy link

bricss commented Nov 24, 2015

@3axap4eHko Yes, I did.

@3axap4eHko
Copy link
Author

@bricss No, man! There are issue not in the favicon.ico. So why this code make only one Oops!:

'use strict';
var http = require('http');
var content = '<html><head><script src="https://code.jquery.com/jquery-2.1.4.min.js"></script><script>$.get("http://127.0.0.1:1337/")</script></head><body>Hello World!</body></html>\n';
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(content);
})
.setTimeout(5000, () => console.log('Oops!'))
.listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

when both of the files (html, ico) are requested?

@bricss
Copy link

bricss commented Nov 24, 2015

Use your first code sample. Open your browser and type localhost:1337 in omnibox, then look into your log, after that press F12 and make XHR request to same URL. You will be surprised.
Also if you don't add favicon link to html markup it may not be requested. Obviously favicon will never be requested by XHR call.

@3axap4eHko
Copy link
Author

@bricss your browser is cached some requests, lets try this example for windows

'use strict';
var http = require('http');
var content = '<html><body>Hello World!<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script><script>$.get("http://127.0.0.1:1337/")</script></body></html>\n';
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(content);
})
.setTimeout(5000, () => console.log('Oops!'))
.listen(1337, '127.0.0.1', () => {
    //Opens on windows default browser
    require('child_process').spawn('cmd', ['/c', 'start', 'http://127.0.0.1:1337/']);
});
console.log('Server running at http://127.0.0.1:1337/');

@bricss
Copy link

bricss commented Nov 25, 2015

Ok, after some debug and research I found that the problem with double log inhere only for Chrome. IE and Firefox doesn't have such problem.
But after careful read of docs I found that server.setTimeout is Sets the timeout value for sockets and may presume that Chrome opens several sockets in same time, in my case it's even 4, cause I see 4 log messages.

@jasnell
Copy link
Member

jasnell commented Apr 9, 2016

Reading through this it's not clear that this is a node problem. Closing but can reopen if necessary.

@jasnell jasnell closed this as completed Apr 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants