Skip to content

Commit

Permalink
unregister sub from subMap when subscribe fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Radu Popovici committed Jul 29, 2022
1 parent cfc73f3 commit 40e60a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions stan_test.go
Expand Up @@ -2815,6 +2815,10 @@ func TestSubTimeout(t *testing.T) {
if req.ClientID != clientName || req.Subject != "foo" || req.Inbox == "" {
t.Fatalf("Unexpected sub close request: %+v", req)
}

if len(scc.subMap) > 0 {
t.Fatal("Expected subMap to be empty")
}
}

func TestSubCloseError(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions sub.go
Expand Up @@ -262,6 +262,9 @@ func (sc *conn) subscribe(subject, qgroup string, cb MsgHandler, options ...Subs
// Listen for actual messages.
nsub, err := sc.nc.Subscribe(sub.inbox, sc.processMsg)
if err != nil {
sc.Lock()
delete(sc.subMap, sub.inbox)
sc.Unlock()
return nil, err
}
nsub.SetPendingLimits(-1, -1)
Expand Down Expand Up @@ -312,6 +315,9 @@ func (sc *conn) subscribe(subject, qgroup string, cb MsgHandler, options ...Subs
// Report this error to the user.
err = ErrSubReqTimeout
}
sc.Lock()
delete(sc.subMap, sub.inbox)
sc.Unlock()
return nil, err
}
r := &pb.SubscriptionResponse{}
Expand Down

0 comments on commit 40e60a9

Please sign in to comment.