fix(router): resolve memory leak from uncancelled contexts in async mode#866
Merged
appleboy merged 2 commits intoappleboy:masterfrom Mar 21, 2026
Merged
Conversation
In pushHandler, context.WithCancel(context.Background()) was called on every request, but cancel() was only invoked in sync mode. In async mode (the common case), the context was never cancelled, causing it to persist in Go's internal context tree for the lifetime of the process. Each request also leaked a monitoring goroutine blocked on <-c.Request.Context().Done(). Replace with c.Request.Context() which is automatically managed by net/http and cleaned up when the request completes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Owner
|
I will take it. |
appleboy
approved these changes
Mar 21, 2026
appleboy
added a commit
that referenced
this pull request
Mar 21, 2026
Previously, handleNotification accepted a context.Context parameter but ignored it (_ context.Context). The context passed from pushHandler (c.Request.Context()) was therefore never used when dispatching notifications, so client disconnection could not cancel in-flight push tasks. This commit: 1. Renames the ignored parameter to `ctx` so it is actually used. 2. Adds a withEitherCancel helper that merges the HTTP request context with the queue-task context, cancelling when either is done (client disconnects OR queue shuts down). 3. Threads the merged context through to notify.SendNotification, which already propagates it to PushToIOS / PushToAndroid / PushToHuawei and DispatchFeedback. This completes the fix originally intended by the goroutine pattern in issue #422, which was removed in #866 because the cancel() call was never reached in async mode.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pushHandlercaused bycontext.WithCancel(context.Background())wherecancel()is never called in async mode (sync: false)c.Request.Context()whose lifecycle is managed by net/http automaticallyRoot Cause
In
pushHandler, every incoming request creates a new context viacontext.WithCancel(context.Background())and spawns a goroutine that only callscancel()whencfg.Core.Syncis true. In async mode (the default and most common configuration),cancel()is never invoked, so:context.Background()forever<-c.Request.Context().Done()until the HTTP connection closes, then exits without cancellingOver time this causes unbounded memory growth (~1 KB per request). At moderate traffic this reaches GiBs within days.
Fix
Replace the
context.WithCancel+ goroutine pattern withc.Request.Context()directly. This is safe becausehandleNotificationalready ignores the context parameter (_ context.Context), so the behavioral change is zero — we're only removing the leak.Evidence
Production memory graph showing continuous growth over 7 days with gorush v1.18.4:
Memory climbs from ~100 MiB to ~1.9 GiB without ever being reclaimed, a classic leak pattern.
Test plan
go build -tags sqlite ./...compiles successfully🤖 Generated with Claude Code