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

FileSystemEventHandler cannot detect changes of files in Windows drives when running from WSL 2 #1072

Open
seanballais opened this issue Sep 18, 2024 · 6 comments

Comments

@seanballais
Copy link

seanballais commented Sep 18, 2024

NOTE: The issue here is beyond the control of the library authors, so treat this issue as a warning.

Issue

I tried making a program that watches for file changes in a Windows drive. Assume that I have the following file structure, where watchdog_test.py is the main Python script:

.
│─ assets/
│    └─ raw/
│        └─ text/
│            └─ app.txt
└─ watchdog_test.py

watchdog_test.py has the following code:

import time

from watchdog.events import FileSystemEventHandler, FileSystemEvent
from watchdog.observers import Observer


class Handler(FileSystemEventHandler):
    def on_any_event(self, event: FileSystemEvent) -> None:
        print(f':: Something happened to {event.src_path}!')


handler = Handler()
observer = Observer()

observer.schedule(handler, 'assets/raw/text', recursive=True)
observer.start()

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print('[] Shutting down...')
finally:
    observer.stop()
    observer.join()

For some reason, it sometimes detects file changes when trying to check the current directory (path for the Observer is set to '.'), but not when watching a specific path (just like above). This code, however, works fantastically when running in Windows watching paths in Windows drives (e.g. C:), as well as in Linux (WSL) watching paths in the WSL drives.

Cause

This issue is caused by the lack of support of file notification support in the 9P protocol servers in Windows and WSL[1], which does affect inotify, which watchdog uses in Linux. This prevents watchdog from watching changes in Windows drive paths inside WSL.

Workaround

Use PollingObserver when you're trying to watch files in Windows drives from WSL.

Possible Action Plan

Since this issue is practically unfixable until complete inotify support for files in Windows drives is added, the documentation might have to be updated to let users know about this issue.

References

[1] - https://youtu.be/lwhMThePdIo?si=NW7fBHIQN7iOosKR&t=3166

@BornKrefeld
Copy link

I faced the same issue with FileSystemEventHandler in Windows. How I can use Polling Observer

@seanballais
Copy link
Author

Just replace Observer() with PollingObserver(), @BornKrefeld.

@BornKrefeld
Copy link

PollingObserver() was not accepted, belongs this CMD to a different interpreter then watchdog?

@seanballais
Copy link
Author

@BornKrefeld, are there any error messages?

@BornKrefeld
Copy link

this is my error message:
"D:\04_Pycharm Projekte\General_Projects\Std_pgm.venv\Scripts\python.exe" "D:\04_Pycharm Projekte\General_Projects\Py_Security\XS1 PY Watchdog_template1.py"
Traceback (most recent call last):
File "D:\04_Pycharm Projekte\General_Projects\Py_Security\XS1 PY Watchdog_template1.py", line 14, in
observer = PollingObserver()
^^^^^^^^^^^^^^^
NameError: name 'PollingObserver' is not defined

Process finished with exit code 1

this was my coding around PollingObserver:

event_handler = MyEventHandler()
observer = PollingObserver()
observer.schedule(event_handler, ".", recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()

@seanballais
Copy link
Author

You forgot to import PollingObserver, @BornKrefeld.

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

3 participants