Skip to content

fix: race condition in auto-scaler during Caddy config reload#2318

Merged
dunglas merged 1 commit intophp:mainfrom
nicolas-grekas:fix-rac
Mar 28, 2026
Merged

fix: race condition in auto-scaler during Caddy config reload#2318
dunglas merged 1 commit intophp:mainfrom
nicolas-grekas:fix-rac

Conversation

@nicolas-grekas
Copy link
Copy Markdown
Contributor

@nicolas-grekas nicolas-grekas commented Mar 27, 2026

Spotted by a failing job on #2287

scaleWorkerThread and scaleRegularThread read the global mainThread from goroutines started by initAutoScaling. During a Caddy reload, FrankenPHPApp.Start() calls Shutdown() then Init(), which overwrites mainThread. The scaler goroutines could read the stale global concurrently with the write.

The fix captures mainThread.done and mainThread.state locally in initAutoScaling and passes them as parameters through startUpscalingThreadsscaleWorkerThread / scaleRegularThread. No package-level globals for these values — each autoscaler generation uses its own captured references, eliminating the race even during concurrent reloads.

@dunglas dunglas requested a review from Copilot March 27, 2026 22:31
Copy link
Copy Markdown
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

Good catch!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a data race during Caddy config reloads by avoiding direct reads of the global mainThread from autoscaler goroutines.

Changes:

  • Introduces scalingDone and scalingState globals intended to capture mainThread.done / mainThread.state at initAutoScaling time.
  • Updates scaleWorkerThread and scaleRegularThread to use the captured references instead of mainThread.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nicolas-grekas
Copy link
Copy Markdown
Contributor Author

Patch updated

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dunglas dunglas merged commit d26834c into php:main Mar 28, 2026
16 checks passed
@dunglas
Copy link
Copy Markdown
Member

dunglas commented Mar 28, 2026

Thanks!

@nicolas-grekas nicolas-grekas deleted the fix-rac branch March 28, 2026 14:57
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.

3 participants