google-gax: native server-streaming cancellation can dispatch another RPC or throw before setup

Aperta
#9,862 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
grpc, node.js, typescript

Direzione di ricerca

Start in core/packages/gax/src/streamingCalls/streaming.ts, especially StreamProxy.cancel(), newStreamingRetryRequest(), and newMakeRequest(); trace how cancellation and retry timers interact. Add regression tests for the four cases listed in the issue, then run the gax package tests covering streaming calls. Done means early cancellation does not start or dereference a request, cancellation stops queued retries and cancels the active attempt, and normal retry and success still work.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

With native server-streaming retries enabled (gaxStreamingRetries: true), cancellation does not terminate the retry state machine:

  • Cancelling during retry backoff still dispatches the next RPC.
  • Cancelling before stream setup causes TypeError: Cannot read properties of undefined (reading 'cancel').
  • The active request's cancel method is copied onto the retry wrapper without binding its receiver.

Reproduced on main at c1c70497020b6d0552012a479f538a4b187196ac, macOS arm64, Node 24.22.3. This reproduction uses only local streams and fake timers; no Google Cloud service or credentials are involved.

Reproduction

After installing dependencies and compiling google-gax, run from core/packages/gax:

const {StreamProxy, StreamType} =
  require('./build/src/streamingCalls/streaming.js');
const {createDefaultBackoffSettings} = require('./build/src/gax.js');
const sinon = require('sinon');
const {PassThrough} = require('stream');

const retry = {
  retryCodes: [14],
  backoffSettings: {
    ...createDefaultBackoffSettings(),
    totalTimeoutMillis: undefined,
    maxRetries: 3,
    initialRetryDelayMillis: 100,
  },
};
const clock = sinon.useFakeTimers();
const requests = [];
const proxy = new StreamProxy(
  StreamType.SERVER_STREAMING, () => {}, false, true,
);
proxy.on('error', () => {});
proxy.setStream(() => {
  const stream = new PassThrough({objectMode: true});
  stream.cancel = sinon.spy();
  requests.push(stream);
  return stream;
}, {}, {}, retry);

requests[0].emit(
  'error', Object.assign(new Error('unavailable'), {code: 14}),
);
proxy.cancel();
clock.tick(100);
console.log(requests.length); // Actual: 2; expected: 1.
clock.restore();

const early = new StreamProxy(
  StreamType.SERVER_STREAMING, () => {}, false, true,
);
early.cancel();
early.setStream(
  () => new PassThrough({objectMode: true}), {}, {}, retry,
); // Throws TypeError reading 'cancel' from undefined.
Cause and proposed scope

StreamProxy.cancel() records _isCancelCalled only when this.stream is absent. Once the retry wrapper exists, cancellation therefore does not block a queued retry. newStreamingRetryRequest() does not retain/clear its retry timer and assigns retryStream.cancel = requestStream.cancel. Conversely, when cancellation happened before setup, the request factory returns undefined and newMakeRequest() immediately dereferences it.

I would like to contribute a narrowly scoped fix that owns cancellation in the native streaming retry state machine: preserve the active transport receiver when cancelling, prevent/clear scheduled retries, and handle cancellation before initialization without starting a request or dereferencing an absent stream. Regression tests would cover early cancellation, cancellation during backoff, cancellation of the current attempt after a retry, and normal retry/success behavior.

I checked the open PR list and cancellation-related PRs. #9359 changes normalCalls/retries.ts (unary calls), so it does not address this streaming path. This report concerns gRPC/native streaming retries, rather than the REST fallback transport issues.

Is this scope acceptable, and is anyone already implementing a fix for this streaming path?

Lingua principale
TypeScript
Stelle
3.2k
Fork
732
Merge medio
2g 11h
PR unite (30g)
99

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di googleapis/google-cloud-node

Tutte le issue di googleapis/google-cloud-node

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.