question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Store's user-supplied unsubscribe function not called on unsubscribe

See original GitHub issue

Describe the bug

If a store is created with a function as a second argument, that function is called when the subscriber count goes from zero to one. In theory, the function returned from that function is called when the subscriber count goes from one to zero. It doesn’t.

To Reproduce

https://svelte.dev/repl/77e4730d8b744fa4ab047a8354b9aedd?version=3.12.1

Expected behavior

When visible becomes false, an alert should appear.

Severity

It’s not great, though in practice it probably only means the occasional memory leak.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Sep 17, 2019

Hi Rich, there’s a double subscribe in your REPL which is causing the issue.

Looking at the debugger, the auto-subscribe fires first, which drops into this code correctly:

if (subscribers.length === 1) {
   stop = start(set) || noop;
}

However, you have another subscribe inside that start which bumps the subscribers length to 2, which causes this code fragment to be skipped on destroy_component:

if (subscribers.length === 0) {
	stop();
	stop = null;
}

Here’s the debugger state: image

Right before the component context is wiped out, we can see the store still has a subscriber with the proper stop function stored in it.

image

I can see the usefulness of protecting against accidental repeated subscribes, though we’d have to add some tracking information so we tell if all subscriptions originate from same component. If they all have the same origin then we’d need to call the stop() function regardless of subscription count.

0reactions
Conduitrycommented, Oct 20, 2019

I don’t think there’s a bug here. As others have noted, the on-first-subscriber function is adding its own first subscriber, which is not being removed until the on-removal-of-last-subscriber function is called, which will be never, as there’s still that extra subscriber hanging. Without that extra subscription, this works fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unsubscribe in subscribe callback function? - angular
Most of the time I'm just wanting the ONE response from my backend. And then I'll want to unsubscribe. So why not call...
Read more >
To unsubscribe, or not to unsubscribe, that is the question.
One of the ways is to call .unsubscribe() on the Subscription we want to close. ... This function is passed to the new...
Read more >
Unsubscribe - Amazon Simple Notification Service
If the Unsubscribe call does not require authentication and the requester is not the subscription owner, a final cancellation message is delivered to...
Read more >
Purchasing - Rivian
Reserve without stepping into a dealership. It's easy and intuitive to reserve your Rivian online. When it comes time to buy, complete your...
Read more >
6 Ways to Unsubscribe from Observables in Angular
If a subscription is not closed the function callback attached to it will ... We do so by calling the unsubscribe method in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found