genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[JS] streamFlow.streamId stays pending when fetch fails before response headers

Open FOWEPJF255 opened this issue 3 weeks ago • 0 comments

Describe the bug

If fetch fails before receiving a response, streamFlow().output and its stream iterator reject, but streamFlow().streamId stays pending forever. A caller awaiting the stream ID to save or resume a remote stream cannot finish its error handling.

streamIdTask in js/genkit/src/client/client.ts is only resolved in responseCallback. The operation's rejection handler errors the channel without settling the stream-ID task. Network failures and aborts before headers never call responseCallback.

To Reproduce

Checked on main 68eb8cc80c14aa33a60bd977bf7600b8ed878d35, using the built genkit/beta/client export. No server or model is required:

import { streamFlow } from 'genkit/beta/client';

globalThis.fetch = async () => {
  throw new Error('connection failed');
};

const call = streamFlow({ url: 'http://stub', input: 'hello' });
const stream = call.stream[Symbol.asyncIterator]().next().then(
  () => 'resolved',
  (error) => `rejected: ${error.message}`,
);

console.log('output:', await call.output.then(
  () => 'resolved',
  (error) => `rejected: ${error.message}`,
));
console.log('stream:', await stream);
console.log('streamId:', await Promise.race([
  call.streamId.then(
    (id) => `resolved: ${id}`,
    (error) => `rejected: ${error.message}`,
  ),
  new Promise((resolve) => setTimeout(() => resolve('still pending'), 100)),
]));

Observed:

output: rejected: connection failed
stream: rejected: connection failed
streamId: still pending

Expected behavior

streamId settles when no response can arrive. Resolving null would match the existing result for a response without an x-genkit-stream-id header and avoid creating another unhandled rejection for callers that only consume output and stream. The operation error should still reach output and the stream unchanged. A stream ID already received from response headers should remain available even if reading the body later fails.

Runtime

  • OS: Windows
  • Version: Genkit 1.44.0, main 68eb8cc80

Node version

  • v24.18.0

Additional context

This is separate from #6781, which fixes EOF handling and split UTF-8 decoding inside the body-read loop. The failure here happens before that loop or any response callback runs. Searches for streamId promise, streamId reject and streamId hang found no existing report or fix for this task-settlement path. AI assistance was used for investigation and reproduction.

FOWEPJF255 avatar Nov 08 '26 18:11 FOWEPJF255