Skip to content

fix(server): answer requests that arrive before the request listener is attached - #56642

Open
doublepi123 wants to merge 1 commit into
anomalyco:v1from
doublepi123:fix/serve-early-request-drop
Open

doublepi123 wants to merge 1 commit into
anomalyco:v1from
doublepi123:fix/serve-early-request-drop

Conversation

@doublepi123

Copy link
Copy Markdown

Issue for this PR

Closes #56641

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

NodeHttpServer.layer starts listening before HttpRouter.serve adds the "request" listener. The route layers are built in between, which takes a few hundred ms. A request that arrives in that window is emitted to no listener, so Node drops it and the client waits on an open socket forever.

serverLayer now registers a temporary "request" listener that holds early requests. When the real listener is added (detected with newListener), the temporary one is removed and the held requests are re-emitted on the next tick. newListener fires before the listener is actually added, which is why the replay waits a tick. After startup there is no extra listener and no per-request overhead.

Targets v1 because that is where 1.x is released from. dev already avoids this by answering 503 until the routes are ready.

How did you verify your code works?

  • Added a test in httpapi-listen.test.ts that starts Server.listen and sends a request on the first accepted connection. It times out without the fix and passes with it.
  • bun test test/server/httpapi-listen.test.ts: 12 pass, run 3 times. bun run typecheck passes. In test/server/, project-copy.test.ts fails the same way with and without this change; everything else passes.
  • Ran opencode serve from source and sent /api/info the moment the port accepted. Without the fix 4/4 hung. With the fix 6/6 got 200 after about 0.6 s.

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…is attached

NodeHttpServer listens before HttpRouter.serve adds the "request"
listener, and building the route layers in between takes a few hundred
ms. Node drops a request emitted with no listener, so a client that
connects as soon as the port opens waits forever. Hold those requests
and replay them once the real listener is attached.

Closes anomalyco#56641
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

This branch has not been deployed

No deployments
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.

1 participant