Skip to content

Commit e8664e5

Browse files
authored
Send close_notify TLS alert on connection shutdown (#5995)
Fixes #5994
1 parent 3505dc3 commit e8664e5

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Npgsql/Internal/NpgsqlConnector.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,10 +2189,25 @@ void FullCleanup()
21892189
/// Closes the socket and cleans up client-side resources associated with this connector.
21902190
/// </summary>
21912191
/// <remarks>
2192-
/// This method doesn't actually perform any meaningful I/O, and therefore is sync-only.
2192+
/// This method doesn't actually perform any meaningful I/O (except sending TLS alert), and therefore is sync-only.
21932193
/// </remarks>
21942194
void Cleanup()
21952195
{
2196+
if (_stream is SslStream sslStream)
2197+
{
2198+
try
2199+
{
2200+
// Send close_notify TLS alert to correctly close connection on postgres's side
2201+
sslStream.ShutdownAsync().GetAwaiter().GetResult();
2202+
// Theoretically we should do a 0 read here to receive server's close_notify alert
2203+
// But overall it doesn't look like it makes much of a difference
2204+
}
2205+
catch
2206+
{
2207+
// ignored
2208+
}
2209+
}
2210+
21962211
try
21972212
{
21982213
_stream?.Dispose();

0 commit comments

Comments
 (0)