Fix a thread-safety issue in Runner.Worker that leads to job crashes - #4614
Open
sebastiandero wants to merge 2 commits into
Open
Fix a thread-safety issue in Runner.Worker that leads to job crashes#4614sebastiandero wants to merge 2 commits into
sebastiandero wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a race in Runner.Worker.ExecutionContext.WriteWebhookPayload() where concurrent readers of event.json (e.g., in parallel: steps) can observe a partially-written file and crash when parsing JSON. It implements an atomic write pattern (write to temp file + replace) and adds an L0 regression test that stresses concurrent rewrites/readers.
Changes:
- Update
ExecutionContext.WriteWebhookPayload()to write the GitHub event payload via a temp file and atomically replaceevent.json. - Add an L0 regression test that repeatedly rewrites the payload while a concurrent reader verifies it never observes truncation.
- Add supporting test imports for file IO and task-based concurrency.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Runner.Worker/ExecutionContext.cs | Switch webhook payload persistence to a temp-write + atomic replace approach to prevent truncated reads under concurrency. |
| src/Test/L0/Worker/ExecutionContextL0.cs | Add a concurrency regression test covering repeated rewrites of event.json while a parallel reader validates the content is never truncated. |
Comment on lines
+1334
to
+1337
| // Ensure "parallel:" does not cause crashes here | ||
| var tempEventFile = Path.Combine(workflowDirectory, $"event.{Guid.NewGuid():N}.tmp"); | ||
| File.WriteAllText(tempEventFile, gitHubEvent, new UTF8Encoding(false)); | ||
| File.Move(tempEventFile, workflowFile, overwrite: true); |
Comment on lines
+1059
to
+1083
| using var writerDone = new CancellationTokenSource(); | ||
| var writer = Task.Run(() => | ||
| { | ||
| try | ||
| { | ||
| for (int i = 0; i < 100; i++) | ||
| { | ||
| ec.WriteWebhookPayload(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| writerDone.Cancel(); | ||
| } | ||
| }); | ||
|
|
||
| var truncatedReadLengths = new List<int>(); | ||
| var reader = Task.Run(() => | ||
| { | ||
| while (!writerDone.IsCancellationRequested) | ||
| { | ||
| using var stream = new FileStream(eventPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); | ||
| using var textReader = new StreamReader(stream, new UTF8Encoding(false)); | ||
| string observed = textReader.ReadToEnd(); | ||
| if (observed.Length != gitHubEvent.Length) |
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.
When introducing "parallel:" into a job, we hit a thread-safety issue in the runner code.
This PR adds a regression test and fixes the problem. We have tested this in production on our side to fix the issue and the regression test is red/green tested.
Error signature (many actions can hit this problem)
I did not find a "contributing" doc or guide so please excuse any mistakes in this submission, let me know what to change.
Thank you!
Seb