Skip to content

fix: accept Next.js static-export file names, and serve a request whose service changed mid-build - #1977

Merged
devin-ai-integration[bot] merged 3 commits into
mainfrom
fix/deploy-keys-handler-rebuild
Nov 9, 2026
Merged

devin-ai-integration[bot] merged 3 commits into
mainfrom
fix/deploy-keys-handler-rebuild

Conversation

@yyyyaaa

@yyyyaaa yyyyaaa commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Two fixes found while dogfooding the desktop's fast path and Deploy on devnet.

Accept the file names a Next.js static export writes

A Next 16 static export names its segment-prefetch files with characters the custom-key rule rejected, so one such file failed a whole site deploy (INVALID_KEY: must start with alphanumeric or underscore …):

Next writes For
__next.!KGFwcCk.__PAGE__.txt a route group, ! plus its base64url name ((app))
tools/__next.$d$id.__PAGE__.txt a dynamic segment, $<type>$<name>
inbox/__next.@modal.__PAGE__.txt a parallel-route slot
  • custom-key.ts allows !, $ and @ after the first character. None of them is percent-encoded in a URL path, so the key a browser asks for is the key that was stored.
  • The length, .., leading-slash and NUL checks are unchanged. A key still can't start with one of the three.

Serve a request whose service changed during its handler build

Right after a schema build, a request to the new tenant API could fail with Failed to create handler for api-<db>…: service … was invalidated while its handler was being built, though the next request succeeded. The schema change's flush evicts the service while the first handler build is running. That build is released, as before, but the request that started it, and any coalesced waiter, used to return the error.

sequenceDiagram
    participant R as request
    participant G as graphile middleware
    participant F as flush (schema change)
    R->>G: handler for service key
    G->>G: build handler (generation n)
    F->>G: evictServiceKey (generation n+1)
    G->>G: release stale build
    G->>G: getApiConfig() again, build again
    G-->>R: served by the new handler
Loading
  • graphile.ts: an invalidated build throws ServiceInvalidatedError. The request resolves its service again (getApiConfig, the same resolver the api middleware uses), sets req.api, and goes through the cache, coalescing and build phases again.
  • At most three resolutions per request, so a service that keeps changing still fails, with the reason.
  • A build's finally deletes only its own in-flight entry, never a newer build's.

🤖 Generated with Claude Code

https://claude.ai/code/session_017WbsG7JQsKEMKNgVsc4psk

…export file names

A Next.js static export names its segment-prefetch files with ! (a route group as
! plus base64url), $ (a dynamic segment) and @ (a parallel-route slot). The custom
key rule rejected all three, so one such file failed a whole site deploy.
… handler build

A schema change flushes the service as it lands. A request that had started the
handler build just before that got 'service ... was invalidated while its handler
was being built' as an internal error, although the next request succeeded. The
request now resolves its service again and builds with it, up to three
resolutions, so a service that keeps changing still fails with the reason. A
build's finally block also no longer deletes a newer build's in-flight entry.
@luxor-code-review

luxor-code-review Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 2 medium

💬 Inline comments (2)

  • 🟡 Retry-path errors leak internal svc_key details to clients — graphile.ts:483
  • 🟡 Failed realtimeManager.stop() skips pgl.release() — graphile.ts:418
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Deleted service surfaces as 500 instead of 404 (graphile.ts:482) — When a service is evicted mid-build and no longer resolves, the retry path throws HandlerCreationError ('the service no longer resolves'), which the outer catch converts to a 500 INTERNAL_FAILURE (graphql/server/src/middleware/graphile.ts:482-486).

The PR extends the custom presigned-URL key validator's middle-alphabet character set and adds matching rejection tests. The bulk of the change is a rework of the single-flight handlerFor path in the GraphQL server middleware: cache rejections now trigger a bounded rebuild-and-serve retry loop guarded by MAX_SERVICE_RESOLUTIONS, in-flight promise entries are cleaned up in finally, and the isApiError duck-type guard in api.ts is exported for reuse by the retry path.

