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 — refactor: replace Bun.serve with Node http.createServer in OAuth handlers by thdxr · Pull Request #18327 · anomalyco/opencode

Conversation

@thdxr

@thdxr thdxr commented Mar 20, 2026

Copy link
Copy Markdown
Member

Summary

  • Replace Bun.serve with http.createServer in MCP OAuth callback server
  • Replace Bun.serve with http.createServer in Codex plugin OAuth server
  • Both use the same pattern: express-style request handlers with res.writeHead/res.end

Sorry, something went wrong.

Uh oh!

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

greptile-apps[bot] reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@thdxr
refactor: replace Bun.serve with Node http.createServer in OAuth hand…
816a5f7
…lers

Use Node's createServer for MCP OAuth callback and Codex plugin OAuth
servers instead of Bun.serve, making them work under Node.js.
@github-actions github-actions Bot added the Vouched label Mar 20, 2026
thdxr added a commit that referenced this pull request Mar 20, 2026
@thdxr
chore: extract OAuth changes into #18327
65e7862
@greptile-apps

greptile-apps Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR refactors both OAuth servers — in mcp/oauth-callback.ts and plugin/codex.ts — to replace Bun.serve with Node.js http.createServer, aligning them with a more portable, runtime-agnostic pattern. The core request-handling logic is preserved faithfully; writeHead/end replace new Response(...) and server lifecycle is wrapped in Promises.

Key observations:

  • Note that AGENTS.md explicitly states "Use Bun APIs when possible". Replacing Bun.serve with Node.js HTTP goes against this guideline. If this is a deliberate step toward making opencode runtime-agnostic (e.g. for Node.js compatibility), that rationale should be documented.
  • packages/opencode/src/mcp/oauth-callback.ts: The migration is clean. stop() correctly awaits server.close() before clearing the reference, ensuring the port is fully released.
  • packages/opencode/src/plugin/codex.ts: stopOAuthServer sets oauthServer = undefined synchronously while server.close() is asynchronous. This creates a race condition: a subsequent startOAuthServer() call may attempt to listen() on a port that is still bound, resulting in an EADDRINUSE error. This is an inconsistency with how oauth-callback.ts handles the same teardown.

Confidence Score: 3/5

  • Mostly safe, but the async teardown race in codex.ts stopOAuthServer could cause EADDRINUSE on rapid re-authentication
  • The oauth-callback.ts migration is correct and the logic is functionally equivalent to the Bun.serve version. However, codex.ts has a real teardown race condition introduced by this refactor that wasn't present with Bun.serve's synchronous stop(), and the change contradicts the AGENTS.md "Use Bun APIs when possible" guideline without explanation.
  • packages/opencode/src/plugin/codex.ts — specifically the stopOAuthServer function at line 324

Important Files Changed

Filename Overview
packages/opencode/src/mcp/oauth-callback.ts Bun.serve replaced with http.createServer; logic is functionally equivalent, stop() correctly awaits close, minor style issue with inline import types in handleRequest signature
packages/opencode/src/plugin/codex.ts Bun.serve replaced with http.createServer; stopOAuthServer sets oauthServer to undefined synchronously while close() is async, creating a race condition if startOAuthServer is called before the port is fully released

Sequence Diagram

sequenceDiagram
    participant App
    participant Server as http.createServer
    participant Handler as Request Handler
    participant Pending as pendingOAuth/pendingAuths

    App->>Server: createServer(handler) + listen(PORT)
    Note over Server: Listening on PORT

    App->>Pending: waitForOAuthCallback(state)
    Note over App: Browser redirected to localhost

    Server->>Handler: GET /auth/callback?code=X&state=Y
    Handler->>Pending: resolve(code or tokens)
    Handler->>Server: res.writeHead(200) + res.end(HTML_SUCCESS)

    alt codex.ts stopOAuthServer
        App->>Server: server.close(cb) — async
        App->>App: oauthServer = undefined — SYNC (race!)
        Note over Server: Port may still be bound when next startOAuthServer runs
    else oauth-callback.ts stop
        App->>Server: await server.close(cb)
        Note over Server: Port released before returning
        App->>App: server = undefined
    end
Loading

Last reviewed commit: "refactor: replace Bu..."

All reactions

Sorry, something went wrong.

Uh oh!

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

greptile-apps[bot]
greptile-apps Bot reviewed Mar 20, 2026
View reviewed changes
Comment thread packages/opencode/src/plugin/codex.ts
Comment on lines 324 to 331
function stopOAuthServer() {
if (oauthServer) {
oauthServer.stop()
oauthServer.close(() => {
log.info("codex oauth server stopped")
})
oauthServer = undefined
log.info("codex oauth server stopped")
}
}

@greptile-apps greptile-apps Bot Mar 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Race condition: port still in use after stopOAuthServer returns

