Skip to content

fix(launch): honour AWS_REGION and the instance type when choosing a region (#732) - #744

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/732-region-precedence
Oct 8, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/732-region-precedence

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #732. The first link in the chain that produced the demo's "5+ minute
hang" — the instance was in a region nobody asked for, which is why its SSH path
was in question at all.

Two independent causes

detectBestRegion took instanceType and never referenced it. The only
occurrence of the identifier was the signature, so the offering check could not
happen even in principle — despite the doc comment claiming it selected a region
"that has the requested instance type available". Ranking was measured TCP
latency plus IP geolocation.

That also made it unstable. Across 21 identical --estimate-only runs from
one machine in one session, 20 chose us-west-1 and one chose us-west-2 — the
demo's observed mix, reproduced. The latency gap between two west-coast endpoints
is smaller than the jitter. And:

$ aws ec2 describe-instance-type-offerings --region us-west-1 \
    --filters Name=instance-type,Values=c8a.large --query 'length(InstanceTypeOfferings)'
0        # us-west-2: 1

So the same command, same environment, no flags, lands in a region where the
launch works or one where it cannot — by coin flip.

Nothing on this path consulted SPORE_REGION/AWS_REGION or the spore
config
, even though the root --region flag has always documented that
precedence. launch registers its own --region, which shadows the root one
and binds a different variable — so fixing the resolver alone would have fixed a
variable nothing reads. That is why the flag help is part of this PR.

The fix

--region → SPORE_REGION/AWS_REGION → spore config → auto-detect, with
auto-detect last rather than only. sporeconfig.Resolve already implements
the middle of that chain, so this reuses it rather than re-deriving it.

  • Candidates are filtered by DescribeInstanceTypeOfferings. A region that
    cannot run the instance is never a candidate.
  • A failed offerings call counts as "offered." Throttling, a transient 5xx,
    or a missing ec2:DescribeInstanceTypeOfferings grant say nothing about
    availability, and excluding a region on that basis would silently narrow the
    choice — or empty it — for a caller whose only problem is an IAM policy.
    Degrading to the old latency-only ranking is the safe direction.
  • The ranking survives the concurrent filter: verdicts are collected by index
    and the slice rebuilt in sorted order, not completion order, which would
    otherwise reorder candidates and pick a slower region.
  • The chosen region prints with its reason — lowest latency of 3 region(s) offering c8g.xlarge, on your continent.
  • If nothing qualifies the launch fails and asks for a region, instead of
    falling back to us-east-1. That default was a guess that could itself not
    offer the type, turning "we could not choose" into a confident wrong choice.

One resolver, because the bug was five-way duplicated

The if region == "" { detectBestRegion(...) } block appeared at five call
sites — single launch, batch queue, and three sweep paths. Fixing the precedence
in one would have left four wrong, which is the same shape as the flag shadowing.
It now lives only in resolveLaunchRegion.

While converting them: one of the five printed its chosen region before
assigning it, so it always announced the empty string. Another line that did not
say what it claimed.

Verification

revert fails
ignore env/config in the chain region = "us-west-1", want eu-central-1 from AWS_REGION — and us-west-1 is the exact region from the report
ignore the instance type filtered to [us-west-1 us-west-2 us-east-1], want [us-west-2 us-east-1]
exclude a region on an API failure filtered to [us-west-2], want all three kept — an API failure is not evidence of unavailability

My first attempt at the first revert produced a build failure rather than a
test failure. That is not a passing gate — it proves the code references a symbol,
not that a test checks the behaviour — so I redid it as a compiling edit
(_ = spawnconfig.SharedConfig()), which is when it actually failed.

The offerings filter takes an instanceTypeOfferer interface rather than
*aws.Client, specifically so the failure branch — the one with a real judgement
call in it — is testable without AWS.

make check-docs caught the --region help change and docs-gen/launch.md is
regenerated in this commit.

gofmt, go vet ./..., go test -short ./..., changelog gate, semgrep scan --config=auto --error (0 findings).

Chain status

Still open from the same report: #733 (spawn status performs a DNS write),
#734 (CPU sentinels), #735 (boot time reported as stopped time), #736 (terminated
instances invisible to lookup), #737 (abort exits 0), #738 (connect quoting).

