Skip to content

Infinite timeout on async UploadFile with OpenAsync/CopyToAsync #1497

@Dunge

Description

@Dunge

I'm using the code here #819 (comment) to have an async upload file and it works.

Unfortunately, if I'm uploading a file and close the connection on the remote end, the method will never finish. I would be expecting some exception to be thrown. Instead the thread remains stuck indefinitely, and it's not a good thing for server-side process doing this operation in batches.

I found this decade old question and tried to set the OperationTimeout property to 30sec, but it doesn't seems to have any effect.

I'm aware I can have a CancellationToken, but this would be a way for me to trigger a cancellation from my end, not one that happens when the connection get closed remotely or if a timeout happen.

Activity

Rob-Hague

Rob-Hague commented on Sep 21, 2024

@Rob-Hague
Collaborator

I think the problem is that the non-async methods in SftpSession are checking on a few wait handles which can throw errors:

WaitOnHandle(wait, OperationTimeout);

public void WaitOnHandle(WaitHandle waitHandle, int millisecondsTimeout)
{
var waitHandles = new[]
{
_errorOccuredWaitHandle,
_sessionDisconnectedWaitHandle,
_channelClosedWaitHandle,
waitHandle
};

But the async methods are not. They should at least (do something like) subscribe to the same events where those wait handles are set

Dunge

Dunge commented on Sep 21, 2024

@Dunge
Author

Thank you very much for this first analysis, you seems to be on to something.

At least it confirms that it's not just me using the lib incorrectly or missing a configuration parameter somewhere.

I assume it's not as simple as adding that same call in the async methods since it's using streams and probably can't subscribe to events the same way. I would love to continue on your investigation work and submit a PR, but I unfortunately do not trust my knowledge enough to fix it properly.

Rob-Hague

Rob-Hague commented on Sep 23, 2024

@Rob-Hague
Collaborator

No problem, yeah I don't think it's quite that simple but I might have a solution which is not too dissimilar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @Rob-Hague@Dunge

      Issue actions

        Infinite timeout on async UploadFile with OpenAsync/CopyToAsync · Issue #1497 · sshnet/SSH.NET