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

request.setTimeout stopped working properly #186

Open
ibrod83 opened this issue Jan 17, 2022 · 4 comments
Open

request.setTimeout stopped working properly #186

ibrod83 opened this issue Jan 17, 2022 · 4 comments

Comments

@ibrod83
Copy link

ibrod83 commented Jan 17, 2022

I've been using version 1.13.0 until the recent security issue. After upgrading to 1.14.7, I've noticed some of my code that relies on follow-redirects fails. Specifically, scenarios where a timeout occurs during stream. Currently tested on node 14.17.0

How to reproduce:

create a server.js file. Note the artificial stream, that fakes a timeout between the packets:

const http = require('http');
const { Readable } = require('stream');

const server = http.createServer((req, res) => {
    const stream = streamWithFakeTimeout()
    stream.pipe(res);
});
server.listen(3000,()=>{
   
});

function streamWithFakeTimeout() {
    const stream= Readable.from((async function* () {
        for (let i = 0; i < 5; i++) {
            var view = new Uint8Array(1);           
            view[0] = 33
            if (i === 0) {//First packet is ok
                i++
                yield view;
            } else {//Then start jamming it.
                await fakeTimeout(2000)//Fake timeout during stream
                yield view;
            }
        }
    })());
    return stream;
}

function fakeTimeout(mil) {
    return new Promise((res) => {
        setTimeout(() => {
            res();
        }, mil)
    })
}

Create a client.js file:

const { http,} = require('follow-redirects');
const fs = require('fs');
// const  http = require('http');//If I use native http, it works as expected

request = http.request('http://localhost:3000', {}, (res) => {
  res.pipe(fs.createWriteStream('./file.txt'))
});

request.end()

request.setTimeout(1500, () => {
    request.abort()
    console.log('request timed out')
});

When follow-redirects is used, the callback of setTimeout doen't fire. The request just ends, and the file is downloaded partially, without any error.

However, when the native http.request function is used, the timeout event fires as expected.

This wasn't the situation in version 1.13.0. I don't know in which version it broke though.

Can you look into it?

@RubenVerborgh
Copy link
Collaborator

Thanks. Duplicate of #181?

@ibrod83
Copy link
Author

ibrod83 commented Jan 18, 2022

They are definitely related :D
Do you want me to close this one and just copy paste it to 181?

@RubenVerborgh
Copy link
Collaborator

It's okay; I will look at #181 and test if a fix also work with this one.

@ibrod83
Copy link
Author

ibrod83 commented Jun 17, 2022

It's okay; I will look at #181 and test if a fix also work with this one.

A reminder :D

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