Skip to content

Commit

Permalink
Merge pull request #366 from oncicaradupopovici/main
Browse files Browse the repository at this point in the history
[FIXED] Prevent memory leak on subscription failure
  • Loading branch information
kozlovic authored Jul 29, 2022
2 parents cfc73f3 + 3e2002b commit 64446ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions stan_test.go
Original file line number Diff line number Diff line change
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
13 changes: 13 additions & 0 deletions sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ func (sc *conn) subscribe(subject, qgroup string, cb MsgHandler, options ...Subs
sc.subMap[sub.inbox] = sub
sc.Unlock()

doClean := true
defer func() {
if doClean {
sc.Lock()
//Un-register subscription.
delete(sc.subMap, sub.inbox)
sc.Unlock()
}
}()

// Hold lock throughout.
sub.Lock()
defer sub.Unlock()
Expand Down Expand Up @@ -325,6 +335,9 @@ func (sc *conn) subscribe(subject, qgroup string, cb MsgHandler, options ...Subs
}
sub.ackInbox = r.AckInbox

// Prevent cleanup on exit.
doClean = false

return sub, nil
}

Expand Down

0 comments on commit 64446ca

Please sign in to comment.