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

Timeout option doesn't work as expected #256

Open
archvlad opened this issue Oct 6, 2023 · 2 comments
Open

Timeout option doesn't work as expected #256

archvlad opened this issue Oct 6, 2023 · 2 comments

Comments

@archvlad
Copy link

archvlad commented Oct 6, 2023

🐞 Bug Report

Describe the bug

When I send request using node-fetch without proxy and it take a long time for server to respond I use timeout to limit the time of awaiting response. After timeout passed error is thrown. But when I make request with proxy using HttpsProxyAgent to the same server I receive error not after timeout but after about 30 seconds.


To Reproduce

  1. Run this code:
const fetch = require("node-fetch");
const { HttpsProxyAgent } = require("https-proxy-agent");

async function main() {
    let agent = new HttpsProxyAgent("http://localhost:8888", {
        timeout: 5000,
    });

    await fetch("https://1.1.1.1:1111", {
        agent,
        timeout: 5000,
    });
}

main();

Expected behaviour

FetchError: network timeout at: https://1.1.1.1:1111/

Your environment

  • OS: Windows 11
  • Node version: 16.20.0
  • Npm version: 8.19.4
@archvlad
Copy link
Author

archvlad commented Oct 6, 2023

I think the problem is around these lines of code:

const proxyResponsePromise = parseProxyResponse(socket);
socket.write(`${payload}\r\n`);
const { connect, buffered } = await proxyResponsePromise;
req.emit('proxyConnect', connect);
this.emit('proxyConnect', connect, req);

In socks-proxy-agent, by the way, timeout works fine. This is the snippet where timeout implemented.

debug('Creating socks proxy connection: %o', socksOpts);
const { socket } = await SocksClient.createConnection(socksOpts);
debug('Successfully created socks proxy connection');
if (timeout !== null) {
socket.setTimeout(timeout);
socket.on('timeout', () => cleanup());
}

When timeout is over socks_1.SocksClient.createConnection(socksOpts)) throws this error:

SocksClientError: Proxy connection timed out

And cleanup() function is called.

However, in https-proxy-agent I don't see such mechanism.

@aalgogiver
Copy link

aalgogiver commented Oct 26, 2023

Agree, socket hangs indefinitely in my case too.
Timeout option is not taken into consideration by the implementation.

@archvlad , the fix seems to be pretty straightforward.
Would you like to suggest a PR for this?

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

No branches or pull requests

2 participants