Skip to content

teeny-request throws when response handler destroys the request stream #8670

Open

Description

@rockwotj

Library Name

teeny-request 10.1.3

Try this API

Not applicable. This is a local Node.js stream lifecycle failure in teeny-request; the remote HTTP response succeeds.

Minimal reproduction

https://gist.github.com/rockwotj/a629eea7b9ccbd7e354659b9f09fead3

Run:

npm install [email protected] nock
node repro.js

On Node.js 24.16.0 this reports an unhandled rejection:

Error [ERR_STREAM_UNABLE_TO_PIPE]: Cannot pipe to a closed or destroyed stream

Steps and root cause

  1. Start consuming the stream returned by teenyRequest().
  2. Destroy that stream from its response event handler. Retry wrappers do this when discarding a response before another attempt.
  3. teeny-request then runs its later response listener and calls pipeline(responseStream, requestStream) with the destroyed destination.
  4. Node rejects the pipeline setup with ERR_STREAM_UNABLE_TO_PIPE; the rejection is not handled, and the response body is not disposed.

The stream-mode implementation defers piping until both reading has begun and the fetch response exists, but it does not recheck whether the returned request stream was destroyed by an earlier response listener.

Expected behavior

If the consumer destroys the returned stream before piping starts, teeny-request should skip pipeline construction and destroy the unused response body. No unhandled rejection should escape.

This is the same production stack previously reported in googleapis/teeny-request#351 and googleapis/teeny-request#352. Those reports were automatically closed for lacking a runnable reproduction.

Activity

  1. dich-bandana commented on Aug 24, 2026

    @dich-bandana

    We independently confirmed this issue in production on Cloud Run through @google-cloud/[email protected] -> [email protected]. A deterministic reproduction exits cleanly on Node 22.16.0 but produces the exact ERR_STREAM_UNABLE_TO_PIPE failure on Node 24.15.0 after the consumer destroys the request stream before the delayed response is piped.

    Google's fix is now open as #9160. I added our production trace, runtime comparison, downstream workaround, and alternatives analysis there: #9160 (comment)

  2. wally-tribute-labs commented on Oct 28, 2026

    @wally-tribute-labs

    Note

    🤖 Claude Opus 5.5 responding on behalf of wally-tribute-labs

    This still reproduces on the latest releases, @google-cloud/[email protected] → [email protected] on Node 24.22.1. No caller code is needed to trigger it: a single retryable 503 on a streamed read is enough. The library's own retry destroys the first request stream, then teeny-request's deferred pipeline() throws and the process exits.

    Repro against a local fake GCS endpoint (first response 503, second 200):

    import http from 'node:http'
    import { Storage } from '@google-cloud/storage'
    
    let hits = 0
    const server = http.createServer((req, res) => {
      hits++
      if (hits === 1) {
        res.writeHead(503, { 'content-type': 'application/json' })
        res.end('{"error":{"code":503,"message":"backend error"}}')
      } else {
        res.writeHead(200, { 'content-type': 'application/octet-stream' })
        res.end('abc')
      }
    })
    await new Promise((r) => server.listen(0, '127.0.0.1', r))
    const storage = new Storage({
      apiEndpoint: `http://127.0.0.1:${server.address().port}`,
      projectId: 'repro',
    })
    const stream = storage.bucket('b').file('o').createReadStream({ validation: false })
    try {
      for await (const _ of stream) {}
    } catch (err) {
      console.log('caught by caller:', err.message) // never reached
    }

    Output (exit code 1):

    Error [ERR_STREAM_UNABLE_TO_PIPE]: Cannot pipe to a closed or destroyed stream
        at pipelineImpl (node:internal/streams/pipeline:264:15)
        at pipeline (node:internal/streams/pipeline:183:10)
        at PassThrough.<anonymous> (node_modules/teeny-request/build/src/index.js:461:43)
        at PassThrough.wrapper (node:events:639:12)
        at PassThrough.emit (node:events:526:24)
        at node_modules/teeny-request/build/src/index.js:474:27
    

    We hit this in production on Cloud Run under Deno 2.9's Node compatibility layer (@google-cloud/[email protected] → [email protected]). There it surfaced as Uncaught (in promise) TypeError: k is not a constructor (Deno's rendering of the same throw) and exited the server instance, taking unrelated in-flight work with it. Until #9160 ships, our workaround is a process-level unhandledrejection handler.

  3. ScalingMBA commented on Nov 4, 2026

    @ScalingMBA

    Still reproducible with teeny-request 11.1.1, reached through @google-cloud/storage 8.4.0 (retry-request 9.1.0, node-fetch 3.3.2, Node.js 24.16.0), with default retry options.

    Trigger in our case: a local HTTP server answers the first alt=media GET of file.createReadStream() with 503 and the second with 200. The download succeeds, but process.on('unhandledRejection') receives ERR_STREAM_UNABLE_TO_PIPE: Cannot pipe to a closed or destroyed stream: retry-request destroys the first attempt's request stream on the retryable response, and teeny-request's once('response') handler then calls pipeline() into it. file.download() shows the same rejection, since it uses the same stream.

    Workaround that avoids it for us: retryOptions: { autoRetry: false } on the Storage client (one request, a clean 503 error).

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