nats-io / nats.net

The official C# Client for NATS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error after upgrade to 1.1.0 from 1.0.8

Kaarel opened this issue · comments

Observed behavior

Our existing applications started throwing exceptions when subscribing to streams.

Expected behavior

Everything should continue to work after upgrade.

Server and client version

Server: 2.10.5
Client (working): 1.0.8
Client (not working): 1.1.0 and 1.1.1

Host environment

No response

Steps to reproduce

  1. Precondition: Run NATS server eg nats-server --jetstream

  2. Create a new C# console project (net8.0) and replace Program.cs generated by the template with below code:

using NATS.Client;
using NATS.Client.JetStream;

Console.WriteLine("Hello, World!");

var con = new ConnectionFactory().CreateConnection();
var streamName = "stream-name";
var consumerName = "consumer-name";
var subject = "my-subject";

var jsm = con.CreateJetStreamManagementContext();
try { jsm.DeleteStream(streamName); } catch { }

jsm.AddStream(StreamConfiguration.Builder()
    .WithName(streamName)
    .WithSubjects(subject)
    .Build());

var consumerConfig = ConsumerConfiguration.Builder()
    .WithDurable(consumerName)
    .WithDeliverSubject($"{consumerName}.{streamName}")
    .WithDeliverGroup(consumerName)
    //.WithFilterSubject(subject) // <------------- Uncomment to make it also work in 1.1.0
    .Build();
jsm.AddOrUpdateConsumer(streamName, consumerConfig);

var options = PushSubscribeOptions.BindTo(streamName, consumerName);
try
{
    con.CreateJetStreamContext().PushSubscribeAsync(
        subject,
        consumerName,
        (sender, args) => { },
        false,
        options);
}
catch (Exception ex)
{
    // Works in NATS.Client 1.0.8
    // THROWS in NATS.Client 1.1.0 and 1.1.1
    // [SUB-90011] Subject does not match consumer configuration filter.
    Console.WriteLine(ex);
}
  1. Press F5

I get the fix is easy (add .WithFilterSubject() to consumer config). But why is it even necessary if everything was working before?

@Kaarel You said "Our existing applications started throwing exceptions when subscribing to streams." This is probably 90011, "Subject does not match consumer configuration filter"

Let's say you have a stream subject foo.> And you are subscribing to foo.a. When you don't supply a filter subject on a consumer, it becomes >, which means all subjects.

So this is a problem, because you think you are subscribing to foo.a but in reality, without this check, you will be getting all messages from all subjects.

So validating the subscribe subject against the filter subject needed to be added and unfortunately, this makes existing code throw exceptions, but I think that is important to do to make developers aware of the issue. I will make sure to add something to the readme.