How do custom Combine subjects clean up cancelled subscriptions?

I'm trying to understand how subjects are supposed to clean up references to subscriptions when they cancel. Looking at OpenCombine's implementation of CurrentValueSubject I see that there's a _subscriptions property which keeps track of the current subscriptions. I can see that they're appended in the receiveSubscriber callback. Where I'm a bit lost is that these subscriptions never seem to be removed. The subscription itself nullifies it's reference to the subject at but the subject doesn't even seem to be notified. Do the subscriptions just stick around after they're cancelled or are they cleaned up somehow?

OpenCombine's implementation of CurrentValueSubject leaks subscriptions. CombineX and Apple Combine clean them up properly.

The proper subscription handling in OpenCombine is a work in progress.

1 Like

Right, that makes sense then :) Thought perhaps there was some magic going on that I wasn't seeing.

Thanks, will take a look at the implementation and see how it's done, I'm guessing the subject needs to be passed into the subscription.

So I was right, but the type erasure certainly makes the implementation more involved than you might expect.

I'm happy to share the good news: the issue with leaking subscriptions in OpenCombine has been fixed in the master branch.

1 Like