Skip to content

Commit

Permalink
Avoid NullReferenceException in TcpClient.EndConnect (#899)
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

Co-authored-by: Paul BARRY DELONGCHAMPS <[email protected]>
  • Loading branch information
Paciv and Paul BARRY DELONGCHAMPS authored Jul 8, 2024
1 parent 26f64a9 commit e5aaea9
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 e5aaea9

Please sign in to comment.