Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix race condition on OrchestrationRuntimeState.NewEvents #380

Merged
merged 2 commits into from
Apr 15, 2024

Conversation

sebastianburckhardt
Copy link
Member

In the current code, the BatchProcessed event was copying the list NewEvents from OrchestrationRuntimeState. However, that list can be mutated when processing more batches. This violates the design principle that events should be immutable, and can lead to race conditions between code that is reading those events (e.g. for serialization) and code that is mutating it.

To fix this, we just make a copy of that list when constructing the BatchProcessed event.

@@ -876,7 +876,7 @@ async Task<PurgeResult> IOrchestrationServicePurgeClient.PurgeInstanceStateAsync
InstanceId = workItem.InstanceId,
BatchStartPosition = messageBatch.BatchStartPosition,
BatchLength = messageBatch.BatchLength,
NewEvents = (List<HistoryEvent>)newOrchestrationRuntimeState.NewEvents,
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we can (or should) statically enforce that NewEvents should be readonly. That way, we can have more confidence that it is immutable. Is that worth it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not aware of an easy (and standard) way to statically enforce this. I think there is a library for immutable collections but I don't really think it is worth the hassle.

Co-authored-by: David Justo <david.justo.1996@gmail.com>
@sebastianburckhardt sebastianburckhardt merged commit 2f94e76 into main Apr 15, 2024
1 of 2 checks passed
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.

2 participants