Skip to content

Fix producer memory leak when unable to dispose - #378

Merged
Lanayx merged 5 commits into
fsprojects:developfrom
otwl169:hotfix/timer-memory-leak
Oct 9, 2026
Merged

Lanayx merged 5 commits into
fsprojects:developfrom
otwl169:hotfix/timer-memory-leak

Conversation

@otwl169

@otwl169 otwl169 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Problem

While disposing a producer, the client sends CloseProducer to the broker. If that request fails because the connection drops or the broker does not respond, the dispose throws. When it throws, the batch timer continues posting into the stopped mailbox indefinitely and the producer remains registered on the connection.

With a default batch timer of 1ms, this will mean we add a Tick to the mailbox every 1ms, which AI suggests is 110 MB an hour, but I haven't double checked that.

What changed

I moved ClientCnx behind an interface to allow me to write the unit tests on the ProducerImpl for this behaviour. The only production logic changes to the ProducerImpl are on lines 885 and 886 of that file. The change is that a failed close now removes the producer from the connection and stops the producer, which stops the timer.

Successful close behavior is unchanged, allowing an in-flight batch to flush before the broker acknowledges the close.

What I didn't change

I noticed that the inflight messages don't fail in this path, which makes me think anyone waiting to publish a message will be hanging indefinitely. If that is true, I think it should be fixed as well.

Testing plan

Added test for new behaviour that we stop the timer
Added test for new behaviour that the producer is removed from the connection

otwl169 and others added 5 commits October 8, 2026 15:25
Keep the failed-close fix on current develop without propagating a connection interface. Exercise a concrete ClientCnx over in-memory pipes, stop every producer timer, and fail pending sends and flushes when close fails.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Lanayx
Lanayx changed the base branch from develop to master October 9, 2026 17:18
@Lanayx
Lanayx changed the base branch from master to develop October 9, 2026 17:18
@Lanayx
Lanayx merged commit bb6c5c0 into fsprojects:develop Oct 9, 2026
1 of 3 checks passed
@otwl169

otwl169 commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

@Lanayx is there a specific reasoning to not put ClientCnx behind an interface?

@Lanayx

Lanayx commented Oct 11, 2026

Copy link
Copy Markdown
Member

@otwl169 Not really, I just didn't find enough justification of introducing the interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants