5

I'm encountering a RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds error while working with Google Cloud Pub/Sub in a Node.js application. Below is a simplified version of the service I'm using:

@Controller() export class EventController { private subscription; @Get('subscribe') public subscribe(): void { console.log('Subscribing to event'); this.stringSubscriber.subscribe(); console.log('Subscribed to event'); } private stringSubscriber = { subscribe: (): void => { try { this.subscription.on('message', async (message: Message): Promise<void> => { logger.info('[subscribe] [onMessage] message', { message: message.data.toString(), deliveryAttempt: message.deliveryAttempt, }); await this.handleEvent({ message }); }); } catch (error) { logger.error('[subscribe] Error while subscribing', { error }); throw error; } } }; private async handleEvent(event: { message: Message }): Promise<void> { .... } } 

it seems to fail when I send a subscribe request to the pod, however it does print the logs before:

Subscribing to event.

Subscribed to event.

node:events:498 throw er; // Unhandled 'error' event ^ RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: "length" is outside of buffer bounds at proto.utf8Write (node:internal/buffer:1066:13) at Op.writeStringBuffer [as fn] (file:///usr/app/apps/demo5/dist/dummy.js:38758:13) at BufferWriter.finish (file:///usr/app/apps/demo5/dist/dummy.js:38708:14) at file:///usr/app/apps/demo5/dist/dummy.js:45199:113 at Array.map (<anonymous>) at createPackageDefinition (file:///usr/app/apps/demo5/dist/dummy.js:45199:41) at Object.fromJSON (file:///usr/app/apps/demo5/dist/dummy.js:45226:14) at _GrpcClient.loadProtoJSON (file:///usr/app/apps/demo5/dist/dummy.js:68212:51) at new IamClient (file:///usr/app/apps/demo5/dist/dummy.js:84620:37) at new SubscriberClient (file:///usr/app/apps/demo5/dist/dummy.js:88646:26) * Ensured that the subscription is properly created and initialized. * Verified that the message handling logic is correct and does not introduce any buffer issues. 
6
  • It appears that your app is bundled for deployment (dist/dummy.js) which makes the stacktrace less helpful. Can you help identify what data the code is attempting to use with _GrpcClient.loadProtoJSON (dummy.js line 68212) and/or Object.fromJSON (dummy.js line 42226)? It certainly seems as though this is an error in the Google library. Please include package.json too. Commented Aug 25, 2024 at 15:47
  • Someone else albeit using a different Google Cloud Node.js SDK is experiencing a similar issue (see 78911398). Commented Aug 25, 2024 at 15:52
  • I am unable to repro your error using the latest (4.6.0) version of `@google-cloud/pubsub Commented Aug 25, 2024 at 17:10
  • Thanks for the quick response! actually regarding the data, I don't even manage to get there... it fails after it has a subscribe request. Commented Aug 25, 2024 at 17:31
  • 2
    WORKED! it seems to be a node version issue, I was running 22.7.0, changed it to 22.5.0 and now it's working, pretty spooky. Commented Aug 25, 2024 at 18:07

3 Answers 3

7

This is probably an error with your Node version. If you (or your deployment) is running on the latest version there appears to be a conflict with the GSM library.

Similar issue

[Issue documented on Github] (https://github.com/nodejs/node/issues/54518#issuecomment-2307687124)

Specify a lower version for node and should be good to go. The error will be fixed in a newer version of the library.

Sign up to request clarification or add additional context in comments.

3 Comments

Per Node's GitHub github.com/nodejs/node/issues/54518#issuecomment-2307687124: "It should be fixed in next week's release (22.8.0) which includes: #54524 as a patch. I will take care of it."
I can confirm using node v22.8.0 resolved this issue.
Issue may have reappeared in node v 23.x. Issue remains open at github.com/nodejs/node/issues/54518. Sticking with LTS (e.g. 22.x) seems to offer a solution.
4

try to update to last node version, in my case 22.8 works like a charm

1 Comment

Can confirm this!
0

I was facing same issue with node v23 Had to downgrade to node v20.18.1 and now it works just fine.

1 Comment

same... this issue came up with Node v23.6.0 just suddenly... working on a gatsby react typescript project if that helps anyone

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.