Skip to content
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

Fix memory leak on timed out connection attempt #278

Merged
merged 3 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions NATS.Client/Conn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ internal void open(Srv s, int timeoutMillis)
task.ContinueWith(t => GC.KeepAlive(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
if (!task.Wait(TimeSpan.FromMilliseconds(timeoutMillis)))
{
close(client);
client = null;
throw new NATSConnectionException("timeout");
}
Expand Down Expand Up @@ -467,7 +468,7 @@ private static bool remoteCertificateValidation(
return false;
}

internal void closeClient(TcpClient c)
internal static void close(TcpClient c)
{
if (c != null)
{
Expand Down Expand Up @@ -500,7 +501,8 @@ internal void makeTLS(Options options)
}
catch (Exception ex)
{
closeClient(client);
sslStream.Dispose();
close(client);
throw new NATSConnectionException("TLS Authentication error", ex);
}
}
Expand Down Expand Up @@ -540,7 +542,7 @@ internal void teardown()
s.Dispose();

if (c != null)
closeClient(c);
close(c);
}
catch (Exception) { }
}
Expand Down Expand Up @@ -601,7 +603,7 @@ void Dispose(bool disposing)
if (stream != null)
stream.Dispose();
if (client != null)
closeClient(client);
close(client);

disposedValue = true;
}
Expand Down
27 changes: 27 additions & 0 deletions NATSUnitTests/UnitTestTLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,33 @@ public void TestTlsFailWithCert()
}
}

// Test verfier to fail on the server cert.
//
private bool verifyCertAlwaysFail(object sender,
X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return false;
}

[Fact]
public void TestTlsFailWithInvalidServerCert()
{
using (NATSServer srv = util.CreateServerWithConfig("tls_1222_verify.conf"))
{
Options opts = util.DefaultTestOptions;
opts.Secure = true;
opts.Url = "nats://localhost:1222";
opts.TLSRemoteCertificationValidationCallback = verifyCertAlwaysFail;

// this will fail, because it's not complete - missing the private
// key.
opts.AddCertificate(UnitTestUtilities.GetFullCertificatePath("client-cert.pem"));

Assert.ThrowsAny<NATSException>(() => new ConnectionFactory().CreateConnection(opts));
}
}

[Fact]
public void TestTlsFailWithBadAuth()
{
Expand Down