From 3e792a41e097ba408dc3cab47ce9ee182df6f81f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 28 Jul 2024 17:01:52 +0000 Subject: [PATCH] Catch exceptions on AsyncTCPConnector_AsyncWrapper background thread Since it spawns a new thread on each attempt, uncaught exceptions were previously undetected, and caused silent failures down the pipe. This should make it easier to track such bugs down. --- .../PubSub/Client/Routing/PubSubRouter.cs | 4 ++-- .../ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs b/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs index 339b320dc..f0685ac08 100644 --- a/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs +++ b/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs @@ -49,7 +49,7 @@ namespace BCG { public RemoteConnectionInfo get_pub_route_for_tag(string tag) { if (!_pub_routes.TryGetFirst(r => r?.IsMatch(tag) ?? false, out var route)) { - throw new ArgumentException($"Failed to find route for tag '{tag}'", nameof(tag)); + Log.assert(false, $"Failed to find route for tag '{tag}'", nameof(tag)); } Log.Gen.info($"PubSubRouter.get_sub_url_for_tag tag: {tag} route: {JsonSerializer.Serialize(route)}"); ArgumentNullException.ThrowIfNull(route.ServerAddress); @@ -58,7 +58,7 @@ namespace BCG { public RemoteConnectionInfo get_sub_route_for_tag(string tag) { if (!_sub_routes.TryGetFirst(r => r?.IsMatch(tag) ?? false, out var route)) { - throw new ArgumentException($"Failed to find route for tag '{tag}'", nameof(tag)); + Log.assert(false, $"Failed to find route for tag '{tag}'", nameof(tag)); } Log.Gen.info($"PubSubRouter.get_sub_url_for_tag tag: {tag} route: {JsonSerializer.Serialize(route)}"); ArgumentNullException.ThrowIfNull(route.ServerAddress); diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs index af474dbfa..6995ba8dd 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs @@ -19,7 +19,16 @@ namespace bts { bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper.connect_to server: {server_address}"); //Task.Run(() => _connect_to(server_address, handler)); ThreadPool.QueueUserWorkItem(_ => _keep_trying_blocking_function( - () => _connect_to(server_address, handler), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5))); + Connect, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5))); + + bool Connect() { + try { + return _connect_to(server_address, handler); + } catch (Exception ex) { + Log.assert(false, $"AsyncTCPConnector_AsyncWrapper connect threw exception: {ex}"); + } + return false; + } } // this is the actual fuction thta calls TCPCLient sync methof and establishes a connection