Skip to content

Pool hangs forever after a request is rejected while the protocol probe is pending (fresh Pool / after clientTtl rotation) #6141

Done

Description

@timothyliu

Bug Description

With allowH2: true on an https: origin, Pool designates one Client as the protocol probe (kProtocolProbe in lib/dispatcher/pool.js) and keeps later requests queued until ALPN completes.

If the request that triggered the probe is rejected while the Request is being built (for example an expect header → NotSupportedError, or keep-alive / upgrade / transfer-encoding → InvalidArgumentError), the probe client never connects, so the probe state is never cleared. From then on kGetDispatcher returns undefined for every request, and every later request on that Pool stays queued forever.

Because those requests are never dispatched, headersTimeout / bodyTimeout never fire. The only way out is to destroy the Pool.

This happens:

  • on the first request of a fresh Pool, and
  • on the first request after a clientTtl rotation (the replacement client becomes the probe again).

A warm Pool is not affected: only the bad request fails, as expected. undici 7.30.1 is not affected in any of the three cases.

Reproducible By

The script below starts a local HTTP/2 TLS server and runs three cases. It needs only undici and openssl.

repro.mjs
// Minimal reproduction: a request that is rejected while the Pool is probing the
// protocol (first request on a fresh Pool, or first request after clientTtl rotation)
// leaves the probe state set forever; every later request on that Pool hangs.
//
// Usage: node repro.mjs
// Requires: [email protected], Node.js >= 22.19 (tested on 24.22.1)
import http2 from 'node:http2'
import { execFileSync } from 'node:child_process'
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Pool } from 'undici'

// Throwaway self-signed certificate for localhost
const dir = mkdtempSync(join(tmpdir(), 'undici-probe-'))
execFileSync('openssl', ['req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', join(dir, 'key.pem'),
  '-out', join(dir, 'cert.pem'), '-days', '1', '-subj', '/CN=localhost'], { stdio: 'ignore' })
const server = http2.createSecureServer({ key: readFileSync(join(dir, 'key.pem')), cert: readFileSync(join(dir, 'cert.pem')) })
server.on('stream', (stream) => { stream.respond({ ':status': 200 }); stream.end('ok') })
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
const origin = `https://localhost:${server.address().port}`

const fire = (pool, headers) => Promise.race([
  pool.request({ path: '/', method: 'GET', headers })
    .then(async (res) => { await res.body.text(); return String(res.statusCode) })
    .catch((err) => err.code),
  new Promise((resolve) => setTimeout(() => resolve('STILL PENDING after 3s'), 3000)),
])

const opts = { allowH2: true, headersTimeout: 2000, bodyTimeout: 2000, connect: { rejectUnauthorized: false } }

// Case 1: fresh Pool, first request rejected at Request construction
{
  const pool = new Pool(origin, opts)
  console.log('fresh pool, first request with Expect:', await fire(pool, { expect: '100-continue' }))
  console.log('fresh pool, next request          :', await fire(pool))
  await pool.destroy().catch(() => {})
}

// Case 2: warm Pool -> only the bad request fails (expected)
{
  const pool = new Pool(origin, opts)
  console.log('warm pool, warm-up request        :', await fire(pool))
  console.log('warm pool, request with Expect    :', await fire(pool, { expect: '100-continue' }))
  console.log('warm pool, next request           :', await fire(pool))
  await pool.destroy().catch(() => {})
}

// Case 3: after clientTtl rotation, the first request is rejected
{
  const pool = new Pool(origin, { ...opts, clientTtl: 200 })
  console.log('ttl pool, warm-up request         :', await fire(pool))
  await new Promise((resolve) => setTimeout(resolve, 300))
  console.log('ttl pool, request with Expect     :', await fire(pool, { expect: '100-continue' }))
  console.log('ttl pool, next request            :', await fire(pool))
  await pool.destroy().catch(() => {})
}

server.close()
rmSync(dir, { recursive: true, force: true })

Output with undici 8.12.3 (Node.js 24.22.1):

fresh pool, first request with Expect: UND_ERR_NOT_SUPPORTED
fresh pool, next request          : STILL PENDING after 3s
warm pool, warm-up request        : 200
warm pool, request with Expect    : UND_ERR_NOT_SUPPORTED
warm pool, next request           : 200
ttl pool, warm-up request         : 200
ttl pool, request with Expect     : UND_ERR_NOT_SUPPORTED
ttl pool, next request            : STILL PENDING after 3s

With undici 7.30.1, every "next request" line is 200.

Expected Behavior

A request that is rejected during construction should only fail itself. Later requests on the same Pool should be dispatched normally, as they are on a warm Pool.

Additional context

This matters for reverse proxies that forward client-supplied headers. A single client request carrying Expect: 100-continue can permanently wedge the upstream Pool: on a fresh Pool, or within the window after each clientTtl rotation. We work around it by stripping these headers before calling pool.request().

The probe gate appears to come from #5799 (avoid pool fan-out during ALPN negotiation).

A possible fix is to clear kProtocolProbe (and drain the queue) when the probe client errors or is removed without ever connecting, or to only assign the probe after the request has been accepted by Request construction.

Environment

  • undici 8.12.3
  • Node.js 24.22.1 (also requires >= 22.19 for undici 8)
  • macOS (also expected on Linux; the issue is in Pool state handling)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions