-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid NullReferenceException in TcpClient.EndConnect #899
Conversation
When the timeout is shorter than the time TcpClient.ConnectAsync takes to throw The task.Wait returns before the end of the task, then dispose the Tcpclient before the end of its ConnectAsync call
Could you possibly make a unit test to demonstrate this? |
It actually only throws a null reference exception internally on the task that is not awaited anymore, because the TcpClient have been disposed before the end of its ConnectAsync method. I did a small repro case, but I don't see how that could fit in a unit test
|
@@ -394,16 +394,17 @@ public virtual void open(Srv s, Options options) | |||
if (!task.Wait(TimeSpan.FromMilliseconds(options.Timeout))) | |||
{ | |||
nce = new NATSConnectionException("timeout"); | |||
task.ContinueWith(t => close(client)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This I understand. The task is still running even though we aren't waiting for it. Since we timed-out we don't want the connection, so close it.
} | ||
} | ||
catch (Exception e) | ||
{ | ||
nce = new NATSConnectionException(e.Message); | ||
close(client); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it possible that the same thing happens? Maybe a better question is when would an exception be thrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an exception is thrown here, it means that the task is completed, and that it ended in an exception.
That's why we can safely dispose the TcpClient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if the task of making the tcp client failed, is there anything to safely close?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It disposes the TcpClient and its underlying Socket in a timely manner, after the connection failed.
If not disposed here, it is bound to be disposed at some point by the GC because the reference is not rooted, but it is always better to avoid unpredictable behaviors.
Can we wrap it in an exception handler just so it doesn't fail. It's probably overkill but doesn't really hurt anything. Other than that this is ready to merge. |
It looks overkill, yes. Also this hasn't been sufficiently tested to be released as is, there would be the need to test connectivity on a mix of multiple reachable and non-reachable node list. |
When the timeout is shorter than the time TcpClient.ConnectAsync takes to throw
The task.Wait returns before the end of the task, then dispose the Tcpclient before the end of its ConnectAsync call