oauthServer is set to undefined synchronously, but server.close() in Node.js is asynchronous — the underlying port is only released once the close callback fires. If startOAuthServer() is called again before that callback fires (e.g. the user immediately re-initiates auth), oauthServer is undefined so the early-return guard is bypassed, and the new listen() call will fail with EADDRINUSE.

By contrast, Bun.serve.stop() released the port synchronously. The sibling McpOAuthCallback.stop() correctly awaits the close before clearing the reference:

await new Promise<void>((resolve) => server!.close(() => resolve()))
server = undefined

stopOAuthServer should follow the same pattern (make it async and await the close) or at minimum only clear oauthServer inside the close callback:

function stopOAuthServer() {
  if (oauthServer) {
    const closing = oauthServer
    oauthServer = undefined
    closing.close(() => {
      log.info("codex oauth server stopped")
    })
  }
}

Note: even the snippet above doesn't fully close the port before returning. If true synchronous-style safety is needed, the function should be made async and awaited at the call site.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread packages/opencode/src/mcp/oauth-callback.ts

export async function ensureRunning(): Promise<void> {
if (server) return
function handleRequest(req: import("http").IncomingMessage, res: import("http").ServerResponse) {

@greptile-apps greptile-apps Bot Mar 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Inline dynamic import types are unnecessary

import("http").IncomingMessage and import("http").ServerResponse are used as inline type expressions, but http is already imported at line 2. The types can be imported alongside createServer to keep the signature readable.

Suggested change
function handleRequest(req: import("http").IncomingMessage, res: import("http").ServerResponse) {
function handleRequest(req: import("http").IncomingMessage, res: import("http").ServerResponse) {

Change the top-level import to:

import { createServer, type IncomingMessage, type ServerResponse } from "http"

Then the function signature becomes:

function handleRequest(req: IncomingMessage, res: ServerResponse) {

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Sorry, something went wrong.

Uh oh!

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

All reactions
@thdxr thdxr added the beta label Mar 20, 2026
@github-actions github-actions Bot mentioned this pull request Mar 20, 2026
📊 Bản tin hàng ngày công cụ AI CLI 2026-03-20 compasify/agents-radar#60
Open
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
10d4f22
…ver in OAuth handlers
@thdxr thdxr mentioned this pull request Mar 20, 2026
refactor(server): replace Bun serve with Hono node adapters #18335
Merged
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
0af8184
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
a73f178
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
9e8e7c3
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
d0fcc72
…ver in OAuth handlers
@thdxr
Merge branch 'dev' into refactor/oauth-node-http
9f5713e
@thdxr thdxr added beta and removed beta labels Mar 20, 2026
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
9d5a661
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
6fc220d
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
a7eee37
…ver in OAuth handlers
@thdxr thdxr added beta and removed beta labels Mar 20, 2026
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
5556be9
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 20, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
170ca7d
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 21, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
172de14
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 21, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
146cf0a
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 21, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
7076af2
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 21, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
bd445b3
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
6720c7a
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
d981ddc
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
e64d1af
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
fcaf094
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
496400f
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
b226686
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
2e3a7a8
…ver in OAuth handlers
opencode-agent Bot added a commit that referenced this pull request Mar 22, 2026
@opencode-agent
Apply PR #18327: refactor: replace Bun.serve with Node http.createSer…
32fffe0
…ver in OAuth handlers
@opencode-agent

opencode-agent Bot commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

⚠️ Blocking Beta Release

This PR cannot be merged into the beta branch due to: Merge failed

Please resolve this issue to include this PR in the next beta release.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Hona
Merge branch 'dev' into refactor/oauth-node-http
a722045
@sjqwert

sjqwert commented Apr 5, 2026

Copy link
Copy Markdown

⚠️ Blocking Beta Release

This PR cannot be merged into the beta branch due to: Merge failed

Please resolve this issue to include this PR in the next beta release.

All reactions

Sorry, something went wrong.

Uh oh!

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

1 similar comment
@sjqwert

sjqwert commented Apr 5, 2026

Copy link
Copy Markdown

⚠️ Blocking Beta Release

This PR cannot be merged into the beta branch due to: Merge failed

Please resolve this issue to include this PR in the next beta release.

All reactions

Sorry, something went wrong.

Uh oh!

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

This was referenced Apr 6, 2026
chore: sync upstream/dev (Hono migration, LSP fix, OpenTUI 0.1.97) #21248
Closed
chore: sync upstream/dev (Hono migration, LSP fix, OpenTUI 0.1.97) Cor-Incorporated/opencode#133
Merged
@achin90 achin90 mentioned this pull request Apr 10, 2026
DispatchTUI prtest achin90/Dispatch#3
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@greptile-apps greptile-apps[bot] greptile-apps[bot] left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

beta Vouched

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.

3 participants

@thdxr @sjqwert @Hona
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.