Skip to content

DNS records leak when spored doesn't exit through Cleanup (abrupt terminate/spot/crash) — no backstop reaper #438

Closed

Description

@scttfrdmn

Symptom

Route53 A-records under an account's {base36}.spore.host subdomain outlive their instances. In the dev account (435415984226, base36 5k0zfnmq) I found stale records pointing at IPs with no live instance:

  • gatetest.5k0zfnmq.spore.host → 13.221.19.38 (no such instance)
  • tstest.5k0zfnmq.spore.host → 3.227.10.61 (no such instance)

Both still resolve publicly. Meanwhile instances that self-terminated on TTL did deregister correctly (verified: name → NONE after termination).

Root cause

Deregistration only happens when spored runs Cleanup() → DeleteDNS. Confirmed paths that call it: terminate() (TTL self-terminate, agent.go:1619), spot-interrupt (1198), and a couple of other graceful exits. But instances that exit without spored running Cleanup leak their record:

Two gaps:

  1. Cleanup isn't wired to SIGTERM. cmd/spored already has signal.Notify(..., SIGTERM) for shutdown, but the DNS Cleanup isn't invoked on that path — so even a graceful systemctl stop (and the shutdown that precedes an OS-level terminate) may not deregister. Hooking Cleanup to the SIGTERM handler would cover systemctl stop and instance-stop, though not hard kills.
  2. No backstop DNS reaper. Nothing reconciles Route53 against live instances. This is the only reliable fix for the abrupt-exit cases (which are inherent — a crashed box can't deregister itself). Analogous to the orphan-instance reaper (spawn-ts#23 / the TTL-reaper Server-side TTL reaper: out-of-band backstop so a spore can never outlive its deadline (even if spored is dead) #70): periodically list {base36}.spore.host A-records and delete any whose IP has no running spawn:managed instance in that account. Natural home: the spawn-dns-updater Lambda gaining a sweep mode, or the existing TTL-reaper Lambda also reaping DNS.

Impact

Stale DNS accumulates indefinitely and resolves to dead (or eventually reassigned) public IPs — a correctness and mild security concern (a name could later point at someone else's EC2 IP). Cosmetically clutters the zone.

Repro

Launch a spawn instance (gets {name}.{base36}.spore.host), then terminate it from the console/API (not via TTL). The A-record remains in Route53 and keeps resolving.

Found while running the spawn-ts browser BYOA demo; the browser launcher can't deregister (instance-side spored owns it), so this is purely a spored/infra concern.

Activity

  1. scttfrdmn commented on Jul 23, 2026

    @scttfrdmn
    ContributorAuthor

    Verified — issue is valid; one correction to claim #1, and the reaper is closer than the report assumes

    Confirmed the mechanism against the code. Two adjustments to the writeup, but the core gap (no record-driven reconciliation) is real.

    Correction to gap #1 ("Cleanup isn't wired to SIGTERM")

    It is wired. On Unix, waitForShutdown() (cmd/spored/shutdown_other.go) does signal.Notify(sigChan, SIGINT, SIGTERM) and blocks on it; runDaemon() then runs agent.Cleanup(cleanupCtx) → DeleteDNS (cmd/spored/main.go:211-237). So a systemctl stop (and the SIGTERM an OS shutdown sends) does deregister today, within the 10s cleanup budget. The real uncovered cases are the ones inherent to abrupt exit: hard crash / OOM / power-off, and a fast spot reclaim or API terminate where the box dies before cleanup completes.

    Gap #2 is the actual fix — and partially exists already

    The ttl-reaper Lambda already deletes DNS (#247) — but only for instances it can still see. It's instance-driven: DescribeInstances → for each past-deadline/reaped candidate it calls deleteDNS() (lambda/ttl-reaper/main.go:407,822; comment: "spored's graceful DeleteDNS never ran"). It captures spawn:dns-name/base36/accountName from the instance tags to know what to delete.

    The hole: once an instance is gone from DescribeInstances (terminated out-of-band and aged out of the API after ~1h, or hard-crashed and reaped by AWS), there's no candidate left to key off — so its record is orphaned with nothing pointing at it. That's exactly your gatetest/tstest: instance long gone, record remains. (Side note: both now return NXDOMAIN/empty via public DoH — either TTL-expired from resolver cache or cleaned since you filed — but the mechanism stands.)

    Recommended fix (record-driven sweep — the inverse enumeration)

    Add a reconciliation pass to the ttl-reaper (it already has Route53 perms + the account-base36 mapping):

    1. ListResourceRecordSets under {base36}.spore.host (A-records + the Use the AWS account's friendly name (not just base36 account ID) in DNS and elsewhere #121 friendly CNAMEs).
    2. Build the set of live spawn:managed instance public IPs from the DescribeInstances pass it already does.
    3. Delete any A-record whose IP is not in that live set — with a grace window (e.g. skip records younger than ~15m) to avoid racing a just-launched instance that registered before it appears in the reaper's instance list.

    This is the only reliable fix for the abrupt-exit cases (a crashed box can't deregister itself), and it complements — doesn't replace — the existing spored-Cleanup and #247 reaper-teardown paths. The security angle (a stale name later resolving to a reassigned EC2 IP) makes the grace-windowed sweep worth doing.

    Scope note

    The fix lives in infra (the ttl-reaper Lambda), not the spawn CLI/spored — so I'm flagging it for a maintainer rather than implementing it here. Happy to draft the sweep logic + tests against lambda/ttl-reaper if you want it as a PR. No code changed for this analysis; read-only.

  2. scttfrdmn commented on Jul 23, 2026

    @scttfrdmn
    ContributorAuthor

    Fixed by PR #439 (merged) — opt-in DNS reconciliation sweep in the ttl-reaper.

    What shipped: REAPER_DNS_SWEEP=true makes the reaper, per account, list the {base36}.{domain} A-records and delete any whose IP has no live (running/pending) spawn:managed instance. base36 is derived from the account ID, so a subdomain with a stale record and zero instances is still reconciled — the record-driven inverse enumeration the #247 instance-driven teardown can't do. This is the reliable fix for the abrupt-exit cases (crash / out-of-band terminate / fast spot reclaim) a crashed box can't self-deregister.

    Safety: opt-in + default off, honors REAPER_DRY_RUN, aborts without deleting if any region's live-IP scan errors (never deletes against a partial live set), touches only plain A-records under the account subdomain (skips the apex + Route53 alias records), and reuses the existing #247 Route53 + DescribeInstances IAM (no new permissions). Pure orphanedRecords core is unit-tested.

    Recommended rollout: deploy with REAPER_DNS_SWEEP=true and REAPER_DRY_RUN=true first — it'll log WOULD delete orphaned A-record … for each stale record (should include the gatetest/tstest leaks you found). Verify that list, then flip REAPER_DRY_RUN=false.

    Closed as completed.

  3. scttfrdmn commented on Jul 31, 2026

    @scttfrdmn
    ContributorAuthor

    Verified the fix live (read-only) while closing out the spawn-ts side. Three findings:

    1. Gap (1) is already closed. This issue named "Cleanup isn't wired to SIGTERM in cmd/spored" — it is: waitForShutdown() → agent.Cleanup() → DeleteDNS, since 0ea9e39 (2026-06-08), with TimeoutStopSec=30 in the systemd unit. No change needed.

    2. The gap-(2) sweep works. Drove the real sweepAccountDNS in dry-run against the live spore.host zone. It correctly flagged a genuine orphan —

    DNS sweep: WOULD delete orphaned A-record win11-test.4zlw3a1t.spore.host -> 13.223.189.34 (no live instance)
    

    — with zero live spawn:managed instances in any scanned region, while sparing every alias and out-of-account record. The partial-scan safety abort was confirmed live too: a cross-account DescribeInstances denial logged aborting sweep for this account and deleted nothing.

    3. But it isn't running in production, and couldn't be turned on. make deploy never passed DnsSweep (nor DnsZoneId/DnsDomain), and sam deploy marks omitted parameters UsePreviousValue — so the documented REAPER_DNS_SWEEP knob had no supported way to be set. Consistent with production: spawn-ttl-reaper-production last updated 2026-06-25 (before #439 landed on 07-23), no REAPER_DNS_SWEEP in its environment, and no dns-sweep= field on its live init line. Fixed in #453.

    Also worth noting: the orphan above belongs to account 390967728545, which is not in the reaper's RoleArns. The sweep is per-account (it derives each {base36} subdomain from the account ID), so that record stays orphaned until the account is added — #453's README change calls this out.

    Remaining to fully close the loop, both gated on approval: deploying the reaper with DNS_SWEEP=true, and the explicit non-graceful (out-of-band terminate) de-register test this issue targets.

  4. added a commit that references this issue on Jul 31, 2026
  5. scttfrdmn commented on Jul 31, 2026

    @scttfrdmn
    ContributorAuthor

    Live verification (infra 966362334030 + dev 435415984226)

    Verified both halves of this issue against the real spore.host zone rather than trusting green CI.

    Gap 1 — Cleanup not wired to SIGTERM: already closed. cmd/spored/main.go waitForShutdown() calls agent.Cleanup(cleanupCtx), which reaches DeleteDNS (pkg/agent/agent.go:1436-1481). In place since 0ea9e39.

    Gap 2 — no backstop reaper: closed in code, but unreachable in production. The sweep (8c0a014 / #439) was correct, but make deploy never passed DnsSweep, and sam deploy marks any parameter it is not given as UsePreviousValue — so the production stack silently kept dns-sweep=false while both the code and the README claimed the feature existed. #453 + #455 fix that. #455 also fixes a second, more dangerous bug found while deploying: with an empty parameter value, sam either rejects the whole --parameter-overrides string or silently mis-keys every parameter (observed NotifyUrl swallowed as the literal key ParameterKey) — a deploy that reports success having applied something other than what was asked.

    Production now reports enforcement and the sweep both live:

    ttl-reaper initialized (accounts=[435415984226], …, max-age=168h0m0s, dry-run=false, graceful=false, graceful-wait=2m0s, dns-sweep=true)
    

    Driving the real sweepAccountDNS in dry-run against the live zone found a genuine orphan — win11-test.4zlw3a1t.spore.host → 13.223.189.34, zero live instances — and correctly spared every alias record and every record outside the account's {base36} subdomain. That orphan's account (390967728545) is not in RoleArns, so the sweep will never reconcile it until the cross-account role is deployed there; the sweep is per-account by construction.

    Correction to this issue's premise: an EC2 API terminate is graceful

    The title lists "out-of-band terminate" alongside crash and spot reclaim. It doesn't belong there. Launched abrupt438 in dev, confirmed registration, then terminated purely via aws ec2 terminate-instances — never touching the instance:

    i-0783ecd0903e0df0c  18.206.211.118  launched 01:17:31Z
    01:18:20Z  UPSERT  abrupt438.5k0zfnmq.spore.host    A      18.206.211.118
    01:18:20Z  UPSERT  abrupt438.mycelium-development…  CNAME  abrupt438.5k0zfnmq.spore.host
    --- aws ec2 terminate-instances ---
    01:21:10Z  DELETE  abrupt438.mycelium-development…  CNAME
    01:21:10Z  DELETE  abrupt438.5k0zfnmq.spore.host    A      18.206.211.118
    +20s: instance=shutting-down  dns=<none>
    +40s: instance=shutting-down  dns=<none>
    +60s: instance=terminated     dns=<none>
    

    The record was gone at +20s, while the instance was still shutting-down, and CloudTrail attributes the DELETE to the dying instance itself — spawn-dns-updater logged the caller as arn:aws:sts::435415984226:assumed-role/spored-instance-role/i-0783ecd0903e0df0c. TerminateInstances triggers an ACPI shutdown → systemd SIGTERM → spored Cleanup() → DeleteDNS, comfortably inside TimeoutStopSec=30. No leak occurred, so the sweep had nothing to reclaim.

    Still unverified live: the genuinely abrupt paths — hard crash / OOM kill, and spot reclaim faster than the shutdown can complete. They're hard to produce on demand. The sweep is the right backstop for them and is now provably enabled and provably safe against the live zone, but its reclaim path has so far only been exercised against a pre-existing orphan, not one created during a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions