Skip to content

Commit 6789005

Browse files
orgadssofisl
andauthored
fix: avoid unhandled exception when reusing a closed client (#1640)
Example: * Execute streamingRecognize * When done, close the client * Execute streamingRecognize again, with data already in the stream * Unhandled exception occurs. TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object at _write (node:internal/streams/writable:474:13) at Writable.write (node:internal/streams/writable:502:10) at Duplexify._write (/project/node_modules/duplexify/index.js:212:22) at doWrite (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:390:139) at writeOrBuffer (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:381:5) at Writable.write (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:302:11) at Pumpify.<anonymous> (/project/node_modules/@google-cloud/speech/build/src/helpers.js:79:27) at Object.onceWrapper (node:events:633:26) at Pumpify.emit (node:events:518:28) at obj.<computed> [as _write] (/project/node_modules/stubs/index.js:28:22) at doWrite (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:390:139) at writeOrBuffer (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:381:5) at Writable.write (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:302:11) at PassThrough.ondata (node:internal/streams/readable:1007:22) at PassThrough.emit (node:events:518:28) at addChunk (node:internal/streams/readable:559:12) { code: 'ERR_INVALID_ARG_TYPE' Reproduction: https://gist.github.com/orgads/13cbf44c91923da27d8772b5f10489c9 This happens because streamingRecognize writes streamingConfig on first chunk. Usually the stream is open in object mode, so it works, but when the client is terminated, PassThrough is used without object mode. Change PassThrough to object mode, so it terminates gracefully. This was reported in [1] and fixed in [2], but was then reverted in [3] which re-generated the code. Fix properly in the generator now. [1] #5464 [2] #5465 [3] #5565 Co-authored-by: sofisl <55454395+sofisl@users.noreply.github.com>
1 parent 8e4dd1c commit 6789005

16 files changed

Lines changed: 16 additions & 16 deletions

File tree

core/generator/gapic-generator-typescript/baselines/bigquery-storage-esm/esm/src/v1beta1/big_query_storage_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export class BigQueryStorageClient {
259259
stub => (...args: Array<{}>) => {
260260
if (this._terminated) {
261261
if (methodName in this.descriptors.stream) {
262-
const stream = new PassThrough();
262+
const stream = new PassThrough({objectMode: true});
263263
setImmediate(() => {
264264
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
265265
});

core/generator/gapic-generator-typescript/baselines/bigquery-storage/src/v1beta1/big_query_storage_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export class BigQueryStorageClient {
236236
stub => (...args: Array<{}>) => {
237237
if (this._terminated) {
238238
if (methodName in this.descriptors.stream) {
239-
const stream = new PassThrough();
239+
const stream = new PassThrough({objectMode: true});
240240
setImmediate(() => {
241241
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
242242
});

core/generator/gapic-generator-typescript/baselines/disable-packing-test-esm/esm/src/v1beta1/echo_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export class EchoClient {
326326
stub => (...args: Array<{}>) => {
327327
if (this._terminated) {
328328
if (methodName in this.descriptors.stream) {
329-
const stream = new PassThrough();
329+
const stream = new PassThrough({objectMode: true});
330330
setImmediate(() => {
331331
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
332332
});

core/generator/gapic-generator-typescript/baselines/disable-packing-test-esm/esm/src/v1beta1/messaging_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ export class MessagingClient {
325325
stub => (...args: Array<{}>) => {
326326
if (this._terminated) {
327327
if (methodName in this.descriptors.stream) {
328-
const stream = new PassThrough();
328+
const stream = new PassThrough({objectMode: true});
329329
setImmediate(() => {
330330
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
331331
});

core/generator/gapic-generator-typescript/baselines/disable-packing-test/src/v1beta1/echo_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export class EchoClient {
303303
stub => (...args: Array<{}>) => {
304304
if (this._terminated) {
305305
if (methodName in this.descriptors.stream) {
306-
const stream = new PassThrough();
306+
const stream = new PassThrough({objectMode: true});
307307
setImmediate(() => {
308308
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
309309
});

core/generator/gapic-generator-typescript/baselines/disable-packing-test/src/v1beta1/messaging_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export class MessagingClient {
302302
stub => (...args: Array<{}>) => {
303303
if (this._terminated) {
304304
if (methodName in this.descriptors.stream) {
305-
const stream = new PassThrough();
305+
const stream = new PassThrough({objectMode: true});
306306
setImmediate(() => {
307307
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
308308
});

core/generator/gapic-generator-typescript/baselines/logging-esm/esm/src/v2/logging_service_v2_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export class LoggingServiceV2Client {
369369
stub => (...args: Array<{}>) => {
370370
if (this._terminated) {
371371
if (methodName in this.descriptors.stream) {
372-
const stream = new PassThrough();
372+
const stream = new PassThrough({objectMode: true});
373373
setImmediate(() => {
374374
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
375375
});

core/generator/gapic-generator-typescript/baselines/logging/src/v2/logging_service_v2_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export class LoggingServiceV2Client {
346346
stub => (...args: Array<{}>) => {
347347
if (this._terminated) {
348348
if (methodName in this.descriptors.stream) {
349-
const stream = new PassThrough();
349+
const stream = new PassThrough({objectMode: true});
350350
setImmediate(() => {
351351
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
352352
});

core/generator/gapic-generator-typescript/baselines/showcase-esm/esm/src/v1beta1/echo_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export class EchoClient {
332332
stub => (...args: Array<{}>) => {
333333
if (this._terminated) {
334334
if (methodName in this.descriptors.stream) {
335-
const stream = new PassThrough();
335+
const stream = new PassThrough({objectMode: true});
336336
setImmediate(() => {
337337
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
338338
});

core/generator/gapic-generator-typescript/baselines/showcase-esm/esm/src/v1beta1/messaging_client.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class MessagingClient {
331331
stub => (...args: Array<{}>) => {
332332
if (this._terminated) {
333333
if (methodName in this.descriptors.stream) {
334-
const stream = new PassThrough();
334+
const stream = new PassThrough({objectMode: true});
335335
setImmediate(() => {
336336
stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.'));
337337
});

0 commit comments

Comments
 (0)