GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(client): wait for service shutdown before restart by kitlangton · Pull Request #50042 · anomalyco/opencode

Conversation

@kitlangton

@kitlangton kitlangton commented Sep 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Why

Service.stop() can report success while the process it just signalled is still alive. After the graceful-shutdown window, it rereads the service registration and returns early if the file disappeared or now describes a successor. A restart caller can therefore proceed before the original process has exited.

The registration answers which service clients should discover. It does not answer whether the process being stopped has exited.

What Changes

Once the client verifies the registered instance and sends SIGTERM, it keeps waiting for that original PID. If the process exceeds the existing five-second grace period, the existing SIGKILL escalation applies even if registration changed. Cleanup retains its existing ownership check before removal; that read/remove pair is not atomic.

During shutdown Before After
Original exits gracefully Waits for exit Same
Registration disappears; original remains alive Returns after grace period Escalates and waits for original exit
Successor registers before the final cleanup read; original remains alive Returns while original survives Stops original; preserves the observed successor and its registration

Two different identities during shutdown

This diagram shows the deliberately forced fixture sequence, including a successor registering while the original still holds its listener:

sequenceDiagram
    participant C as Client stop()
    participant A as Original process A
    participant R as Registration
    participant B as Successor B
    C->>R: Verify original instance A
    C->>A: SIGTERM
    A->>R: Remove A's registration
    Note over A: Fixture keeps A's listener open
    B->>R: Register B on another port
    alt Before
        C->>R: Registration no longer describes A
        Note over C,A: stop() returns while A is alive
    else After
        C->>A: SIGKILL after existing grace period
        A-->>C: Original process exits
        C->>R: Preserve registration belonging to B
        Note over C,B: stop() succeeds; B remains healthy
    end
Loading

Evidence boundary: current server code closes its listener before running its returned registration cleanup. These tests force early unregister to establish the client's stop contract; they do not establish that normal current server teardown creates this sequence, or attribute all reported startup timeouts to it.

Separate confirmed cleanup race: if cleanup reads the original record, a successor publishes, and the old cleanup then removes the shared path, it can delete the successor's registration. Adversarial real-filesystem tests reproduce this in both client variants at this PR's head, as well as in the server registration cleanup. This PR's successor test covers publication before the cleanup read; it does not establish atomic successor preservation. The publication/removal protocol needs a separate repair.

Before / After Proof

Both Effect and Promise clients run real child-process/socket regressions:

  • Missing registration: the child unregisters on SIGTERM but retains its listener. stop() must wait until its PID is gone and the old port can be rebound.
  • Successor registration: after the original unregisters, another child publishes its registration. stop() must finish stopping the original while preserving the successor's full registration, live PID, and healthy HTTP endpoint.
  • Process survives SIGKILL (POSIX): the child's parent never reaps it, so SIGKILL leaves a zombie that still answers kill(pid, 0). stop() must fail after both grace periods with Server process <pid> is still running rather than hang or report success.

For the successor case, the fixture publishes an explicit post-unregister barrier. The test confirms the successor registered while the original was still alive; it does not rely on an arbitrary sleep.

Same successor tests Result
Pre-fix registration guard restored 2 failed: original PID was still alive after stop() returned
Fixed implementation 2 passed per run: original gone, old port reusable, successor healthy

Scope

This PR owns completion of a client-requested stop, including the stop used during version replacement. #50782 handles transient bind recovery; #50784 preserves startup errors. Each has its own reproducer.

Verification

cd packages/client
bun run test test/service.test.ts test/promise-service.test.ts -t 'preserving a newly registered successor'
bun run test test/service.test.ts test/promise-service.test.ts --rerun-each 3

cd ../..
bun run check
bunx prettier --check packages/client/test/fixture/service.ts packages/client/test/service.test.ts packages/client/test/promise-service.test.ts
  • Before: the targeted successor tests fail in both client variants with the old guard restored.
  • After: all 30 focused tests passed across three runs — 90 passed, 0 failed.
  • Root lint/typecheck passed: 35/35 tasks; formatting passed.
  • After rebasing onto v2 00738c5b2d: the six stop tests pass across three runs (18/18); full root lint/typecheck passes. Four unrelated generated-client tests (file.write) fail on v2 itself, with or without this change.
  • Tests use owned fixture children and random ports on macOS/Bun. The successor cases retain the real five-second graceful-shutdown window. No installed background service is used.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added the contributor label Sep 19, 2026
@kitlangton
kitlangton force-pushed the restart-determinism branch from 6e2b228 to 9e91aee Compare September 23, 2026 00:15
@kitlangton kitlangton mentioned this pull request Sep 23, 2026
fix(server): preserve historical service health probes #50824
Closed
@kitlangton
kitlangton force-pushed the restart-determinism branch from 4481b5b to ec36e60 Compare September 25, 2026 21:24
@kitlangton
kitlangton force-pushed the restart-determinism branch from ec36e60 to 1d0e1aa Compare October 4, 2026 04:58
kitlangton added 4 commits October 4, 2026 13:34
@kitlangton
fix(client): wait for service shutdown before restart
a6939d7
@kitlangton
test(client): preserve successors during service shutdown
c3b7f6a
@kitlangton
test(client): cover failed service stop escalation
baa3b4b
@kitlangton
test(client): install fixture signal handlers before registering
83436da
@kitlangton
kitlangton force-pushed the restart-determinism branch from 1d0e1aa to 83436da Compare October 4, 2026 20:54
@kitlangton kitlangton mentioned this pull request Oct 4, 2026
refactor(client): share the service health probe between clients #53237
Merged
@kitlangton
kitlangton merged commit 25c54cb into v2 Oct 5, 2026
9 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@kitlangton
kitlangton deleted the restart-determinism branch October 5, 2026 03:59
Ichinose-Kazuki pushed a commit to Ichinose-Kazuki/opencode that referenced this pull request Oct 7, 2026
@kitlangton
fix(client): wait for service shutdown before restart (anomalyco#50042)
2c9332c
@neriousy neriousy mentioned this pull request Nov 13, 2026
cli: service restart can silently reuse an unresponsive incumbent #37795
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

contributor

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@kitlangton
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.