Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Prevent memory leak on subscription failure #366

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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