Open
Conversation
This uses a weak reference to the notifier in _SysProcessEvent and adds a `__del__` method for Notifier. This prevents pyinotify from leaking inotify file descriptors (instances) and its watches. Fixes seb-m#95 I have used the following script to test it: from __future__ import print_function import gc import os from pyinotify import Notifier, WatchManager def has_inotify_fds(): "Does the current process have inotify fds?" r = 0 for x in os.walk('/proc/{}/fd'.format(os.getpid())): for y in x[2]: path = os.path.join(x[0], y) if os.path.exists(path): if 'inotify' in os.path.join(os.readlink(path)): r += 1 return r gc.disable() wm = WatchManager() n = Notifier(wm) assert has_inotify_fds() == 1 # Stopping multiple times should work: n.stop() assert has_inotify_fds() == 0 n.stop() gc.collect() # XXX: needs a new WatchManager, because the previous fd gets closed by stop! wm = WatchManager() n = Notifier(wm) assert has_inotify_fds() == 1 # Collect anything. gc.collect() assert n in gc.get_objects() n = None assert gc.garbage == [] assert not any(isinstance(x, Notifier) for x in gc.garbage) assert not any(isinstance(x, Notifier) for x in gc.get_objects()) assert has_inotify_fds() == 0
89a88f6 to
c88f370
Compare
Contributor
Author
|
Rebased on master. @seb-m |
Owner
|
Hi @blueyed, I still don't have an opinion about it. I must say I didn't have time to review it and think about it carefully and this is the kind of structural change I don't want to commit lightly. |
|
FYI, I just ran into this issue and thought of a similar fix. But I think that closing the fd from Notifier is the wrong thing to do (and has been in the first place, it's not introduced by this PR); the fd is owned by |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This uses a weak reference to the notifier in
_SysProcessEventand addsa
__del__method for Notifier.This prevents pyinotify from leaking inotify file descriptors
(instances) and its watches.
Fixes #95
I have used the following script to test it: