TransferClient.LoginAsync() causes an UnobservedTaskException

0 votes
asked Nov 22, 2021 by jon.reis (140 points)
edited Nov 22, 2021 by jon.reis

Rebex Library: Latest on nuget

My scenario is this. I have a FileTransferClient mClient connecting to an FTP server where the username is set, but the password is incorrect. What I am seeing is that mClient.LoginAsync does not throw. It happily returns with IsAuthenticated = false. Later on I get an UnobservedTaskException in my UnobservedTaskException handler with the following stack trace:

021-11-19 14:47:14.6287 (5156.2) Debug [Initializer] Unexpected logic error [AggregateException] A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.
-->[FtpException] Login or password incorrect! (530).
--> at Rebex.Net.Ftp.aqhpm(String p0, String p1, String p2)
--> at Rebex.Net.Ftp.jnmvq(Object p0, Enum p1, Object[] p2)
--> at owtsr.ucnia.nmvkt(Object p0). Please contact

  await m_Client.ConnectAsync(m_Configuration.Host, m_Configuration.Port.Value,(FileTransferMode) m_Configuration.FileTransferType);

  // this is not throwing when there is a login error
  await m_Client.LoginAsync(m_Configuration.User, m_Configuration.Password);

What I am assuming is happening is that your library is not awaiting a task inside LoginAsync. The language in the help documention for LoginAsync seems to imply that this is only beginning an async operation which is not what one would expect from an Async method. Perhaps this is a hold-over from when this was BeginLogin?

"Begins asynchronous Login operation. Authenticates the user to the FTP server."

Lastly, if I replace mClient.LoginAsync with the sync mClient.Login, I get an exception thrown as one would expect. I would expect that awaiting LoginAsync should have the same behavior as the sync method Login.

Could you please have a look at this and let me know if I should be doing something differently.

Applies to: Rebex FTP/SSL

1 Answer

0 votes
answered Nov 22, 2021 by Lukas Pokorny (128,250 points)

Thanks for letting us know about this. It looks like error handling in FileTransferClient.LoginAsync method is wrong. We'll fix this and send you a link to a hotfix. Sorry for the inconvenience!

commented Nov 22, 2021 by jon.reis (140 points)
No worries. Thanks for taking a look at it.
commented Nov 22, 2021 by Lukas Pokorny (128,250 points)
I just sent a link to a pre-release build to your e-mail, please give it a try and let me know whether it resolves the issue.
commented Nov 22, 2021 by jon.reis (140 points)
Thank you for the quick turnaround Lukas. I can confirm this fixes the issue. I get the exception while awaiting LoginAsync()
commented Nov 22, 2021 by jon.reis (140 points)
Would it be possible for you to add this to nuget as a pre-release package so we can release a version of our product with the fix?
commented Nov 23, 2021 by Lukas Pokorny (128,250 points)
Thanks for testing! We plan to publish the next release tomorrow, so hopefully a pre-release won't be needed.
commented Nov 24, 2021 by Lukas Pokorny (128,250 points)
commented Nov 24, 2021 by jon.reis (140 points)
Thank you for the fix Lukas. Much appreciated.
I must admit though that I am a little taken that these calls are not truly asynchronous.
commented Nov 29, 2021 by Lukas Pokorny (128,250 points)
I understand. Sadly, there has been one major practical reason for keeping the synchronous code - we still maintain a single code branch for the current mainstream .NET platforms and legacy platforms such as .NET Compact Framework 3.5 or .NET 2.0. We did experiment a bit with asynchronous code on these platforms, but it turned out asynchronous code and legacy platforms was not a good match at all. We will finally drop support for these old platforms in R7.0 next year - then, we will finally be free to gradually rewrite those hundreds of thousands lines of code to eventually reach true asynchronicity.