Fix DeferAsync async handler nesting bug in CommandAsync#7999
Merged
Arkatufus merged 4 commits intoJan 21, 2026
Conversation
…7998) DeferAsync with an async handler threw "RunTask calls cannot be nested" when called from within a CommandAsync handler after an await, when there were no pending persist operations. The fix removes the immediate RunTask execution optimization for async handlers and always queues them to _pendingInvocations. This avoids the nesting issue since the handler executes after the command handler completes. Added regression tests to verify the fix.
Aaronontheweb
commented
Jan 21, 2026
Member
Author
Aaronontheweb
left a comment
There was a problem hiding this comment.
Detailed my changes
|
|
||
| if (_pendingInvocations.Count == 0) | ||
| { | ||
| RunTask(() => handler(evt)); |
Member
Author
There was a problem hiding this comment.
This was my bad - added this as part of #7937 , but this was wrong
Aaronontheweb
added a commit
to Aaronontheweb/akka.net
that referenced
this pull request
Jan 26, 2026
…7998) (akkadotnet#7999) DeferAsync with an async handler threw "RunTask calls cannot be nested" when called from within a CommandAsync handler after an await, when there were no pending persist operations. The fix removes the immediate RunTask execution optimization for async handlers and always queues them to _pendingInvocations. This avoids the nesting issue since the handler executes after the command handler completes. Added regression tests to verify the fix.
Merged
Aaronontheweb
added a commit
that referenced
this pull request
Jan 26, 2026
DeferAsync with an async handler threw "RunTask calls cannot be nested" when called from within a CommandAsync handler after an await, when there were no pending persist operations. The fix removes the immediate RunTask execution optimization for async handlers and always queues them to _pendingInvocations. This avoids the nesting issue since the handler executes after the command handler completes. Added regression tests to verify the fix.
This was referenced Jan 27, 2026
This was referenced Feb 9, 2026
This was referenced Feb 20, 2026
This was referenced May 21, 2026
Open
Closed
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7998
DeferAsyncwith async handler throwing "RunTask calls cannot be nested" when called from within aCommandAsynchandler after anawait, when there are no pending persist operationsRoot Cause
The async handler version of
DeferAsynchad an optimization that immediately executed the handler viaRunTask()when_pendingInvocations.Count == 0. When called from within aCommandAsynchandler (which is already in aRunTaskcontext), this caused the nesting exception.Fix
Always queue async handlers to
_pendingInvocationsinstead of attempting immediate execution. The handler executes viaPeekApplyHandlerafter the command handler completes, safely outside any existingRunTaskcontext.Test plan