#4468·blitz

[blitz-rpc] Concurrency race condition and query parameter leakage caused by singleton mutable routePathURL in __internal_buildRpcClient

Author: codeCraft-RitikCreated Sep 26, 2026Updated Sep 26, 2026
Labelskind/bugstatus/triage

What is the problem?

In packages/blitz-rpc/src/client/rpc.ts, __internal_buildRpcClient creates a singleton URL instance in outer scope:

typescript
const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
const routePathURL = new URL(fullRoutePath, window.location.origin)

This single mutable URL object is shared across all executions of the generated RPC client function.

This causes three bugs:

  1. Race Condition under Concurrency: If two React components invoke the query simultaneously with different parameters, Request 2 mutates routePathURL.searchParams before Request 1's fetch() fires. Both requests execute with Request 2's parameters, poisoning the TanStack Query cache.
  2. Stale Parameter Leakage: routePathURL.searchParams is never cleared. If a query is executed with parameters { status: "pending" } and later executed with undefined, the previous query parameters persist and are re-transmitted indefinitely.
  3. Falsy Values Dropped: if (httpMethod === "GET" && serialized.json) treats 0, false, and "" as falsy. Primitive parameters such as numerical ID 0 or boolean false are silently ignored and not attached to the query string.

Paste all your error logs here:

[blitz:rpc] Starting request for /api/rpc/getItem with { id: 101 }
[blitz:rpc] Starting request for /api/rpc/getItem with { id: 102 }
[blitz:rpc] Outgoing fetch URL: http://localhost:3000/api/rpc/getItem?params=%7B%22id%22%3A102%7D
[blitz:rpc] Outgoing fetch URL: http://localhost:3000/api/rpc/getItem?params=%7B%22id%22%3A102%7D
[Error] Component A received data for ID 102 instead of 101. Cache key mismatch in QueryClient.

Paste all relevant code snippets here:

typescript
// packages/blitz-rpc/src/client/rpc.ts (lines 50-87)
export function __internal_buildRpcClient({
  resolverName,
  resolverType,
  routePath,
  httpMethod,
}: BuildRpcClientParams): RpcClient {
  const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
  // BUG: Single URL instance shared across all calls to httpClient
  const routePathURL = new URL(fullRoutePath, window.location.origin)

  const httpClient: RpcClientBase = async (params, opts = {}, signal = undefined) => {
    ...
    // BUG: Falsy check drops 0, false, ""
    if (httpMethod === "GET" && serialized.json) {
      // BUG: Mutates the shared instance; never resets params if serialized.json is undefined
      routePathURL.searchParams.set("params", stringify(serialized.json))
      routePathURL.searchParams.set("meta", stringify(serialized.meta))
    }

    const {beforeHttpRequest, beforeHttpResponse} = globalThis.__BLITZ_MIDDLEWARE_HOOKS

    const promise = window
      .fetch(
        routePathURL,
        ...

What are detailed steps to reproduce this?

  1. Build an RPC client for a GET query:
    typescript
    const client = __internal_buildRpcClient({
      resolverName: "getItems",
      resolverType: "query",
      routePath: "/getItems",
      httpMethod: "GET",
    })
  2. Execute Call 1 with parameters:
    typescript
    await client({ category: "books" })
  3. Execute Call 2 without parameters:
    typescript
    await client(undefined)
  4. Observe the outgoing network request for Call 2:
    • Expected: http://localhost:3000/api/rpc/getItems
    • Actual: http://localhost:3000/api/rpc/getItems?params=%7B%22category%22%3A%22books%22%7D (leaked from Call 1!)

Run blitz -v and paste the output here:

macOS / Windows / Linux
blitz: 3.0.2 (local)
@blitzjs/auth: 3.0.2 (local)
@blitzjs/next: 3.0.2 (local)
@blitzjs/rpc: 3.0.2 (local)

Please include below any other applicable logs and screenshots that show your problem:

Concurrent Mutation Timeline:

Time t0: Component A triggers client({ id: 101 }) -> routePathURL.searchParams sets id=101
Time t1: Component B triggers client({ id: 102 }) -> routePathURL.searchParams overwrites id=102
Time t2: Component A's fetch(routePathURL) executes -> fetches id=102 (CORRUPTED)
Time t3: Component B's fetch(routePathURL) executes -> fetches id=102

Proposed Fix (Pull Request diff):

diff
--- a/packages/blitz-rpc/src/client/rpc.ts
+++ b/packages/blitz-rpc/src/client/rpc.ts
@@ -49,8 +49,8 @@ export function __internal_buildRpcClient({
   httpMethod,
 }: BuildRpcClientParams): RpcClient {
   const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
-  const routePathURL = new URL(fullRoutePath, window.location.origin)
   const httpClient: RpcClientBase = async (params, opts = {}, signal = undefined) => {
+    const routePathURL = new URL(fullRoutePath, window.location.origin)
     const debug = (await import("debug")).default("blitz:rpc")
     if (!opts.fromQueryHook && !opts.fromInvoke) {
       console.warn(
@@ -74,7 +74,7 @@ export function __internal_buildRpcClient({
       serialized = serialize(params)
     }
 
-    if (httpMethod === "GET" && serialized.json) {
+    if (httpMethod === "GET" && serialized.json !== undefined) {
       routePathURL.searchParams.set("params", stringify(serialized.json))
       routePathURL.searchParams.set("meta", stringify(serialized.meta))
     }