amai-brat opened a new issue, #54: URL: https://github.com/apache/activemq-nms-openwire/issues/54
## Description Every failed TCP connection attempt leaks a **`TaskScheduler.UnobservedTaskException`**. When the broker is unreachable (connection refused), `TcpTransportFactory.TryConnectSocket` calls `Socket.BeginConnect(...)` but never calls the matching `Socket.EndConnect(...)`, so the connect failure is never observed. More info [here](https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.beginconnect?view=net-10.0) in remarks. ## Version / Environment - `Apache.NMS.ActiveMQ`: `2.3.0` (affects other versions too) - Reproduced on .NET 10 (SDK 10.0.112, Linux). ## Steps to reproduce 1. Create console app with `Apache.NMS.ActiveMQ` 2. Program.cs ```csharp using Apache.NMS; var unobserved = 0; TaskScheduler.UnobservedTaskException += (s, e) => { Interlocked.Increment(ref unobserved); Console.WriteLine("UNOBSERVED: " + e.Exception.GetBaseException()); e.SetObserved(); }; for (var i = 0; i < 2; i++) { try { var factory = new NMSConnectionFactory("activemq:tcp://localhost:61999"); var conn = factory.CreateConnection(); conn.Start(); conn.Close(); } catch (Exception ex) { Console.WriteLine($"caught {ex.GetType().Name}"); } // Unobserved task exceptions only surface when the faulted task is finalized. GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Thread.Sleep(50); } Console.WriteLine($"Captured {unobserved} unobserved exceptions."); ``` 3. Run (ActiveMQ is not running) ## Actual behaviour ``` caught NMSConnectionException UNOBSERVED: System.Net.Sockets.SocketException (111): Connection refused at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state) UNOBSERVED: System.Net.Sockets.SocketException (111): Connection refused at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state) caught NMSConnectionException UNOBSERVED: System.Net.Sockets.SocketException (111): Connection refused at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state) UNOBSERVED: System.Net.Sockets.SocketException (111): Connection refused at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state) Captured 4 unobserved exceptions. ``` ## Expected behaviour ``` caught NMSConnectionException caught NMSConnectionException Captured 0 unobserved exceptions. ``` ## How to fix ```diff Subject: [PATCH] Fix async socket connection --- Index: src/Transport/Tcp/TcpTransportFactory.cs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/Transport/Tcp/TcpTransportFactory.cs b/src/Transport/Tcp/TcpTransportFactory.cs --- a/src/Transport/Tcp/TcpTransportFactory.cs (revision 8ed7e410228868117df86aa70007cca2b58cd944) +++ b/src/Transport/Tcp/TcpTransportFactory.cs (date 1793090004492) @@ -256,10 +256,19 @@ result.AsyncWaitHandle.WaitOne(ConnectTimeout, true); if(!socket.Connected) { - socket.Close(); + try + { + socket.EndConnect(result); + } + catch + { + } + + socket.Close(); } else { + socket.EndConnect(result); return socket; } } ``` I can create PR, if you approve that this is a bug -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information, visit: https://activemq.apache.org/contact
