Skip to content

Write plugins.json before spored starts (#1023) - #1045

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/1023
Oct 16, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/1023

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1023

What

BuildLinuxBootstrap appended the --plugin declarations (cat > /etc/spawn/plugins.json) after linuxBootstrapBody. The body ends in systemctl start spored. The unit is Type=simple, so systemctl start returns as soon as spored is forked. spored reads the file once, at startup: provider.loadPluginDeclarations, then agent.newAgent → rt.LoadFromDeclarations. So plugins loaded only if the bootstrap's cat won a race against spored's IMDS reads and its first DescribeTags.

The declarations are now written before the body, next to /etc/spawn/command and the ready-gates list. The file exists before spored is even installed.

Why this, and not watch/reload in spored. Moving the write removes the race by construction, and changes nothing in spored. Watch/reload would add a new runtime path (fsnotify or polling, partial-write handling, loading plugins after the agent is running) to fix an ordering bug in a script. A reload could also be wanted later for plugins changed at runtime, but this bug doesn't need it.

Test

pkg/launcher/bootstrap_plugins_order_test.go, TestPluginsJSONExistsWhenSporedStarts, is bash-level. It runs the real generated bootstrap:

  • PATH is an empty directory, so every external command reaches bash's command_not_found_handle. That handler logs the command and succeeds, passes mkdir/cat/chmod through to the real binaries, and answers uname with x86_64.
  • /etc/spawn is rewritten into a temp dir.
  • The verified spored download is stubbed to succeed, because it is a network fetch the bootstrap rightly aborts on.
  • errexit is off, because unrelated writes to root-owned paths fail in a test.

The systemctl stub records whether plugins.json is non-empty at the moment it is asked to start spored. The test also fails if start spored is never reached, so a harness that stops early can't pass vacuously. Every harness anchor fails loudly if it disappears. The test skips on bash < 4 (macOS /bin/bash 3.2 has no command_not_found_handle).

Against the original ordering, it fails with spored was started before /etc/spawn/plugins.json was written (recorded "absent"), and go vet is clean. It passes with the fix.

Checks

  • make check (credentials stripped): the only failure is the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker.
  • make check-nested: 9 modules clean.
  • golangci-lint --new-from-rev=origin/main ./pkg/launcher/...: 0 issues.

Interaction with #1021

#1021 puts script += spotReplacementStopScript immediately after the plugin block, and its comment explains that placement by this race.

Not verified

This was not run on a real instance. The ordering is observed under a stubbed bash run, not under cloud-init and systemd.

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

BuildLinuxBootstrap appended the --plugin declarations after
linuxBootstrapBody, which ends in `systemctl start spored`. The unit is
Type=simple, so systemctl returns once spored is forked, and spored reads
/etc/spawn/plugins.json exactly once at startup. Plugins loaded only if the
bootstrap's cat won the race.

The declarations are now written before the body, alongside
/etc/spawn/command and the ready-gates list, so the file exists before spored
is installed. TestPluginsJSONExistsWhenSporedStarts runs the real generated
bootstrap under bash with every external command stubbed and records, from
the systemctl stub, whether the file exists at `start spored`.
@scttfrdmn
scttfrdmn merged commit 67048c4 into main Oct 16, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1023 branch October 16, 2026 21:11
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…1023's plugin order (#1007)

The sandboxed bootstrap tests execute the Linux user data under bash with
stubbed spored and su, which the Windows CI job (#973) cannot reproduce, so
they skip there as the other Linux-bootstrap execution tests do (#1002).
Rebased onto #1023/#1045, plugins.json is written before spored starts, so
the post-spored stop sits straight after the bootstrap body; the changelog
and the ordering test's comment now say so.

(#1007)
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…1023's plugin order (#1007)

The sandboxed bootstrap tests execute the Linux user data under bash with
stubbed spored and su, which the Windows CI job (#973) cannot reproduce, so
they skip there as the other Linux-bootstrap execution tests do (#1002).
Rebased onto #1023/#1045, plugins.json is written before spored starts, so
the post-spored stop sits straight after the bootstrap body; the changelog
and the ordering test's comment now say so.

(#1007)
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…1021)

* Stop a Spot replacement from re-running the user's workload (#1007)

A replacement EC2 launches from a leaked persistent Spot request boots the
original's user data. The bootstrap started --command in the background
before spored, so the replacement re-ran the workload from the start, side
effects included, while spored was deciding to retire it.

New `spored is-spot-replacement` runs spotrequest.DetectReplacement and
exits 0 (replacement), 1 (not) or 2 (unknown). It retries an inconclusive
read every 5s for up to --timeout (60s), and not at all on an IAM refusal.

The Linux bootstrap calls it twice through spawn_is_spot_replacement:
inside the backgrounded --command subshell, ahead of the command, so it
never delays spored's start; and right after spored starts, where a
replacement exits the script. So storage, plugins, the container, custom
user data and the MPI, batch-queue and pool-worker scripts appended later
don't run either. Definite answers are cached in /run/spawn.

Only exit 0 skips. Unknown, a missing spored, an older spored without the
subcommand (cobra exits 1) and a timeout all run the workload, as before.
Holding would strand originals whenever EC2/IMDS is slow or a custom role
can't read tags; a replacement that gets past it is still retired by
spored and alarmed by the reaper.

The Windows bootstrap starts no workload (it runs no --command or user
data), so it has nothing to gate; a comment says what to do if it gains one.

Also adds TestRunDaemonStartsTheReplacementCheck, which fails if runDaemon
stops starting the #995 check (the gap #1008 noted), and a Detection.Err
field so the subcommand can tell an IAM refusal from a transient failure.

The bootstrap tests run the generated --command block and the post-spored
tail under bash with stubbed spored and su; each new test was watched
failing against a compiling revert.

(#1007)

* Run the post-spored replacement stop after the plugin declarations (#1007)

spored reads /etc/spawn/plugins.json once, at startup, and the bootstrap
writes it just after `systemctl start spored`. Putting the stop's EC2 round
trip between the two would have made spored miss the declarations on every
original instance. The stop now follows the plugins block; a gate keeps it
there. The existing race is filed as #1023.

(#1007)

* Name the original instance in spot_replacement_retired (#1007)

The notification comes from the replacement, whose instance id nobody
registered with spore-bot, so the bot's registration gate dropped every
DM and SMS for it. spored now also sends original_instance_id (via
Notifier.WithOriginalInstance), which spore-bot uses for this event alone.
Older bots ignore the unknown field.

(#1007)

* Skip the bash-run Spot replacement bootstrap tests on Windows; note #1023's plugin order (#1007)

The sandboxed bootstrap tests execute the Linux user data under bash with
stubbed spored and su, which the Windows CI job (#973) cannot reproduce, so
they skip there as the other Linux-bootstrap execution tests do (#1002).
Rebased onto #1023/#1045, plugins.json is written before spored starts, so
the post-spored stop sits straight after the bootstrap body; the changelog
and the ordering test's comment now say so.

(#1007)

* Use the repo's bare nosemgrep on the spot-replacement test harness (#1007)
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.

Linux bootstrap writes /etc/spawn/plugins.json after starting spored, which reads it only at startup

1 participant