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

Warn user when calling subscribe outside of a reactive context #752

Merged
merged 1 commit into from
Mar 4, 2022

Conversation

dannyfreeman
Copy link
Contributor

@dannyfreeman dannyfreeman commented Feb 7, 2022

I saw the help wanted tag on issue #740 and decided to open a PR for it

This will log warnings to the console every time a call to subscribe is made in a place it shouldn't be called, like event handlers.

Here is what the warning looks like in firefox:
image

Right now it is printed as a warning. It may be more useful to print it as an error so the user can see the stack trace and find what code triggered the message.

Calling subscribe outside of a reactive context will cache the
subscription, but there will be no ratoms around to remove the
subscription from the cache when the ratom is disposed. This can lead
to memory leaks where subscriptions stay in the cache indefinately.

This should fix issue day8#740
@dannyfreeman
Copy link
Contributor Author

dannyfreeman commented Feb 8, 2022

I put this in the issue, but wanted to drop it here too. It may be worth figuring out how to change re-frame such that this warning is not necessary.

I made this branch a while back to explore what seems to be a reasonable solution to the problem:
master...dannyfreeman:bypass-subscription-cache-outside-reactive-context

In short, when the current call to subscribe is not happening in a reactive context and a subscription is not already cached, then bypass the subscription cache. Just return a new reaction. If the subscription was already cached then any code can grab and derefence that cached subscription (which is now only cached in reactive contexts, so something will eventually clean it up).

@superstructor
Copy link
Contributor

Thanks @dannyfreeman 👍🏻 Code is good quality.

Please raise a new issue for the solution described in #752 (comment) as my understanding is it would be best separated as a distinct future change to this even if it'll replace this warning.

@dannyfreeman
Copy link
Contributor Author

Thank you @superstructor
I followed up with a new ticket #753 and a draft PR to go along with it #754

aatkin added a commit to CSCfi/rems that referenced this pull request Apr 3, 2024
After this update re-frame prints warnings to console log regarding subscription leaks. The change was introduced in day8/re-frame#752 and released in https://github.com/day8/re-frame/releases/tag/v1.3.0 and quite many components in REMS are affected.

shadow-cljs update *should* be harmless.
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