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

Watched file no longer watched after a change #235

Closed
sdondley opened this issue Jul 12, 2023 · 8 comments
Closed

Watched file no longer watched after a change #235

sdondley opened this issue Jul 12, 2023 · 8 comments
Labels

Comments

@sdondley
Copy link

Description

If I watch a single file and then make a change to it, the changs is picked up by watchlist. However, the second time a change is made to a file, watchfiles seems to no longer notice. watchfiles seems to work fine when watching a directory.

Example Code

from watchfiles import run_process

def callback(changes):
    print('changes detected:', changes)

def foobar(a, b):
    print('foobar called with:', a, b)

if __name__ == '__main__':
    run_process('single_file', target=foobar, args=(1, 2), callback=callback)

Watchfiles Output

./watch.py
foobar called with: 1 2
changes detected: {(<Change.deleted: 3>, '/home/admin/flask/single_file')}
process already dead, exit code: 0
foobar called with: 1 2

I get no more output as more changes are made to the file.

Operating System & Architecture

Linux-6.1.0-10-cloud-amd64-x86_64-with-glibc2.36
#1 SMP PREEMPT_DYNAMIC Debian 6.1.37-1 (2023-07-03)

Environment

using tmux

Python & Watchfiles Version

python: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0], watchfiles: 0.19.0

Rust & Cargo Version

No response

@sdondley sdondley added the bug label Jul 12, 2023
@sdondley
Copy link
Author

Just tested this on my mac. Works perfectly and no problems with tmux. Hmm.

I should maybe also note that watchfiles was installed with pipx with things running in some kind of venv for python. I'm not familiar with how that works at all. It's the first I've come across this kind of thing.

@sdondley sdondley changed the title More than two changes to a watched file seems to break things Watched file no longer watched after a change Jul 12, 2023
@sdondley
Copy link
Author

To try to find a workaround, I turned on force_polling:

#!/home/admin/.local/pipx/venvs/watchfiles/bin/python3
from watchfiles import watch

for changes in watch('testfile', force_polling=True):
    print(changes)

It seemed to work for a bit but then I got errors. Here's the output:

{(<Change.modified: 2>, 'testfile')}
{(<Change.modified: 2>, 'testfile')}
{(<Change.modified: 2>, 'testfile')}
{(<Change.modified: 2>, 'testfile')}
{(<Change.modified: 2>, 'testfile')}
Traceback (most recent call last):
  File "/home/admin/flask/./watch.py", line 4, in <module>
    for changes in watch('testfile', force_polling=True):
  File "/home/admin/.local/pipx/venvs/watchfiles/lib/python3.11/site-packages/watchfiles/main.py", line 105, in watch
    raw_changes = watcher.watch(debounce, step, rust_timeout, stop_event)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_rust_notify.WatchfilesRustInternalError: error in underlying watcher: No such file or directory (os error 2) about ["testfile"]

@sdondley
Copy link
Author

Doing some more experimenting. Got a different error:

{(<Change.modified: 2>, 'testfile')}
thread 'notify-rs poll loop' panicked at 'internal error: entered unreachable code', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/notify-5.0.0-pre.15/src/poll.rs:206:49
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "/home/admin/test/./watch.py", line 4, in <module>
    for changes in watch('testfile', force_polling=True, raise_interrupt=False):
  File "/home/admin/.local/pipx/venvs/watchfiles/lib/python3.11/site-packages/watchfiles/main.py", line 105, in watch
    raw_changes = watcher.watch(debounce, step, rust_timeout, stop_event)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_rust_notify.WatchfilesRustInternalError: error in underlying watcher: No such file or directory (os error 2) about ["testfile"]

I'm making my edits to the file with vim.

@sdondley
Copy link
Author

If I append to the file with a bash command like echo blah >> testfile, things works perfectly.

I suspect this has something to do with how vim modifies/updates files.

@sdondley
Copy link
Author

Ok, after searching the issue queue on vim, I found this: #215

Which led me to this: notify-rs/notify#394

So as best I can tell, this watchfiles isn't really going to work for me when watching a single file until some lower level issue in notify gets addressed.

@samuelcolvin
Copy link
Owner

If the file gets deleted, it's not surprising watching stops.

I would watch the entire directory with rescursive=False, then check which file changes in your own logic.

@samuelcolvin samuelcolvin closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2023
@sdondley
Copy link
Author

What I ended up doing is putting the file in a subdir and symlinking to it from the parent dir and then watching the subdir.

@honglei
Copy link

honglei commented Aug 18, 2023

When I use vi to edit and save the watched file, the lib give <Change.deleted: 3>, '/home/uos/conf/send.json', which caused watch stoped . But the file is not removed.
Env: Python3.11.4/watchfiels 0.19/Debian10 arm64

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

No branches or pull requests

3 participants