Skip to content

refactor(server): simplify listener lifecycle#27413

Merged
kitlangton merged 5 commits into
devfrom
refactor/server-listener-lifecycle
May 15, 2026
Merged

refactor(server): simplify listener lifecycle#27413
kitlangton merged 5 commits into
devfrom
refactor/server-listener-lifecycle

Conversation

@kitlangton
Copy link
Copy Markdown
Contributor

@kitlangton kitlangton commented May 14, 2026

Summary

  • Move Server.listen to a Promise facade over an Effect-native listener lifecycle with named startup, port fallback, mDNS, force-close, and stop helpers.
  • Replace ad hoc stop Promise caching with Effect.cached once-only shutdown effects and register mDNS cleanup on the listener scope.
  • Simplify the default HTTP API handler export and route-layer typing so the listener build no longer needs the unsafe layer cast.
  • Rename the route app namespace from ExperimentalHttpApiServer to HttpApiApp.
  • Remove the obsolete opencode.server.backend route middleware that was only useful during the old backend split.
  • Disable Effect's default HTTP middleware logger for the in-process web handler so TUI/worker requests do not print Sent HTTP response logs to stderr.
  • Add shutdown behavior coverage for concurrent/repeated forced stop and forcing an in-progress graceful stop.
  • Add regression coverage for in-process handler log suppression and custom CORS denial.

Testing

  • bunx prettier --write src/server/server.ts src/server/routes/instance/httpapi/server.ts test/server/httpapi-ui.test.ts test/server/httpapi-listen.test.ts test/server/httpapi-cors.test.ts from packages/opencode
  • bunx oxlint packages/opencode/src/server/server.ts packages/opencode/src/server/routes/instance/httpapi/server.ts packages/opencode/test/server/httpapi-ui.test.ts packages/opencode/test/server/httpapi-listen.test.ts packages/opencode/test/server/httpapi-cors.test.ts from repo root (warnings only in pre-existing listen-test assertions)
  • bun typecheck from packages/opencode
  • bun test --timeout 30000 test/server/httpapi-listen.test.ts test/server/httpapi-cors.test.ts test/server/httpapi-ui.test.ts from packages/opencode
  • Reproduced the TUI log spam on pre-fix commit 6fd0cda3e via bun dev and verified the current PR branch no longer prints Sent HTTP response logs during startup or /status
  • Ran five parallel refactor-review agents against origin/dev...origin/refactor/server-listener-lifecycle for API surface, behavioral equivalence, safety/edge cases, test coverage, and dead-code cleanup.

Notes

  • repo-root bun turbo typecheck was attempted with the repo-pinned Bun 1.3.13, but still fails in unrelated packages/core Bun/DOM response typings.
  • Residual untested edge cases: mDNS publish/unpublish and the explicit port: 0 prefers-4096 fallback path. Both are preserved by inspection; testing them would require extra global stubbing or port coordination.

@kitlangton kitlangton force-pushed the refactor/server-listener-lifecycle branch 2 times, most recently from a3aa1f4 to eccd81e Compare May 14, 2026 01:31
@kitlangton kitlangton force-pushed the refactor/server-listener-lifecycle branch from eccd81e to 6fd0cda Compare May 14, 2026 02:00
@kitlangton kitlangton enabled auto-merge (squash) May 14, 2026 23:20
The forceRequested Ref + trailing re-yield of forceCloseOnce only forced
graceful stops to wait for a concurrent force-close's WebSocketTracker
cleanup. The scope close already triggers closeAllConnections via the
serverRef.forceStop flag, so all callers observe the same end state
without it.
Add the previously-untested edge cases:
- port: 0 prefers 4096 when free
- port: 0 falls back to a random port when 4096 is taken
- mDNS is skipped on loopback hostnames
- mDNS publishes on non-loopback hostnames and unpublishes on stop(true)
- mDNS unpublishes on graceful stop() via the scope finalizer
@kitlangton kitlangton force-pushed the refactor/server-listener-lifecycle branch from d782361 to fb62bc6 Compare May 14, 2026 23:51
@kitlangton kitlangton merged commit 195f592 into dev May 15, 2026
10 checks passed
@kitlangton kitlangton deleted the refactor/server-listener-lifecycle branch May 15, 2026 00:00
rustybret pushed a commit to rustybret/opencode that referenced this pull request May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant