dsoprea / PyInotify

An efficient and elegant inotify (Linux filesystem activity monitor) library for Python. Python 2 and 3 compatible.
GNU General Public License v2.0
242 stars 73 forks source link

Crashed in calls.py(32): _check_nonnegative #71

Closed renatosamperio closed 5 years ago

renatosamperio commented 5 years ago

I made a simple program to monitor my home path:

import inotify.adapters

def _main():
    i = inotify.adapters.InotifyTree('/opt/test_user/')
    try:
        for event in i.event_gen():
            if event is None:
                continue
            (header, types, path, filename) = event
            if 'IN_ISDIR' not in types:
               print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(
                 path, filename, types))
if __name__ == '__main__':
    _main()

I am getting the following error:

Traceback (most recent call last):
  File "sample2.py", line 26, in <module>
    _main()
  File "sample2.py", line 4, in _main
    i = inotify.adapters.InotifyTree('/opt/test_user/')
  File "/usr/local/lib/python2.7/dist-packages/inotify/adapters.py", line 340, in __init__
    self.__load_tree(path)
  File "/usr/local/lib/python2.7/dist-packages/inotify/adapters.py", line 354, in __load_tree
    for filename in os.listdir(current_path):

I had to run the program with SUDO though. But whenver I remove the following exception, all works fine:

def _check_nonnegative(result):
    if result == -1:
        raise InotifyError("Call failed (should not be -1): (%d)" % 
                           (result,))

Why was this check required? Is it good idea to remove it?

Regards,

Renato.

alphadx commented 5 years ago

This is an debian/ubuntu "issue". You can fix it adding this line in /etc/sysctl.conf file. fs.inotify.max_user_watches=524288

It's about the max number of user that can watch. It show when you try to watch a huge number of files/directory.

rgammans commented 5 years ago

This maybe a seperate issue but....

I've just hit this problem; and got the following message.

inotify.calls.InotifyError: Call failed (should not be -1): (-1) ERRNO=(0)

And was confused, and slowed in my debugging by the zero errno. I changed the sysctl variable as above; and it works; but I still see a zero errno if I try something that shouldn't work. Is there something else going on here? ( I would have expected something like ENOSPC and EACCESS respectively, or at least their numeric equivalents)

Python 3.7.2rc1 (default, Dec 12 2018, 06:25:49)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inotify.adapters
>>> i = inotify.adapters.Inotify()
>>> i.add_watch("./testdir")
1
>>> i.add_watch("/root" )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/.local/share/virtualenvs/test/lib/python3.7/site-packages/inotify/adapters.py", line 95, in add_watch
    wd = inotify.calls.inotify_add_watch(self.__inotify_fd, path_bytes, mask)
  File "~/.local/share/virtualenvs/test/lib/python3.7/site-packages/inotify/calls.py", line 36, in _check_nonnegative
    (result,))
inotify.calls.InotifyError: Call failed (should not be -1): (-1) ERRNO=(0)
dsoprea commented 5 years ago

I'm not really sure where we can go with this discussion. Doesn't seem like an issue with the project. As much as I'd like to help, I don't think I/we can.

If you make any progress, feel free to post a pull request with a modification to the README.md to share it with others.

@alphadx That includes you. You're welcome to add a "Troubleshooting" section with this as the first item.

prafulchavda4 commented 3 years ago

echo fs.inotify.max_user_watches=16384 | sudo tee -a /etc/sysctl.conf sudo sysctl -p

it's work please check it

prafulchavda4 commented 3 years ago

https://dev.to/rubiin/ubuntu-increase-inotify-watcher-file-watch-limit-kf4