Skip to content

Fix TwoWayAgent deadlock in TaskCompletionSource constructor#56

Merged
fw2568 merged 2 commits intomainfrom
fix/agent-deadlock
Apr 10, 2026
Merged

Fix TwoWayAgent deadlock in TaskCompletionSource constructor#56
fw2568 merged 2 commits intomainfrom
fix/agent-deadlock

Conversation

@fw2568
Copy link
Copy Markdown
Contributor

@fw2568 fw2568 commented Apr 10, 2026

Summary

  • Fix TaskContinuationOptionsTaskCreationOptions in TwoWayAgent.Tell() — the wrong enum was silently boxed as object state instead of configuring async continuations, causing deadlocks when continuations call Tell() on the same agent.
  • Add regression test that reproduces the deadlock scenario.

Fixes #55

Test plan

  • Test deadlocks without fix (3s timeout, assertion failure)
  • Test passes with fix (21ms)
  • All 30 existing tests still pass

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a deadlock in TwoWayAgent.Tell() by correctly configuring TaskCompletionSource continuations to run asynchronously, and adds a regression test that reproduces the problematic synchronous-continuation scenario.

Changes:

  • Replace incorrect TaskContinuationOptions.RunContinuationsAsynchronously argument with TaskCreationOptions.RunContinuationsAsynchronously when constructing TaskCompletionSource in TwoWayAgent.Tell().
  • Add a regression test that forces a synchronous continuation to call Tell() again and validates it completes without blocking.
  • Normalize the test project file header (BOM removal / formatting-only change).

Reviewed changes

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

File Description
src/Dbosoft.Functional/Agent.cs Fixes TaskCompletionSource construction so continuations don’t run inline on the ActionBlock thread.
test/Dbosoft.Functional.Tests/TwoWayAgentTests.cs Adds regression coverage for the deadlock scenario via a synchronous continuation calling Tell() again.
test/Dbosoft.Functional.Tests/Dbosoft.Functional.Tests.csproj Formatting-only change to the project file header.

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

Comment thread src/Dbosoft.Functional/Agent.cs Outdated
Comment thread test/Dbosoft.Functional.Tests/TwoWayAgentTests.cs Outdated
…ource constructor

TaskCompletionSource<T> was created with TaskContinuationOptions.RunContinuationsAsynchronously
which silently matched the (object? state) overload instead of configuring async continuations.
This caused SetResult() to run continuations inline, deadlocking when a continuation called
Tell() on the same agent.

Fixes #55
@fw2568 fw2568 force-pushed the fix/agent-deadlock branch from 65e76c8 to 4597201 Compare April 10, 2026 21:17
- Fault or cancel the TaskCompletionSource when ActionBlock.Post() returns
  false, preventing callers from hanging indefinitely on a completed/canceled agent.
- Remove unused System.Threading using in test file.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


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

Comment thread src/Dbosoft.Functional/Agent.cs
@fw2568 fw2568 merged commit a376032 into main Apr 10, 2026
6 checks passed
@fw2568 fw2568 deleted the fix/agent-deadlock branch April 10, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TwoWayAgent uses TaskContinuationOptions instead of TaskCreationOptions causing synchronous continuation deadlocks

2 participants