Watchman 4.9.0(inotify)订阅丢失文件删除通知问题问询
Hey there, let's break down the two key issues you're facing with your Watchman setup and how to fix them:
1. No directory change notification when deleting a file
This is actually expected default behavior in Watchman. When you delete a file, the parent directory's metadata (like mtime) does update, but Watchman doesn't automatically trigger a directory event unless you explicitly configure it to monitor directory attributes.
To capture directory changes alongside file events, adjust your subscription to explicitly include both file and directory monitoring. Modify your subscribe query like this:
q = w.query('subscribe', '/tmp/z', 'Buffy', { 'expression': ["since", clock], 'fields': ["name", "exists", "oclock", "ctime_ns", "new", "mode"], 'match': ["**/*", {"type": "any"}] # Explicitly match files and directories })
Alternatively, you can refine the expression to target both file and directory types directly:
'expression': [ "allof", ["since", clock], ["anyof", ["type", "f"], ["type", "d"] ] ]
2. Missing delete notification after simulating crash recovery
The core problem here is that you're using an outdated clock value when re-subscribing. Let's trace your code flow:
- After touching the file, you update
clockto the post-touch event clock. - You delete the file and receive the delete event, but you don't update
clockto the post-delete value from that event. - When you reconnect and subscribe with the old (post-touch) clock, Watchman doesn't surface the delete event again because the original subscription context was lost when you closed the client.
Fix this by saving the clock from the delete event before closing the first client:
# After deleting the file delete_data = w.receive() clock = delete_data['clock'] # Update clock to the post-delete value print('Delete file:') print(delete_data) print('Updated Clock:', clock)
Now when you reconnect and subscribe with this updated clock, Watchman will correctly report any changes after the delete (or confirm no new changes exist, instead of hanging).
One extra note: Watchman 4.9.0 is quite outdated (released in 2018) and has known edge-case bugs with inotify event handling and persistence. Upgrading to the latest stable version would likely resolve many of these quirks long-term.
内容的提问来源于stack exchange,提问作者jcea

