nats.js: Permissions Violation during subscribe unexpectedly closes the whole connection

  • Client version: 2.8.0
  • Node version: 16.14.2

What’s happening

If in subscription iterator for await (const message of subscription) happens Permissions Violation error like this

NatsError: 'Permissions Violation for Subscription to "subject"'
    at Function.toError (/code/node_modules/nats/lib/nats-base-client/protocol.js:306:20)
    at ProtocolHandler.<anonymous> (/code/node_modules/nats/lib/nats-base-client/protocol.js:339:41)
    at Generator.next (<anonymous>)
    at /code/node_modules/nats/lib/nats-base-client/protocol.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/code/node_modules/nats/lib/nats-base-client/protocol.js:4:12)
    at ProtocolHandler.processError (/code/node_modules/nats/lib/nats-base-client/protocol.js:337:16)
    at ProtocolHandler.push (/code/node_modules/nats/lib/nats-base-client/protocol.js:426:22)
    at Parser.parse (/code/node_modules/nats/lib/nats-base-client/parser.js:312:45)
    at ProtocolHandler.<anonymous> (/code/node_modules/nats/lib/nats-base-client/protocol.js:189:45)

it will close the whole NATS connection. Here’s what connection.closed() will yield (same error):

NatsError: 'Permissions Violation for Subscription to "subject"'
    at Function.toError (/code/node_modules/nats/lib/nats-base-client/protocol.js:306:20)
    at ProtocolHandler.<anonymous> (/code/node_modules/nats/lib/nats-base-client/protocol.js:339:41)
    at Generator.next (<anonymous>)
    at /code/node_modules/nats/lib/nats-base-client/protocol.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/code/node_modules/nats/lib/nats-base-client/protocol.js:4:12)
    at ProtocolHandler.processError (/code/node_modules/nats/lib/nats-base-client/protocol.js:337:16)
    at ProtocolHandler.push (/code/node_modules/nats/lib/nats-base-client/protocol.js:426:22)
    at Parser.parse (/code/node_modules/nats/lib/nats-base-client/parser.js:312:45)
    at ProtocolHandler.<anonymous> (/code/node_modules/nats/lib/nats-base-client/protocol.js:189:45)

What’s expected

I think closing the whole connection is very unexpected behavior here. While clearly, this is usually a human error when setting up NATS permissions, the fact that some subscriptions failed to doesn’t mean that the whole connection should be killed because other subs are still able to work. It should be up to the application to decide what to do in such situations.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (13 by maintainers)

Most upvoted comments

Yeap, the issue is present only in v2.8.0.

With 2.8.1-2 I’m unable to reproduce it, so we’re good to go 👍

It might be, but it’s always closing after an attempt to subscribe to the subject without permission. It’s 100% reproducible.

Sorry, I’ve put the wrong version number. We’re using 2.8.1-2 which was introduced for nats-io/nats.js#524

Going to put the same test in node, perhaps something is not quite right there.

@aricart

const options = {
  maxReconnectAttempts: -1,
  name: 'service-name', // always static
  pass: 'pass', // always static
  servers: ['some-server-example.com:4333'], // always same URL here, leads to nats-operator behind the balancer
  user: 'user', // always static
  inboxPrefix: 'service-name.inbox', // always static
}