-
Notifications
You must be signed in to change notification settings - Fork 530
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
Ensure unsubscribing only removes a single subscription #1315
base: main
Are you sure you want to change the base?
Ensure unsubscribing only removes a single subscription #1315
Conversation
Hey @tomasz-tomczyk thanks or the PR. This is an area of the code base I always have to take time to re-remember every time I look at it :D. Can you add some tests that verify a few things:
Do make sure to check the whole registry via |
Thanks for your feedback!
So with my test setup, they're using the same topic, just with added args that don't distinguish it as different topics. I've added a test that confirms the unsubscribed topic did not receive a message in e64a6fa but:
I've added tests in 55039ae but they're actually failing - the registry does grow at the moment, whether that piece of code in for field_key <- pdict_fields(doc_id) do
Registry.unregister(registry, field_key)
end Assertion failing: code: assert [] == PubSub.list_registry_keys()
left: []
right: [
"__absinthe__:doc:-576460752303319933:439A5CADA422CD766FA8845A25B8BBBB5C97F523C30BEF654B0AB3030E802FDC",
"__absinthe__:doc:-576460752303320253:439A5CADA422CD766FA8845A25B8BBBB5C97F523C30BEF654B0AB3030E802FDC",
"__absinthe__:doc:-576460752303320573:439A5CADA422CD766FA8845A25B8BBBB5C97F523C30BEF654B0AB3030E802FDC"
] Presumably, that's a bug with the original implementation too? Unless there's a reason these keys are kept? Happy to try to fix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have encountered this same bug, and implemented these changes to correct it.
@tomasz-tomczyk it'd be great if we could get these fixes into your PR and then get the issue fixed for good!
lib/absinthe/subscription.ex
Outdated
for field_key <- pdict_fields(doc_id) do | ||
Registry.unregister(registry, field_key) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still want to clean up the record in the registry, but this only removes the entry associated with the doc_id
and not all subscriptions with the same field_key
for field_key <- pdict_fields(doc_id) do | |
Registry.unregister(registry, field_key) | |
end | |
for field_key <- pdict_fields(doc_id) do | |
Registry.unregister_match(registry, field_key, doc_id) | |
end |
@@ -77,6 +77,10 @@ defmodule Absinthe.Execution.SubscriptionTest do | |||
# this pubsub is local and doesn't support clusters | |||
:ok | |||
end | |||
|
|||
def list_registry_keys() do | |||
Registry.keys(__MODULE__, self()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the registry name that is used to store the subscription entries internally
Registry.keys(__MODULE__, self()) | |
Registry.keys(__MODULE__.Registry, self()) |
Thanks @howleysv! That was very helpful. @benwilson512 all tests pass now. Any other suggestions? 🙏 ❤️ |
@tomasz-tomczyk Looks like the same fix was merged to main (unreleased) 6 months ago in #1336 and about to ship in 1.7.9 |
In our scheduling/calendar app, we subscribe for events in a given location, but we allow you to pass in a date filter, which we use in a custom resolver which allows us to limit what you receive to just the date you're looking. It looks something like this:
As you navigate between days in the calendar, our React state seems to start the new subscription before unsubscribing from the previous one.
The code added/modified here seems to then cause the Registry to get rid of both subscriptions and no more events are sent to the frontend.
I confirmed that by logging what is in the Registry at each step:
Registry keys after the first subscription:
Registry keys after the second subscription:
Registry keys after unsubscribing once:
This looks like an invalid subscription since it no longer has the topic definition.
Just removing those 3 lines fixes the issues and I managed to replicate it in a test (omitted the custom resolver we have since it seems unnecessary for this example) and seemingly everything else passes, but I'm not entirely sure what the original code wanted to achieve.
Appreciate any feedback and guidance!