Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When watching LibreOffice Writer file, get [Errno 9] Bad file descriptor #744

Closed
superlou opened this issue Dec 24, 2020 · 4 comments
Closed

Comments

@superlou
Copy link

superlou commented Dec 24, 2020

I'm using the following code based on the example:

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class EventHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print(event)


def main():
    event_handler = EventHandler()
    observer = Observer()
    observer.schedule(event_handler, './test.docx')
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()


if __name__ == '__main__':
    main()

The first time I save the watched file in LibreOffice, I see the event printed, but on subsequent prints, there is nothing until I send the KeyboardInterrupt. The terminal output is:

<FileModifiedEvent: event_type=modified, src_path='./test.docx', is_directory=False>
^CTraceback (most recent call last):
  File "main.py", line 49, in main
    time.sleep(1)
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 61, in <module>
    main()
  File "main.py", line 51, in main
    observer.stop()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/utils/__init__.py", line 81, in stop
    self.on_thread_stop()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/observers/api.py", line 361, in on_thread_stop
    self.unschedule_all()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/observers/api.py", line 357, in unschedule_all
    self._clear_emitters()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/observers/api.py", line 231, in _clear_emitters
    emitter.stop()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/utils/__init__.py", line 81, in stop
    self.on_thread_stop()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/observers/inotify.py", line 121, in on_thread_stop
    self._inotify.close()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/observers/inotify_buffer.py", line 50, in close
    self.stop()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/utils/__init__.py", line 81, in stop
    self.on_thread_stop()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/observers/inotify_buffer.py", line 46, in on_thread_stop
    self._inotify.close()
  File "/home/lsimons/.local/lib/python3.8/site-packages/watchdog/observers/inotify_c.py", line 277, in close
    os.close(self._inotify_fd)
OSError: [Errno 9] Bad file descriptor

I've noticed that when LibreOffice saves, a bunch of temporary files are created. If I instead watch the home directory recursively (observer.schedule(event_handler, './', recursive=True)), I can see modified events on the edited file:

# First save
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<FileModifiedEvent: event_type=modified, src_path='./.~lock.test.docx#', is_directory=False>
<FileModifiedEvent: event_type=modified, src_path='./lu204422ynalp.tmp', is_directory=False>
<FileModifiedEvent: event_type=modified, src_path='./lu204422ynalp.tmp', is_directory=False>
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<FileModifiedEvent: event_type=modified, src_path='./test.docx', is_directory=False>
# Second save
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<FileModifiedEvent: event_type=modified, src_path='./.~lock.test.docx#', is_directory=False>
<FileModifiedEvent: event_type=modified, src_path='./lu204422ynalr.tmp', is_directory=False>
<FileModifiedEvent: event_type=modified, src_path='./lu204422ynalr.tmp', is_directory=False>
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<FileModifiedEvent: event_type=modified, src_path='./test.docx', is_directory=False>

However, watching the entire directory recursively that a file is in and filtering for only the events I'm interested in doesn't feel like a good solution. Is this kind of behavior expected? I thought maybe LibreOffice was deleting and recreating test.docx rapidly during a save, but the FileModifiedEvent is modified rather than deletion and creation.

@BoboTiG
Copy link
Collaborator

BoboTiG commented Feb 10, 2021

Should be closed by #760.

@superlou
Copy link
Author

When testing the example code with watchdog 2.0.1, the error is gone, but the event handler is still only called the first time the file is saved. Is this considered a new bug?

@BoboTiG
Copy link
Collaborator

BoboTiG commented Feb 20, 2021

That's normal. The error is gone as expected but then the watcher is no more watching anything. You have to setup again the watcher because the watched file was deleted by LibreOffice. It may be easier to watch the parent folder instead of the file.

@superlou
Copy link
Author

Yep, that looks to be the case. If I add an on_deleted(...) method to the EventHandler I can see it get deleted:

<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<DirModifiedEvent: event_type=modified, src_path='.', is_directory=True>
<FileModifiedEvent: event_type=modified, src_path='./test.docx', is_directory=False>
<DirDeletedEvent: event_type=deleted, src_path='./test.docx', is_directory=True>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants