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

https.request timeout options doesn't work #23282

Open
ertygiq opened this issue Oct 5, 2018 · 9 comments
Open

https.request timeout options doesn't work #23282

ertygiq opened this issue Oct 5, 2018 · 9 comments
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. https Issues or PRs related to the https subsystem.

Comments

@ertygiq
Copy link

ertygiq commented Oct 5, 2018

Node version 8.11.3. 64-bit Windows 8.1

As I understood from docs, timeout property in https.request options sets socket connection timeout.
I set it to minimum - 1 millisecond and it should definitely trigger 'timeout' event.

    const options = {
        host: 'yobit.net',
        path: '/api/3/depth/ltc_btc',
        timeout: 1
    }
    const req = https.request(options, res => {
    });
    
    req.on('timeout', () => {
        console.error('request timeout');
    })
    req.end();

I expect 'reqest timeout' console message but it doen't appear.

However, when I create my custom https Agent with overriden createConnection method that sets initial socket timeout to 1 millisecond there:

MyAgent.prototype.createConnection = function() {
    const socket = https.Agent.prototype.createConnection.apply(this, arguments);
    socket.setTimeout(1);
    return socket;
}

and run the first code with agent parameter set to my custom agent, now it fires 'timeout' event.

@thefourtheye thefourtheye added the https Issues or PRs related to the https subsystem. label Oct 6, 2018
@thefourtheye
Copy link
Contributor

I can confirm this behaviour in my Ubuntu 18.04 as well, with Node.js 8.11.3.


cc @nodejs/http

@YECHUNAN
Copy link

YECHUNAN commented Oct 7, 2018

This issue may be related back to #12005, #8101, #7580, which dates back to when this feature is requested. By the way, it does timeout with Node.js 10.11.0 on my Windows 10 machine using the first part of code above.

@teifip
Copy link

teifip commented Mar 2, 2019

I'm observing the same behavior with v10.15.2 on Ubuntu 16.04.6 LTS.
If the timeout is set as part of the HTTPS request options, than the timeout event is emitted as expected. Instead, a timeout set with request.setTimeout() does not seem to take effect.

@Yohandah
Copy link

Yohandah commented Jan 28, 2020

I still can't find documentation on how to set a custom timeout for a given request :-( the following isn't working .... :

https.get("url", { timeout: 1000 }, (res) => {
    resp.on("timeout", () => {
        console.log("Never fired");
    });
});

Doing this doesn't work either :

https.get("url", (req, res) => {
    req.setTimeout(1000);
});

Can someone help ?

@jasnell jasnell added the help wanted Issues that need assistance from volunteers or PRs that need help to proceed. label Jun 26, 2020
@whipgrit
Copy link

whipgrit commented Jul 2, 2020

this worked for me:

let promiseHTTPSRequest = function(options)
{
return new Promise(function (resolve, reject)
{

			options.timeout = 5000;	//limit https request 
			
			var req = https.request(options, function(res) 
			{
			  var msg = "";
			  
			  res.setEncoding("utf8");
			  res.on("data", function(chunk) {
				msg += chunk;
			  });
			  res.on("end", function() {
				//console.log(msg);
				resolve(msg);
			  });
			});
			req.on("error", function(e) {
			  console.error(e);
			  reject(e);
			});
			 req.on("timeout", function(chunk) {
				console.log("Waited for response for " + options.timeout/1000 + " seconds. Exiting");
				reject("Waited for response for " + options.timeout/1000 + " seconds. Exiting");
			});
			req.write(options.post_data);
			req.end();
			
		}).catch(function (error) {
			console.log(error);
		});
	};

@mmomtchev
Copy link
Contributor

I tried reproducing the original issue and for me everything works as expected
Tested on Linux and Windows 10 with Node 12 and Node 14
I tried both request.timeout parameter on creation and request.setTimeout() call after creation
Does anyone still have this issue on a recent version?

@eduveks
Copy link

eduveks commented Apr 28, 2022

I thought I was facing this behavior with Node v17.5.0 on macOS.

But no! Only works with setTimeout!

Then with setTimeout seems to be working very well.

@flav-code
Copy link

Hello i have the same issue in node v18
the timeout is never called
image

@SiNONiMiTY
Copy link

SiNONiMiTY commented Feb 20, 2023

Encountering this weird behavior as well, timeout is not being hit.

Node v18.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. https Issues or PRs related to the https subsystem.
Projects
None yet
Development

No branches or pull requests