guard / listen

The Listen gem listens to file modifications and notifies you about the changes.
https://rubygems.org/gems/listen
MIT License
1.92k stars 246 forks source link

Wrap Listener instances in WeakRef #477

Closed jonathanhefner closed 4 years ago

jonathanhefner commented 4 years ago

The @listeners variable holds a reference to each Listener instance created by Listen.to and can only be cleared by calling Listen.stop. This can prevent Listener instances from being garbage collected if they are abandoned before Listen.stop is called.

This commit wraps Listener instances in WeakRef before adding them to @listeners, to allow them to be garbage collected.

Partially addresses #476.

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.1%) to 97.831% when pulling 63eff89147b4776158acc0a6e603cd443e4d1627 on jonathanhefner:listeners-memory-leak into ba5059c9f3a8594eae7e0050687c1a6735046d85 on guard:master.

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.03%) to 97.757% when pulling 694e28bc0431ded8392ad09bfcfcb84bcc570da3 on jonathanhefner:listeners-memory-leak into ba5059c9f3a8594eae7e0050687c1a6735046d85 on guard:master.

jonathanhefner commented 4 years ago

@headius Does this look all right to you? I decided to forgo clearing dead WeakRefs in Listen.to because it would require using Mutex instead of a simple Queue to stay thread-safe. I can make that change if you prefer. However, if someone were to create so many listeners that dead WeakRefs could become a problem, they might prefer using Listener.new instead (since it will have less overhead).