dotnet / dotnet/reactive

Disposables returned by Subscribe should use weak references

Open
#1,280 3 comments 1 reaction 0 assignees View on GitHub
[area] Rx
Dominant language
C#
Stars
7.2k
Forks
798
PR merge metrics
No merged PRs in 30d

Description

#### Feature request
> Which next library version (i.e., patch, minor or major)?

Any; this change would be nearly transparent to users.

> What are the platform(s), environment(s) and related component version(s)?

All

> How commonly is this feature needed (one project, several projects, company-wide, global)?

This would primarily benefit users of Rx who rely heavily on hot `IObservable` instances, where memory management is more of an issue.

> Please describe the feature.

Most `Subscribe` functions throughout the code seem to return an `IDisposable` implementation which holds a strong reference to the source and/or observers (see the source of `Subject` and `SubjectDisposable` as an example). In the case of a hot `IObservable` where `Subscribe` is called and the returned `IDisposable` is stored as a strong reference, this results in a memory leak if the `IObservable` and `IObserver` are both dereferenced; the `IDisposable` will keep both alive. If the `IDisposable` instance were to hold a weak reference to the source, then dereferencing the `IObservable` and `IObserver` would be enough for both to get garbage collected.

While you could certainly argue that this issue is minor, or that the expectation is that the `IDisposable`'s lifetime should not be longer than the `IObserver`'s, I quickly ran into this while spending the afternoon investigating how lifetime management works and this definitely threw me for a loop, since the semantics of `Dispose` have always implied "do nothing" semantics in the case that no action is needed (ie: repeated calls).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.