Files Change
graphile/graphile-presigned-url-plugin/src/custom-key.ts + __tests__/custom-key.test.ts Widens the allowed object-key alphabet with !, $, @ and adds tests for the new accept/reject cases.
graphql/server/src/middleware/graphile.ts Reworks schema-handler resolution: bounded retry on ServiceInvalidatedError, in-flight map cleanup, mid-request req.api re-resolution.
graphql/server/src/middleware/__tests__/graphile-single-flight.test.ts Updates tests to expect rebuild-and-serve semantics and a bounded-retry failure case.
graphql/server/src/middleware/api.ts Exports the isApiError type guard so the retry path can reuse it.

Reviewed commit: 3fc316a

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adds !, $, and @ to the allowed custom S3 object-key alphabet with test coverage, and refactors the single-flight schema-handler resolution in the GraphQL middleware with bounded retries and an exported isApiError guard.

Key findings

  • 🟡 Retry-path errors leak internal svc_key details to clients — graphile.ts:483
  • 🟡 Failed realtimeManager.stop() skips pgl.release() — graphile.ts:418

Comment on lines +483 to +485
throw new HandlerCreationError(`Failed to create handler for ${key}: the service no longer resolves`, {
cacheKey: key
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 security · medium

Retry-path errors leak internal svc_key details to clients

On the re-resolution path, HandlerCreationError messages embed the internal service cache key — Failed to create handler for ${key}: the service no longer resolves (graphql/server/src/middleware/graphile.ts:483) — and the outer catch forwards e.message verbatim into errors.INTERNAL_FAILURE({ details }) (graphile.ts:499-501), which respondWithGraphQLError serializes into the client-visible error body. If getApiConfig throws inside the retry (resolution diagnostics embedding schema lists, hosts, and database ids), that raw message is surfaced too. Clients learn internal cache-key naming and resolution-plane details on every failed re-resolution.

📋 Prompt for AI Agents

In graphql/server/src/middleware/graphile.ts lines 468-506: wrap the getApiConfig(opts as ApiOptions, req) call at line 481 in its own try/catch so a resolution throw does not propagate raw; and replace the client-visible detail strings so details never contains the internal key or the cause's raw message — use a fixed generic detail such as 'the service is being reconfigured' for both the 'no longer resolves' and MAX_SERVICE_RESOLUTIONS HandlerCreationError cases (lines 475-485), keeping full context in the adjacent log calls. Why: these messages reach the outer catch at lines 494-503 and are serialized to clients via respondWithGraphQLError.

Comment on lines +418 to +420
await instance.realtimeManager?.stop();
await instance.pgl.release();
throw new ServiceInvalidatedError(key);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 bug · medium

Failed realtimeManager.stop() skips pgl.release()

On the service-invalidated teardown path, await instance.realtimeManager?.stop() and await instance.pgl.release() run as sequential awaits with no cleanup guard (graphql/server/src/middleware/graphile.ts:418-419). If stop() rejects, pgl.release() never runs and the builder's pool client/handle leaks — and the rejected error replaces the ServiceInvalidatedError signal so coalesced waiters also miss the invalidation path. The instance is discarded either way, so the leak is permanent for that build.

📋 Prompt for AI Agents

In graphql/server/src/middleware/graphile.ts lines 417-421, replace the sequential awaits with a try/finally: attempt await instance.realtimeManager?.stop() inside the try (log or swallow its error), call await instance.pgl.release() in the finally block, and still throw new ServiceInvalidatedError(key) afterwards so coalesced waiters keep the invalidation signal.

@yyyyaaa
yyyyaaa requested a review from Anmol1696 November 9, 2026 02:36
…release a discarded build

Review of the handler rebuild:
- A handler failure reached the client as 'Failed to create handler for <service
  key>: …', and the key of a header-routed request names the database and its
  schemas. The message now says 'this API'; the key stays in the log and the
  error's context. A re-resolution that throws answers a fixed reason.
- A failing realtime teardown skipped releasing the discarded build. Both steps
  now run, and the invalidation still reaches the waiters.
- A service that no longer resolves after the rebuild answers 404, as a fresh
  request for it does, not 500.
@devin-ai-integration
devin-ai-integration Bot merged commit f57a39b into main Nov 9, 2026
19 checks passed
@devin-ai-integration
devin-ai-integration Bot deleted the fix/deploy-keys-handler-rebuild branch November 9, 2026 03:21
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