From 20d0a81f10aba3f948eb01c142fab7d377b4364a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Kle=C5=A1tinec?= Date: Mon, 2 Oct 2023 12:38:17 +0200 Subject: [PATCH 1/2] Prevent UnobservedTaskException in LeaderElector --- src/KubernetesClient/LeaderElection/LeaderElector.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/KubernetesClient/LeaderElection/LeaderElector.cs b/src/KubernetesClient/LeaderElection/LeaderElector.cs index 3d8d3c318..d80738ff1 100644 --- a/src/KubernetesClient/LeaderElection/LeaderElector.cs +++ b/src/KubernetesClient/LeaderElection/LeaderElector.cs @@ -211,8 +211,11 @@ private async Task AcquireAsync(CancellationToken cancellationToken) // wait RetryPeriod since acq return immediately await Task.Delay(delay, cancellationToken).ConfigureAwait(false); } - - // else timeout + else + { + // else timeout + _ = acq.ContinueWith(t => _ = t.Exception /* prevent UnobservedTaskException */, TaskContinuationOptions.OnlyOnFaulted); + } } finally { From 8cdac30783ad54fad689f186d60960c60614d946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Kle=C5=A1tinec?= Date: Sun, 8 Oct 2023 09:05:26 +0200 Subject: [PATCH 2/2] Loggin exception --- src/KubernetesClient/LeaderElection/LeaderElector.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/KubernetesClient/LeaderElection/LeaderElector.cs b/src/KubernetesClient/LeaderElection/LeaderElector.cs index d80738ff1..d78a967ba 100644 --- a/src/KubernetesClient/LeaderElection/LeaderElector.cs +++ b/src/KubernetesClient/LeaderElection/LeaderElector.cs @@ -214,7 +214,7 @@ private async Task AcquireAsync(CancellationToken cancellationToken) else { // else timeout - _ = acq.ContinueWith(t => _ = t.Exception /* prevent UnobservedTaskException */, TaskContinuationOptions.OnlyOnFaulted); + _ = acq.ContinueWith(t => OnError?.Invoke(t.Exception), TaskContinuationOptions.OnlyOnFaulted); } } finally