Align waitforexternalevent waiter picking order to LIFO#509
Merged
YunchuWang merged 3 commits intomainfrom Nov 25, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR changes the external event waiter picking order from FIFO (First In First Out) to LIFO (Last In First Out) for the isolated worker implementation, aligning it with the in-process model behavior. This change makes it easier for users to abandon external events they no longer care about, particularly in scenarios using Task.WhenAny in loops.
- Replaced Queue-based event waiter storage with a custom Stack implementation using a single-linked list
- Added comprehensive integration tests validating LIFO behavior including cancellation scenarios, multiple waiters/events, and event buffering
- Updated existing test expectations to reflect LIFO ordering
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs | Refactored external event handling from Queue (FIFO) to Stack (LIFO) by changing the data structure from Dictionary<string, Queue<IEventSource>> to Dictionary<string, IEventSource> with linked list implementation, and updated event completion logic to pop from the top of the stack |
| src/Worker/Core/Shims/TaskOrchestrationContextWrapper.EventSource.cs | Added Next property to IEventSource interface and its implementation to support single-linked list structure for LIFO ordering |
| test/Grpc.IntegrationTests/OrchestrationPatterns.cs | Updated ExternalEventsInParallel test expectations to reflect LIFO behavior with detailed explanatory comments |
| test/Grpc.IntegrationTests/ExternalEventStackTests.cs | Added new comprehensive integration test file with four test cases covering LIFO behavior, cancellation handling (Issue #508), multiple events/waiters, and event buffering scenarios |
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
test/Grpc.IntegrationTests/ExternalEventStackTests.cs:179
- This assignment to metadata is useless, since its value is never read.
await server.Client.WaitForInstanceStartAsync(
instanceId, this.TimeoutToken);
philliphoff
reviewed
Nov 21, 2025
halspang
reviewed
Nov 21, 2025
ddde5e2 to
2e78473
Compare
halspang
approved these changes
Nov 25, 2025
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.
Fix #508
This pull request refactors the handling of external events in the orchestration worker to use a stack (LIFO) approach instead of a queue (FIFO). This change improves the delivery of events to the most recent waiter, making it easier for users to abandon events they no longer care about and avoids issues with cancelled waiters. The update is thoroughly validated by new and updated integration tests that confirm correct LIFO event delivery and proper handling of cancellation and buffering scenarios.
Core event handling changes:
externalEventSourcesdata structure inTaskOrchestrationContextWrapperfrom a queue to a stack using a single-linked list, so the most recent waiter receives events first.WaitForExternalEventandCompleteExternalEventto add new waiters to the top of the stack and deliver events in LIFO order, ensuring correct handling when waiters are cancelled or abandoned [1] [2] [3].IEventSourceinterface and its implementation to support stack-based linking via a newNextproperty [1] [2].Testing and validation:
ExternalEventStackTests.cs) that covers LIFO event delivery, cancellation scenarios, multiple waiters, and event buffering to ensure correctness of the new stack-based behavior.OrchestrationPatterns.cs) to expect LIFO delivery order, reflecting the new semantics.