Skip to content

Commit 39ee1cc

Browse files
committed
https://trello.com/c/UU52dZPd Attempt to catch unhandled errors, and log them better
1 parent 3b064cf commit 39ee1cc

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

ProReception.DistributionServerInfrastructure/HostedServices/SignalRHostedService.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public Task StartAsync(CancellationToken cancellationToken)
3535
{
3636
startUpTask = ExecuteStartUp(stoppingCts.Token);
3737

38+
// Observe exceptions from the background task to prevent unobserved task exceptions from crashing the service
39+
_ = startUpTask.ContinueWith(task =>
40+
{
41+
if (task.IsFaulted && task.Exception != null)
42+
{
43+
logger.LogError(task.Exception, "Fatal error in {ServiceName} startup - service may be in degraded state", typeof(T).Name);
44+
}
45+
}, TaskScheduler.Default);
46+
3847
return Task.CompletedTask;
3948
}
4049

@@ -124,11 +133,13 @@ private async Task LoginAndCreateSignalRConnection(CancellationToken cancellatio
124133
}
125134
catch (Exception ex)
126135
{
127-
logger.LogWarning(ex, "SignalR AccessTokenProvider: failed to refresh token");
128-
// Return existing token (may fail with 401 and trigger reconnect), or null if missing
136+
logger.LogError(ex, "SignalR AccessTokenProvider: CRITICAL - failed to refresh token, using existing token");
137+
// Return existing token (may fail with 401 and trigger reconnect)
138+
// Don't throw - let SignalR handle the authentication failure and reconnect
129139
if (string.IsNullOrWhiteSpace(current.AccessToken))
130140
{
131-
return null;
141+
logger.LogError("SignalR AccessTokenProvider: No valid token available, returning empty string");
142+
return string.Empty;
132143
}
133144
}
134145
}

0 commit comments

Comments
 (0)