Skip to content

Fix hiera.yaml write race in the pupmod spec_helper template - #91

Draft
silug wants to merge 1 commit into
simp:mainfrom
silug:fix/hiera-yaml-write-race
Draft

Fix hiera.yaml write race in the pupmod spec_helper template#91
silug wants to merge 1 commit into
simp:mainfrom
silug:fix/hiera-yaml-write-race

Conversation

@silug

@silug silug commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Ref #90

Root cause of the fleet-wide random hieradata-not-applied failures

The config-level before(:all) hook in the pupmod spec_helper.rb template rewrites spec/fixtures/hieradata/hiera.yaml with a truncating File.open(..., 'w') before every top-level example group. Under rake parallel_spec, every worker runs this hook — so a catalogue compile in one worker that reads hiera.yaml inside another worker's truncate-to-write window sees an empty hiera config and silently compiles without any custom hieradata (falling back to code defaults). This matches every failure signature collected in #90; the set_hieradata global-state design originally suspected there was ruled out — a simplib PR that bypassed it entirely still hit the flake in its own CI run.

Confirmed by deterministic reproduction in pupmod-simp-simplib (simp/pupmod-simp-simplib#362): rewriting hiera.yaml in a tight truncate-then-write loop while running dlookup_spec produced the exact CI failure on the first run; with atomic rewrites, 8/8 runs passed under the same concurrent load. Fixed there per-module in simp/pupmod-simp-simplib#363 with this same change.

Change

Write hiera.yaml atomically: write to a PID-suffixed temp file in the same directory (rename(2) is only atomic within a filesystem) and File.rename it into place. Readers then always see a complete file.

Status

Draft: held until the in-flight template-refresh work merges, then this should ride the next baseline sync across the fleet. Note that simp/pupmod-simp-simplib#363 patched its module-local copy directly — the synced template must include this fix or the next sync will regress it.

🤖 Generated with Claude Code

The config-level before(:all) hook rewrites
spec/fixtures/hieradata/hiera.yaml with a truncating write before every
top-level example group. Under `rake parallel_spec`, every worker runs
this hook, and a catalogue compile in another worker that reads
hiera.yaml inside the truncate-to-write window sees an empty hiera
config and silently compiles without any custom hieradata.

This is the confirmed root cause of the fleet-wide random
hieradata-not-applied spec failures (ref simp#90) — not the set_hieradata
global-state design originally suspected. Reproduced deterministically
in pupmod-simp-simplib by rewriting hiera.yaml in a tight loop while
running dlookup_spec (simp/pupmod-simp-simplib#362, fixed there by
simp/pupmod-simp-simplib#363 with this same change).

Write the file atomically instead: write to a PID-suffixed temp file in
the same directory and rename it into place. Readers then always see a
complete hiera.yaml. The temp file must be in the same directory --
rename(2) is only atomic within a filesystem.

Ref simp#90

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant