Skip to content

Fix a thread-safety issue in Runner.Worker that leads to job crashes - #4614

Open
sebastiandero wants to merge 2 commits into
actions:mainfrom
sebastiandero:feature/fix-parallel-action-crash
Open

Fix a thread-safety issue in Runner.Worker that leads to job crashes#4614
sebastiandero wants to merge 2 commits into
actions:mainfrom
sebastiandero:feature/fix-parallel-action-crash

Conversation

@sebastiandero

Copy link
Copy Markdown

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)

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at new Context (.../buf-action/<sha>/dist/index.js:1871:37)
    ...
    at Module._compile (node:internal/modules/cjs/loader:1854:14)
Node.js ...

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

@sebastiandero
sebastiandero requested a review from a team as a code owner August 6, 2026 22:19
Copilot AI lite review requested due to automatic review settings August 6, 2026 22:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 replace event.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)
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