Skip to content

feat(service): add spawn service for long-lived HTTP services (#409) - #481

Merged
scttfrdmn merged 3 commits into
mainfrom
feat/409-spawn-service
Aug 3, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
feat/409-spawn-service

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #409.

The gap

spawn had no answer for "run a long-lived HTTP service on an instance and give me a working local URL." spawn launch assumes a job that ends. spawn connect -- cmd runs a command but gives you no way to learn what port it bound. Neither closes the loop.

The seam

One JSON line on the service's stdout:

{"event":"ready","addr":"127.0.0.1:54321","provenance":{"commit":"abc1234"}}

The service picks its own port (:0, let the OS assign) and announces what it actually bound; spawn forwards to that. Nothing guesses a port, and nothing polls one — "is 8080 open yet?" cannot distinguish my service is starting from someone else's is already there, and the answer arrives late either way.

spawn names no workload, and no workload names spawn. docs/service-readiness-contract.md is the public spec, so a binary can be made spawnable without reading spawn's source.

Layout

File Role
pkg/service/readiness.go scan stdout for a ready event; verify candidates
pkg/service/local.go run a service as a local child (the $0 test path)
pkg/service/tunnel.go open and verify a loopback forward
pkg/service/remote.go the over-SSH ladder: session → readiness → tunnel → hold
cmd/service.go the verb

Two hazards, both tested

The readiness line is forgeable. init() runs before main, so a workload can always print a well-formed line of its own choosing first — "first match wins" is defeated by construction, and a launcher that trusts the first line forwards to whatever port the workload named. Each candidate is verified before it's trusted; the real line wins.

Verification is an HTTP request, not a TCP connect. ssh -L binds and accepts on the local end whether or not anything is listening on the far side, so a successful connect to a forward proves only that ssh is running. A forged address passes a TCP dial and fails a request. TestRunRemoteTCPDialWouldAcceptAForgedAddress demonstrates the difference directly rather than asserting it in a comment.

Any HTTP status counts, including 401 and 404: the question is whether a server is there, not whether the caller is authorized. Rejecting non-200s would reject every authenticated service as forged.

Lifetime and cost

Closing an SSH session does not reliably kill what it started — no TTY means no SIGHUP — so a service would keep serving, and keep billing, after the launcher exits. The remote command is wrapped: it backgrounds the service and blocks on stdin, then TERM→KILLs on EOF.

That's a request, not a guarantee; a partition can strand the remote process. The instance TTL is the guarantee, so:

  • the launch path calls guardZombieInstance — the same mandatory-TTL guard as every other launch (a flag-driven verb loses the spec's TTL guarantee, so it has to)
  • OnComplete: "terminate", not stop — a stopped instance still holds a billable EBS volume
  • --dry-run quotes the rate and the rate × ttl bound before anything is spent
  • --host attaches to an instance spawn did not launch, and promises no teardown

Exposure is loopback-only on both ends. A supplied token goes into the printed URL (or you can't connect) but never into the loggable address form.

The verb also classifies its own exit against EC2 rather than guessing: a session that ends because the TTL expired (the designed end) and one that ends because the connection dropped (a failure) look identical from inside the process, and a guess would either cry wolf on every expiry or hide a real drop.

Testing

go test ./pkg/service/ ./cmd/ — green, including with -race. The whole contract is exercised end to end at $0 over a local loopback path: real child process, real readiness line, real forward, driven through the forward, including both forgery cases. The Substrate emulator is control-plane only (instances never boot, no SSH), so the emulator could not have kept the first real spawn from being the first test.

Every guard was mutation-tested to confirm its test is load-bearing — terminate→stop, re-appending the addr args, dropping the --host guard, disabling the idle default in the dry run, mutating the caller's args in place, if true in the hold loop, treating stopped as still-serving, HTTPProbe→TCPDialer, breaking the stdin gate, removing stdin.Close() from Stop. Each mutation fails at least one test. One of these found a real gap: the exit classification had no coverage at all, which is why holdService now takes injectable sessionDone/instanceGone and instanceStateIsGone is extracted.

Note on CI

TestCatalogValid fails on this branch and on main — the shipped catalog references private ECR images for chimerax/paraview (#392). Unrelated to this change; confirmed pre-existing by stashing.

make check runs gofmt -w ., which also wants to reformat three unrelated files carrying pre-existing drift (lambda/dashboard-api/auth.go, pkg/launcher/bootstrap_test.go, pkg/scheduler/scheduler.go). Left alone — one topic per PR.

Deliberately not here

Migrating cmd/plugin.go:withSSHTunnel and pkg/plugin/push_ssh.go:pushViaTunnel onto service.OpenTunnel. Both predate this and duplicate the forward logic; that's a separate topic.

Adds a verb for the shape spawn had no answer to: run a long-lived HTTP
service on an instance and get a working local URL for it. `spawn launch`
assumes a job that ends; `spawn connect -- cmd` gives no way to learn the
port. Neither closes the loop.

The seam is one JSON line on the service's stdout:

    {"event":"ready","addr":"127.0.0.1:54321","provenance":{...}}

The service picks its own port (`:0`) and announces what it bound; spawn
forwards to that. Nothing guesses a port, and nothing polls one — "is 8080
open yet?" cannot tell *my service starting* from *someone else's already
there*. spawn does not name any workload and no workload names spawn.

pkg/service is the reusable core (local child, readiness scan, tunnel,
remote-over-SSH ladder); cmd/service.go is the verb. docs/ carries the
public spec so a binary can be made spawnable without reading the code.

Two hazards drove the design, both covered by tests:

- The readiness line is forgeable. `init()` runs before `main`, so a
  workload can always print a well-formed line of its choosing first —
  "first match wins" is defeated by construction. Each candidate address
  is verified before it is trusted.
- Verification is an HTTP request, not a TCP connect, because `ssh -L`
  accepts on the local end whether or not anything listens on the far
  side. A connect to a forward proves only that ssh is running. Any HTTP
  status counts, including 401/404 — the question is whether a server is
  there, not whether the caller is authorized.

Lifetime: closing an SSH session does not reliably kill what it started
(no TTY, so no SIGHUP), so the remote command is wrapped to TERM→KILL the
service on stdin EOF. That is a request, not a guarantee — a partition can
strand it. The instance TTL is the guarantee, so the launch path goes
through the same mandatory-TTL zombie guard as every other launch, and
`--dry-run` quotes the bound (rate × ttl) before spending anything.
`--host` attaches to an instance spawn did not launch and promises no
teardown.

Exposure is loopback-only on both ends. The token, when a service supplies
one, goes in the printed URL but never in the loggable address form.

Tested at $0 over a local loopback path — the Substrate emulator is
control-plane only (instances never boot, no SSH), so the emulator could
not keep the first real spawn from being the first test.
@codecov

codecov Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

Semgrep's dangerous-exec-command rule is blocking in this repo (#368), and
it fires on every `exec.Command` with a non-literal argument — which is all
six of them here. Annotated in the repo's existing style rather than
widened in .semgrepignore, so each one carries its own reason:

- `runQuietly`: the binary is a literal at both call sites ("scp", "ssh")
  and the args come from scpArgs/SSHExecArgs. No shell is involved, so an
  argument holding shell metacharacters is one argv element, not a command.
- `RunLocal`: running a caller-named binary IS the contract. The path comes
  from the operator invoking spawn, who already has a local shell.
- The four test sites: fixtures. Two of them run a shell deliberately —
  the forged-readiness-line reproduction needs one, and WrapRemoteCommand
  produces a shell script by design, so verifying the reap-on-EOF wrapper
  means running it.

Also replaces the test server's `fmt.Fprint(w, body)` with `w.Write`, which
is what it should have been: there is nothing to format, and it drops the
no-fprintf-to-responsewriter finding rather than suppressing it.
… XSS rule

Two CI gates.

The Tier 0 gate is doing exactly its job: a new top-level command must be
classified as covered or deferred, and `service` was neither. Deferred to
Tier 2, because the verb IS a booted instance plus an SSH session plus a
port-forward, and Substrate emulates the control plane only — instances
never boot and there is no SSH. What the verb exists for is nonetheless
fully tested at $0 in pkg/service (real child, real readiness line, real
loopback forward, driven through the forward, both forgery cases), with the
launch config, cost bound, and exit classification unit-tested in cmd.

Semgrep has a second rule for the test server's response write —
no-direct-write-to-responsewriter fires on `w.Write` just as
no-fprintf-to-responsewriter fired on `fmt.Fprint`, so there is no spelling
that satisfies both. Annotated: the body is a literal from the test and the
only reader is an in-process HTTP probe, so there is no browser and no HTML
to escape.
@scttfrdmn
scttfrdmn merged commit 48ba7f7 into main Aug 3, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the feat/409-spawn-service branch August 3, 2026 02:04
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.

New workload type: spawn service — spawn a long-lived HTTP service, read readiness from stdout, tunnel to it (DCV-free)

1 participant