Why SSH was specifically unreachable in us-west-1 remains unexplained — two
obvious causes are ruled out in #740 — but with this merged, a launch with
AWS_REGION set no longer goes there to find out.

@scttfrdmn
scttfrdmn force-pushed the fix/732-region-precedence branch from 4e3e27d to e6a2ec1 Compare October 8, 2026 03:23
@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 27.55102% with 71 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/launch_regions.go 51.06% 23 Missing ⚠️
cmd/launch_sweep.go 0.00% 20 Missing ⚠️
pkg/aws/instanceofferings.go 0.00% 13 Missing ⚠️
cmd/launch_single.go 0.00% 8 Missing ⚠️
cmd/launch_batchqueue.go 0.00% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

…region

Reported from a demo: with AWS_REGION=us-east-1 set and a configured
default, launches landed in us-west-1 and us-west-2 — neither asked for
— and c8a.large is not offered in us-west-1 at all, so that one died
with EC2's opaque "Unsupported: The requested configuration is currently
not supported".

Two independent causes.

detectBestRegion took instanceType as a parameter and NEVER REFERENCED
IT — the only occurrence of the identifier was the signature, so the
offering check could not happen even in principle. Ranking was measured
TCP latency plus IP geolocation, which also made it unstable: across 21
identical runs from one machine, 20 chose one region and 1 chose
another, because the latency gap between two west-coast endpoints is
smaller than the jitter. Same command, same environment, working region
or broken one by coin flip.

And nothing on this path consulted SPORE_REGION/AWS_REGION or the spore
config, though the root --region flag has always documented that
precedence. `launch` registers its OWN --region, which shadows the root
one and binds a different variable, so the resolved value was never
read. Fixing the resolver alone would have fixed a variable nothing
reads.

Now: --region > SPORE_REGION/AWS_REGION > spore config > auto-detect,
with auto-detect last rather than only. sporeconfig.Resolve already
implements the middle of that chain, so this reuses it.

- Auto-detect filters candidates by DescribeInstanceTypeOfferings, so a
  region that cannot run the instance is never a candidate.
- A FAILED offerings call counts as "offered". Throttling, a 5xx, or a
  missing ec2:DescribeInstanceTypeOfferings grant say nothing about
  availability, and excluding on that basis would silently narrow the
  choice — or empty it — for a caller whose only problem is an IAM
  policy. Degrading to the old ranking is the safe direction.
- The ranking survives the concurrent filter: results are rebuilt in the
  original sorted order, not completion order.
- The chosen region prints WITH its reason ("lowest latency of 3
  region(s) offering c8g.xlarge, on your continent").
- If nothing qualifies the launch FAILS and asks for a region, instead
  of falling back to us-east-1 — a guess that could itself not offer the
  type, turning "could not choose" into a confident wrong choice.

The precedence lives in one resolveLaunchRegion because the
`if region == ""` block it replaces was duplicated at FIVE call sites
(single launch, batch queue, three sweep paths). One of those five also
printed its chosen region before assigning it, so it always announced
the empty string.

Gates confirmed to fail on revert: ignoring env/config (returns
us-west-1 — the exact region from the report), ignoring the instance
type, and excluding a region on an API failure.

The first revert attempt was a build failure rather than a test failure,
which is not a passing gate; redone as a compiling edit.
@scttfrdmn
scttfrdmn force-pushed the fix/732-region-precedence branch from e6a2ec1 to 9fabd9f Compare October 8, 2026 03:36
@scttfrdmn
scttfrdmn merged commit 0cb674b into main Oct 8, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/732-region-precedence branch October 8, 2026 03:49
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.

launch auto-detects a region by TCP latency, ignoring AWS_REGION, the config default, and whether the instance type is even offered there

1 participant