-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
nce = new NATSConnectionException(e.Message); | ||
close(client); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 (nce != null) | ||
{ | ||
close(client); | ||
client = null; | ||
throw nce; | ||
} | ||
|
@@ -435,11 +436,10 @@ private static bool remoteCertificateValidation( | |
public virtual void close(TcpClient c) | ||
{ | ||
#if NET46 | ||
c?.Close(); | ||
c?.Close(); | ||
#else | ||
c?.Dispose(); | ||
#endif | ||
c = null; | ||
} | ||
|
||
public void makeTLS() | ||
|
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.