theophilusx/ssh2-sftp-client

Opened too much file descriptors after "Write stream error" in _put() function.

Happy83 opened this issue · 4 comments

Hi guys,
I have a problem with bad permissions on target directory for function _put().
I receive message "_put: Write stream error: Permission denied" and the readStream for a local file isn't destroyed.
And then I see a many opened file descriptors and I get “Too many open files” error message in linux.

I think, the right solution is to destroy readStream in wtr.once('error', ... block.

What do you thinks?

Yeah, I'm talking about the read stream created inside the put method. When the write stream is rejected with error 'Write stream error', the read stream isn't destroyed.

I simulated this behavior with a simply loop every 5 seconds. I wanted to put the file from local computer to remote storage with no write permission to destination folder. Everytime I received message '_put: Write stream error: Permission denied'. That's ok. But...

When I call 'll /proc/[pid]/fd' I see a lot of rows with the source file. Every 5 seconds increased by new opening for reading.

So, I tried to add this code after your wtr.once('error', (err) => { on row 715 (index.js):

if (rdr) {
    rdr.destroy()
}

And voila, it's worked. No one new file descriptor stay opened.

I have pushed a fix for this issue into the master branch.