Skip to content

Commit 1465e58

Browse files
committed
Catch rare ObjectDisposedException when connecting.
This seems like it could be caused by a rare race condition when timeout occurs during connection; see #1033 (comment). Signed-off-by: Bradley Grainger <[email protected]>
1 parent e8241a5 commit 1465e58

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,24 @@ private async Task<bool> OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer
10241024
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "Connect Timeout expired.");
10251025
}
10261026

1027-
HostName = hostName;
1028-
m_tcpClient = tcpClient;
1029-
m_socket = m_tcpClient.Client;
1030-
m_socket.NoDelay = true;
1031-
m_stream = m_tcpClient.GetStream();
1032-
m_socket.SetKeepAlive(cs.Keepalive);
1027+
try
1028+
{
1029+
HostName = hostName;
1030+
m_tcpClient = tcpClient;
1031+
m_socket = m_tcpClient.Client;
1032+
m_socket.NoDelay = true;
1033+
m_stream = m_tcpClient.GetStream();
1034+
m_socket.SetKeepAlive(cs.Keepalive);
1035+
}
1036+
catch (ObjectDisposedException) when (cancellationToken.IsCancellationRequested)
1037+
{
1038+
Utility.Dispose(ref m_stream);
1039+
SafeDispose(ref m_tcpClient);
1040+
SafeDispose(ref m_socket);
1041+
Log.Info("Session{0} connect timeout expired connecting to IpAddress {1} for HostName '{2}'", m_logArguments[0], ipAddress, hostName);
1042+
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "Connect Timeout expired.");
1043+
}
1044+
10331045
lock (m_lock)
10341046
m_state = State.Connected;
10351047
Log.Trace("Session{0} connected to IpAddress {1} for HostName '{2}' with local Port {3}", m_logArguments[0], ipAddress, hostName, (m_socket.LocalEndPoint as IPEndPoint)?.Port);

0 commit comments

Comments
 (0)