From f56828b91f9cef602a5a40f9e0a5865342ef4fe8 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Mon, 7 Feb 2022 13:23:12 +0100 Subject: [PATCH] Dispose CancellationTokenSource --- FluentFTP/Streams/FtpSocketStream.cs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/FluentFTP/Streams/FtpSocketStream.cs b/FluentFTP/Streams/FtpSocketStream.cs index 1ca7c44fa..81f001dac 100644 --- a/FluentFTP/Streams/FtpSocketStream.cs +++ b/FluentFTP/Streams/FtpSocketStream.cs @@ -979,16 +979,18 @@ public async Task ConnectAsync(string host, int port, FtpIpVersion ipVersions, C #if CORE if (this.ConnectTimeout > 0) { - var timeoutSrc = new CancellationTokenSource(); - timeoutSrc.CancelAfter(this.ConnectTimeout); - - var tokenSrc = CancellationTokenSource.CreateLinkedTokenSource( - token, timeoutSrc.Token); - token = tokenSrc.Token; + using (var timeoutSrc = CancellationTokenSource.CreateLinkedTokenSource(token)) + { + timeoutSrc.CancelAfter(this.ConnectTimeout); + await EnableCancellation(m_socket.ConnectAsync(addresses[i], port), timeoutSrc.Token, () => CloseSocket()); + break; + } + } + else + { + await EnableCancellation(m_socket.ConnectAsync(addresses[i], port), token, () => CloseSocket()); + break; } - - await EnableCancellation(m_socket.ConnectAsync(addresses[i], port), token, () => CloseSocket()); - break; #else var connectResult = m_socket.BeginConnect(addresses[i], port, null, null); await EnableCancellation(Task.Factory.FromAsync(connectResult, m_socket.EndConnect), token, () => CloseSocket());