Skip to content

Commit

Permalink
Avoid NullReferenceException in TcpClient.EndConnect
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Paul BARRY DELONGCHAMPS committed Jun 12, 2024
1 parent 711388d commit cbd9c37
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/NATS.Client/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

if (nce != null)
{
close(client);
client = null;
throw nce;
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit cbd9c37

Please sign in to comment.