Skip to content

Commit 1a7d9ca

Browse files
committed
Handle Post() failure in TwoWayAgent.Tell() and remove unused using
- 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.
1 parent 4597201 commit 1a7d9ca

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/Dbosoft.Functional/Agent.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,16 @@ await process(state, t.Item1)
138138

139139
public Task<TReply> Tell(TMsg message)
140140
{
141-
var tcs = new TaskCompletionSource<TReply>(TaskCreationOptions.RunContinuationsAsynchronously);
142-
_actionBlock.Post((message, tcs));
143-
141+
var tcs = new TaskCompletionSource<TReply>(TaskCreationOptions.RunContinuationsAsynchronously);
142+
143+
if (!_actionBlock.Post((message, tcs)))
144+
{
145+
if (_cancellationToken.IsCancellationRequested)
146+
tcs.SetCanceled();
147+
else
148+
tcs.SetException(new InvalidOperationException("The agent is no longer accepting messages."));
149+
}
150+
144151
return tcs.Task;
145152
}
146153
}

test/Dbosoft.Functional.Tests/TwoWayAgentTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Threading;
32
using System.Threading.Tasks;
43
using Dbosoft.Functional;
54
using Xunit;

0 commit comments

Comments
 (0)