Skip to content

Add ObserveException extension methods for Task and Task<T> - #92

Merged
Tyrrrz merged 11 commits into
primefrom
copilot/add-task-extension-method
Jul 22, 2026
Merged

Add ObserveException extension methods for Task and Task<T>#92
Tyrrrz merged 11 commits into
primefrom
copilot/add-task-extension-method

Conversation

Copilot AI commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Detached (fire-and-forget) tasks that fault produce unobserved task exceptions, which can crash the process or generate noise via TaskScheduler.UnobservedTaskException. This adds a lightweight way to explicitly suppress them when the exception cannot be meaningfully handled.

Changes

  • TaskExtensions — adds ObserveException() for both Task and Task<T>, registering an OnlyOnFaulted continuation that reads t.Exception to mark it observed
  • TaskExtensionsTests — covers faulted and successful cases for both task types

Usage

// Fire-and-forget task where the exception is intentionally discarded
Task.Run(() => DoSomethingAsync()).ObserveException();

Comment thread PowerKit/Extensions/TaskExtensions.cs Outdated
Copilot AI requested a review from Tyrrrz July 22, 2026 14:30
@Tyrrrz
Tyrrrz marked this pull request as ready for review July 22, 2026 14:31
Copilot AI review requested due to automatic review settings July 22, 2026 14:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces ObserveException() extension methods on Task and Task<T> to explicitly observe (and thereby suppress) unobserved task exceptions for detached/fire-and-forget tasks, and adds tests intended to validate faulted and successful paths.

Changes:

  • Added Task.ObserveException() and Task<T>.ObserveException() to observe t.Exception via a continuation.
  • Added unit tests covering faulted and successful cases for both Task and Task<T>.
  • Added additional tests intended to ensure no unobserved-task-exception behavior occurs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
PowerKit/Extensions/TaskExtensions.cs Adds ObserveException() overloads for Task and Task<T> via a continuation that reads t.Exception.
PowerKit.Tests/Extensions/TaskExtensionsTests.cs Adds tests for faulted/successful cases and attempts to test unobserved-exception behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/TaskExtensions.cs
Comment thread PowerKit/Extensions/TaskExtensions.cs Outdated
Comment thread PowerKit.Tests/Extensions/TaskExtensionsTests.cs Outdated
Comment thread PowerKit.Tests/Extensions/TaskExtensionsTests.cs Outdated
Tyrrrz and others added 4 commits July 22, 2026 17:38
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Tyrrrz

Tyrrrz commented Jul 22, 2026

Copy link
Copy Markdown
Owner

@copilot do we actually need separate implementations for Task/Task<T>?

Copilot AI commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@copilot do we actually need separate implementations for Task/Task<T>?

No — separate implementations aren’t needed. Task<T> inherits from Task, so the Task receiver extension applies to both. I removed the duplicate generic overload in 1da1c7a.

@Tyrrrz

Tyrrrz commented Jul 22, 2026

Copy link
Copy Markdown
Owner

@copilot please fix build errors

Copilot AI commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please fix build errors

Fixed in 37220ad. The failing CS4014 warning in TaskExtensionsTests is resolved by explicitly discarding the returned task (_ = ...ObserveException()), and formatting is now clean for that file.

@Tyrrrz
Tyrrrz merged commit 9f04c2a into prime Jul 22, 2026
7 checks passed
@Tyrrrz
Tyrrrz deleted the copilot/add-task-extension-method branch July 22, 2026 18:15
@codecov

codecov Bot commented Jul 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.18%. Comparing base (c168afd) to head (37220ad).
⚠️ Report is 1 commits behind head on prime.

Additional details and impacted files
@@            Coverage Diff             @@
##            prime      #92      +/-   ##
==========================================
+ Coverage   87.13%   87.18%   +0.05%     
==========================================
  Files          75       76       +1     
  Lines        1352     1358       +6     
  Branches      236      236              
==========================================
+ Hits         1178     1184       +6     
  Misses        122      122              
  Partials       52       52              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants