diff --git a/nats.go b/nats.go index 9317227ad..10ad16967 100644 --- a/nats.go +++ b/nats.go @@ -3932,7 +3932,8 @@ func (jsi *jsSub) trackSequences(msg *Msg) { // NextMsg will return the next message available to a synchronous subscriber // or block until one is available. An error is returned if the subscription is invalid (ErrBadSubscription), -// the connection is closed (ErrConnectionClosed), or the timeout is reached (ErrTimeout). +// the connection is closed (ErrConnectionClosed), the timeout is reached (ErrTimeout), +// or if there were no responders (ErrNoResponders) when used in the context of a request/reply. func (s *Subscription) NextMsg(timeout time.Duration) (*Msg, error) { if s == nil { return nil, ErrBadSubscription @@ -4053,6 +4054,9 @@ func (s *Subscription) processNextMsgDelivered(msg *Msg) error { nc.mu.Unlock() } } + if len(msg.Data) == 0 && msg.Header.Get(statusHdr) == noResponders { + return ErrNoResponders + } return nil } diff --git a/test/basic_test.go b/test/basic_test.go index f5ecf6c72..f39c8f150 100644 --- a/test/basic_test.go +++ b/test/basic_test.go @@ -623,6 +623,20 @@ func TestBasicNoRespondersSupport(t *testing.T) { if m, err := nc.RequestWithContext(ctx, "foo", nil); err != nats.ErrNoResponders { t.Fatalf("Expected a no responders error and nil msg, got m:%+v and err: %v", m, err) } + + // SubscribeSync + inbox := nats.NewInbox() + sub, err := nc.SubscribeSync(inbox) + if err != nil { + t.Fatal(err) + } + err = nc.PublishRequest("foo", inbox, nil) + if err != nil { + t.Fatal(err) + } + if m, err := sub.NextMsg(2 * time.Second); err != nats.ErrNoResponders { + t.Fatalf("Expected a no responders error and nil msg, got m:%+v and err: %v", m, err) + } } func TestOldRequest(t *testing.T) {