nodejs-speech icon indicating copy to clipboard operation
nodejs-speech copied to clipboard

Unhandled 'error' event crash

Open jmillan opened this issue 8 years ago • 71 comments

Environment details

  • OS: Debian 8.10
  • Node.js version: v8.10.0
  • npm version: 5.6.0
  • @google-cloud/speech version: 1.4.0
Target

Get continuous transcriptions from an audio stream which length is undefined.

NOTE: I am aware of the quotas and limits for the speech recognition service.

Observations

As shown in the shared code, the streamingRecognize() write steam is re-generated on every data event which reports an error (typically being: exceeded maximum allowed stream duration of 65 seconds).

After some time (usually less than 5 minutes) the following unhandled exception is thrown which stops the application completely:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: 14 UNAVAILABLE: 502:Bad Gateway
    at createStatusError (/service/node_modules/grpc/src/client.js:64:15)
    at ClientDuplexStream._emitStatusIfDone (/service/node_modules/grpc/src/client.js:270:19)
    at ClientDuplexStream._receiveStatus (/service/node_modules/grpc/src/client.js:248:8)
    at /service/node_modules/grpc/src/client.js:804:12

The logs clearly point to grpc.

Questions/Concerns

My main question is: Is it actually possible to achieve continuous transcriptions of undefined audio lengths by using StreamingRecognize or any other ways provided by this service?

If there is a way to achieve this with StreamingRecognize.How can the exposed error be avoided, or achieved in any other way?

Thanks.

Code that reproduces the crash

const speech = require('@google-cloud/speech');

class GoogleSpeech
{
	constructor({ languageCode = 'en-US' })
	{
		logger.debug('constructor()');

		// Google Speech client.
		this._client = new speech.SpeechClient();

		// Google Speech configuration request.
		this._request =
		{
			config : {
				encoding              : 'LINEAR16',
				sampleRateHertz       : 16000,
				enableWordTimeOffsets : true,
				languageCode
			},
			// 'true' to perform continuous recognition even if the user pauses speaking.
			singleUtterance : false,
			// 'true' to enable tentative hypoteses.
			interimResults  : true
		};

		// Plain audio readable stream.
		this._audioStream = null;
	}

	/**
	 * @param {Readable} audioStream
	 */
	start(audioStream)
	{
		logger.debug('start()');

		this._audioStream = audioStream;

		this._start();
	}

	stop()
	{
		logger.debug('stop()');
	}

	_start()
	{
		logger.debug('_start()');

		try
		{
			// Create a writable stream to which pipe the plain audio.
			this._recognizeStream = this._client.streamingRecognize(this._request);
		}
		catch (error)
		{
			logger.error('streamingRecognize() error: [%s]', error.message);

			return;
		}

		this._recognizeStream
			.on('error', (error) =>
			{
				logger.error('streamingRecognize() "error" event [%s]', error.message);
				this._audioStream.unpipe(this._recognizeStream);
			})
			.on('data', (data) =>
			{
				if (data.error)
					logger.error('streamingRecognize() "data" event error [%s]', data.error);

				else
					logger.debug(data.results[0].alternatives[0].transcript);
			})
			.on('unpipe', () =>
			{
				delete this._recognizeStream;

				this._start();
			});

		// Pipe the audio stream into the Speech API.
		this._audioStream.pipe(this._recognizeStream);
	}
}

jmillan avatar Apr 24 '18 10:04 jmillan

Hi @jmillan

I was also facing this issue and cause of that my application get crashed.

I just get fixed in tricky way Create new recognition object after every 50 sec and unpipe previous one And use try cache while creating new object of recognition and in cache recall init method like below code.

var recognizeStream;
var initSpeechToText = function(){
		try
		{
			return self.speech_to_text.streamingRecognize(self.recognizeStreamOpt)
				.on('error', (e)=>onError)
				.on('data', data => onData)	
		}
		catch (e)
		{
			console.log("ERROR BIG", e);
			return initSpeechToText();
		}
}

var startStt = function(flag){
	if (flag)
	{
		bufferStream.unpipe(recognizeStream);
		console.log("re init after 60 sec");
	}
	
	recognizeStream = initSpeechToText();
	bufferStream.pipe(recognizeStream);
}

startStt();
sstInterval = setInterval(() => { startStt(true) } , 60000);

will fix bad gateway 502 error.

niravpatel2008 avatar Apr 24 '18 10:04 niravpatel2008

Hi @niravpatel2008,

I just get fixed in tricky way Create new recolonization object after every 50 sec

I guess you mean recognition when saying recolonization. Do you refer to the write stream returned by streamingRecognize()?

and unpipe previous one will fix bad gateway 502 error.

Let me try to understand what you say. Lets consider that we already have a read stream piped to a write steam and we are getting the transcripts:

  1. Create new write stream out of streamingRecognize()
  2. Unpipe read stream from current write stream.
  3. Once unpiped, pipe read stream with new write stream, which is from now on the current one.
  4. Wait 50 seconds
  5. Goto 1

jmillan avatar Apr 24 '18 11:04 jmillan

@jmillan

Sorry its my mistake due to auto correct(its recognition not recolonization).

Yes you are on right track please implement your steps and hope it will also works for you.

niravpatel2008 avatar Apr 24 '18 11:04 niravpatel2008

Already testing,

Thanks for you comments @niravpatel2008

jmillan avatar Apr 24 '18 11:04 jmillan

@jmillan

Please have look at update comment

niravpatel2008 avatar Apr 24 '18 12:04 niravpatel2008

Sill experiencing the crash within this code:

	_start()
	{
		logger.debug('_start()');

		this._recognizeStream = this._createRecognizeStream();

		// Pipe the audio stream into the Speech API.
		this._audioStream.pipe(this._recognizeStream);

		// Substitute current recognizeStream with a new one before timeout expiration.
		setTimeout(() =>
		{
			this._audioStream.unpipe(this._recognizeStream);
		}, 50 * 1000);
	}

	_createRecognizeStream()
	{
		logger.debug('_createRecognizeStream()');

		const recognizeStream = this._client.streamingRecognize(this._request);

		recognizeStream
			.on('error', (error) =>
			{
				logger.error('streamingRecognize() "error" event [%s]', error.message);
			})
			.on('data', (data) =>
			{
				if (data.error)
					logger.error('streamingRecognize() "data" event error [%s]', data.error);

				else
					logger.error(data.results[0].alternatives[0].transcript);
			})
			.on('unpipe', () =>
			{
				logger.debug('unpiped, ending old recognizeStream');

				recognizeStream.end();

				this._start();
			});

		return recognizeStream;
	}

After some time:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: 14 UNAVAILABLE: 502:Bad Gateway
    at createStatusError (/service/node_modules/grpc/src/client.js:64:15)
    at ClientDuplexStream._emitStatusIfDone (/service/node_modules/grpc/src/client.js:270:19)
    at ClientDuplexStream._receiveStatus (/service/node_modules/grpc/src/client.js:248:8)
    at /service/node_modules/grpc/src/client.js:804:12

jmillan avatar Apr 24 '18 12:04 jmillan

Please have look at update comment

I just have seen this comment. Checking right now.

jmillan avatar Apr 24 '18 12:04 jmillan

Not even with the minimum code possible

	_start()
	{
		logger.debug('_start()');

		this._recognizeStream = this._createRecognizeStream();

		// Pipe the audio stream into the Speech API.
		this._audioStream.pipe(this._recognizeStream);

		// Substitute current recognizeStream with a new one before timeout expiration.
		setTimeout(() =>
		{
			this._audioStream.unpipe(this._recognizeStream);

			this._start();
		}, 60 * 1000);
	}

	_createRecognizeStream()
	{
		logger.debug('_createRecognizeStream()');

		const recognizeStream = this._client.streamingRecognize(this._request);

		recognizeStream
			.on('error', (error) =>
			{
				logger.error('streamingRecognize() "error" event [%s]', error.message);
			})
			.on('data', (data) =>
			{
				if (data.error)
					logger.error('streamingRecognize() "data" event error [%s]', data.error);

				else
					logger.error(data.results[0].alternatives[0].transcript);
			});

		return recognizeStream;
	}

The issue must be somewhere else releated to grpc itself more than within the use of the speech library.

jmillan avatar Apr 24 '18 13:04 jmillan

@jmillan Please add try cache like below. 502 Bad Gateway is generic error code here is the link for [causes] (https://www.lifewire.com/502-bad-gateway-error-explained-2622939).

So just to fix this issue lets add try catch block in your code which will fix this issue. until grpc lib dev fix in library.

_start()
{
	logger.debug('_start()');

	this._recognizeStream = this._createRecognizeStream();

	// Pipe the audio stream into the Speech API.
	this._audioStream.pipe(this._recognizeStream);

	// Substitute current recognizeStream with a new one before timeout expiration.
	setTimeout(() =>
	{
		this._audioStream.unpipe(this._recognizeStream);

		this._start();
	}, 60 * 1000);
}

_createRecognizeStream()
{
	try
	{
		logger.debug('_createRecognizeStream()');
		
		const recognizeStream = this._client.streamingRecognize(this._request);

		recognizeStream
			.on('error', (error) =>
			{
				logger.error('streamingRecognize() "error" event [%s]', error.message);
			})
			.on('data', (data) =>
			{
				if (data.error)
					logger.error('streamingRecognize() "data" event error [%s]', data.error);

				else
					logger.error(data.results[0].alternatives[0].transcript);
			});

		return recognizeStream;
	}
	catch(e)
	{
		return this._createRecognizeStream();
	}
}

niravpatel2008 avatar Apr 24 '18 13:04 niravpatel2008

I used to have try/catch there originally (before opening this issue) and I was getting the error. Anyway I've just put it and test is ongoing.

Thanks. Will update here.

jmillan avatar Apr 24 '18 13:04 jmillan

Exception happened again.

There's no alternative left but going down to grpc.

jmillan avatar Apr 24 '18 14:04 jmillan

In case this brings some light to someone who knows about grpc:

This is result of setting an uncaughtException event handler in the process:

  uncaughtException:  { Error: 14 UNAVAILABLE: 502:Bad Gateway
    at createStatusError (/service/node_modules/grpc/src/client.js:64:15)
    at ClientDuplexStream._emitStatusIfDone (/service/node_modules/grpc/src/client.js:270:19)
    at ClientDuplexStream._receiveStatus (/service/node_modules/grpc/src/client.js:248:8)
    at /service/node_modules/grpc/src/client.js:804:12
  code: 14,
  metadata:
   Metadata {
     _internal_repr: { 'content-length': [Array], date: [Array], 'alt-svc': [Array] } },
  details: '502:Bad Gateway' } +680ms

  uncaughtException metadata:  Metadata {
  _internal_repr:
   { 'content-length': [ '0' ],
     date: [ 'Tue, 24 Apr 2018 15:29:41 GMT' ],
     'alt-svc':
      [ 'hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35"' ] } } +15ms

jmillan avatar Apr 24 '18 15:04 jmillan

Reproducible

The 5th time it creates a new write stream via streamingRecognize() it crashes.

jmillan avatar Apr 24 '18 16:04 jmillan

@jmillan i was also facing this issue but by changing in flow as i mentioned above try catch block and timer it fixed for me least its working for me.

niravpatel2008 avatar Apr 25 '18 04:04 niravpatel2008

@niravpatel2008 I've just tried your try/catch fix and it also didn't work for me.

marcelgoya avatar Apr 25 '18 12:04 marcelgoya

For the folks from Google, is there any way to catch this exception or does it need to be fixed inside the library?

marcelgoya avatar Apr 25 '18 12:04 marcelgoya

The limits docs clearly state that there is a maximum streaming request time of 5 minutes, and that requests and/or attempts at audio processing in excess of these limits will produce an error.

Could anyone please confirm that what we are trying to achieve is currently not doable with google-speech?

jmillan avatar Apr 26 '18 14:04 jmillan

niravpatel2008, could you please share some details of your installation?

  • OS
  • Node.js version
  • npm version
  • @google-cloud/speech version
  • Parameters provided to streamRecognize()
  • Audio input type

Thanks in advance.

jmillan avatar Apr 26 '18 18:04 jmillan

@jmillan Sure below are the installation details.

OS : centos 6.5 Node.js version : v6.12.0 npm version: 5.5.1 @google-cloud/speech version : ^1.1.0 Parameters provided to streamRecognize() :

{
			config: {
			  encoding: "LINEAR16",
			  sampleRateHertz: "16000",
			  languageCode: "en-US",
			  profanity_filter: true,
			  model: "phone_call"
			},
			interimResults: true,
		}

Audio input type: wav (16k rate)

niravpatel2008 avatar Apr 27 '18 04:04 niravpatel2008

Thanks @niravpatel2008,

How large is the wav file? Does it exceed 5 or even 10 minutes?

jmillan avatar Apr 27 '18 05:04 jmillan