AuthenticateAsServerAsync hangs

≯℡__Kan透↙ 提交于 2019-12-11 13:44:33

问题


I'm using

await stream.AuthenticateAsServerAsync(serverCert, true, SslProtocols.Tls, false);

In my application to secure a connection. Unfortunately in rare cases (roughly one out of 1000) this hangs completely. I tried to set timeouts on the lower level NetworkStream and the TcpClient.

But nothing works, those connections keep accumulating over time.

And by it hangs completely I mean it gets stuck inside there for days (if the application runs that long).

I currently solved this with a dirty hack. Im starting a new thread, wait 5seconds inside it, and if the call didnt return in time I call tcpClient.Close() to force the SslStream to abort.

Whats the issue here? What is the intended way to handle scenarios like this in general where you need a timeout on a function but there is no overload that takes a TimeSpan or CancellationToken?

Shouldnt the lower level timeouts on the tcpClient at least throw an exception? Why is that not happening either?


回答1:


Reason

Seems like the async versions of all network related stuff does not support timeouts. Thats why AuthenticateAsServerAsync does not support timeouts either.

From the documentation:

Note

This property affects only synchronous reads performed by calling the Read method. This property does not affect asynchronous reads performed by calling the BeginRead method.

How I solved it

Before I start AuthenticateAsServerAsync(...) I first start my own Task with an delay.

Task.Run(async() {
    await Task.Delay(TimeSpan.FromMinutes(1));
    tcpClient.Close();
});

If someone has a better way to do this please let me know :)



来源:https://stackoverflow.com/questions/37897425/authenticateasserverasync-hangs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!