ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

discussion: timeout handling using AbortController

Open iambighead opened this issue 2 years ago • 1 comments

Not sure if this is discussed before. Can ssh2 support AbortController so certain operations can be cancellable due to say timeout?

Currently I implemented a timeout before invoking FastGet. But while the main app timeout the operation (do not wait for it to finish), I believe FastGet actually is still running. So I am looking for a way to cancel the operation when timeout, without terminating the connection. The advantage is I can reduce unnecessary bandwidth usage, and clean up the file without the file being locked.

Thanks for your time.

iambighead avatar May 19 '23 00:05 iambighead

One option to get cancel/timeout to work with fastGet/Put, would be to modify opts.step() in fastXfer() to error if step() returns true.

https://github.com/mscdex/ssh2/blob/281c290fc706ed3c2215f5e80a90a2e2498368f3/lib/protocol/SFTP.js#LL2331C48-L2331C48

e.g. Change (In ssh2 library):

             onstep && onstep(total, nb, fsize);

To:

              // Abort if onstep returns true
              if (onstep && onstep(total, nb, fsize)) {
                return onerror(new Error('Cancelled by user'));
              }

Then you could use the following (application code) to cancel fastGet()/fastPut() mid-transfer.

            var opts = {
                step: (transferred, chunk, total)=>{
                    if (canceled())
                        return true;
                }
            };
            fastGet(..., opts);

But could be a breaking change if someone's incorrectly returning a value from step(), though.

It would also be handy to expose fastXfer() in the public API, so you can copy files between ftp servers. fastGet/Put() only work local->remote or remote->local, but sometimes you may need to do remote->remote.

BitStream1 avatar Jun 03 '23 20:06 BitStream1