This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
fix: Memory leak when using reactive subscriptions with KeepAlive #642
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.
Hi !
This PR should fix a memory leak when using webflux with subscriptions and KeepAlive.
Explanation
ApolloSubscriptionKeepAliveRunner is responsible for sending keep alive messages through the websocket as long as the websocketSubscriptionSession is "opened". This runner, uses a scheduled Executor for this purpose.
When a subscription is closed, of an exception occurs while sending message, KeepAlive message is not more scheduled and everything works fine.
Bug :
ReactiveWebSocketSubscriptionSession is the object representing a Reactive Subscription, this object always return
true
on theisOpen
method, the keepAliveRunner will never stop sending messages, and stack references to dead subscriptions (Memory leak).No exception occurs while sending messages on a closed subscription, because we are just publishing to a Flux without subscribers. See ReactiveWebSocketSubscriptionsHandler.
A memory leak is also present on
SingleSubscriberPublisher
in graphql-java project as we keep adding data to theConcurrentLinkedDeque
even if there is no subscribers.Fix :
ReactiveWebSocketSubscriptionSession is now catching the call to
close
method to update the internal state of the websocket.