mscdex / node-ftp

An FTP client module for node.js
MIT License
1.13k stars 243 forks source link

Destroy not triggering close #142

Open eshober opened 8 years ago

eshober commented 8 years ago

Sometimes my connection hangs while getting a file and I'd like to timeout by calling destroy to trigger a close event. When I do this, close is not called right away and sometimes not at all.

`var ftp = require('ftp'); var fs = require('fs');

var connect = function(server, user, pwd) { var client = new ftp();

client.on('ready', function() {
    client.list(function (err, list) {
        if (err) {
            console.log('Error listing files');
            return;
        }

        if (list && list.length > 0) {
            var file = list[0].name;

            var timeout = setTimeout(function() {
                console.log('Download of file ' + file + ' timed out!');
                client.destroy();
            }, 15 * 1000);

            console.log('Downloading file ' + file + '...');

            client.get(file, function (err, stream) {

                if (err) {
                    console.log('Error downloading file ' + file);
                    client.end();
                } else {

                    stream.on('close', function () {

                        clearTimeout(timeout);

                        console.log('Downloaded file ' + file);
                    });

                    stream.pipe(fs.createWriteStream(file));

                    client.end();
                }
            });
        }
    })
});

client.on('close', function(err) {
    console.log('Closed FTP connection');
    process.exit(0);
});

client.connect({
    host: server,
    user: user,
    password: pwd
});

};

connect(process.env.FTP_SERVER_1, process.env.FTP_USER_1, process.env.FTP_PWD_1);`

eshober commented 8 years ago

Note: I set the time out value low to show the problem. Of course I give more time during normal run.