Repository navigation
fix: prevent file watcher dead after stopping one of same inode paths - #414
Johnson-zs wants to merge 1 commit into
Conversation
Added reference-counted registry to prevent GLib's inotify backend from canceling shared watch descriptors when multiple paths point to the same inode (via symlinks or bind mounts). Previously, if two DWatcher instances monitored the same directory through different paths (e.g., directory and symlink), stopping one would cancel the underlying inotify watch, leaving the other watcher permanently deaf. The new MonitorKeepAliveRegistry tracks active watchers per inode, only canceling the shared monitor when the last watcher for that inode stops. This ensures remaining watchers continue receiving events. Two test cases verify both that stopping one path does not affect others and that a fresh watcher works after all previous watchers for an inode are stopped. Influence: 1. Test monitoring a directory and its symlink simultaneously, then stop one watcher 2. Verify the remaining watcher still receives file creation/deletion events 3. Test scenario with multiple bind mounts or hard links to same directory 4. Test that after stopping all watchers for an inode, new watchers work correctly 5. Ensure non-local file paths (non-inotify cases) fall back to direct cancel without regression fix: 修复停止同一inode路径之一后文件监视器失效的问题 添加了引用计数注册表,以防止在多个路径指向同一inode(通过符号链接或绑定 挂载)时,GLib的inotify后端取消共享监视描述符。之前,如果两个DWatcher实 例通过不同路径(例如目录和符号链接)监视同一目录,停止其中一个会取消底层 的inotify监视,导致另一个监视器永久失效。新的MonitorKeepAliveRegistry按 inode跟踪活动监视器,仅在该inode的最后一个监视器停止时才取消共享监视器。 这确保了剩余的监视器能继续接收事件。两个测试用例验证了停止一个路径不影响 其他路径,以及在该inode的所有先前监视器停止后,新的监视器仍能正常工作。 Influence: 1. 测试同时监视目录及其符号链接,然后停止其中一个监视器 2. 验证剩余的监视器仍能接收文件创建/删除事件 3. 测试具有多个绑定挂载或硬链接到同一目录的场景 4. 测试在停止某个inode的所有监视器后,新的监视器能正常工作 5. 确保非本地文件路径(非inotify情况)回退到直接取消而不会引起回归 Log: PMS: Task-398995
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Johnson-zs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Reviewer's GuideImplementationTrack active monitors by inode and defer canceling parked GLib monitors until the last watcher for that inode stops.
TestsAdd symlink-based regressions for keeping a surviving watcher active and for starting a fresh watcher after all prior watchers stop.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 6 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/dfm-io/dfm-io/dwatcher.cpp" line_range="289" />
<code_context>
+ if (MonitorKeepAliveRegistry::instance().retainOrPark(d->inodeKey, d->gmonitor, &doomed)) {
+ // 同inode仍有其它路径在监视:不cancel(会误删GLib共享订阅使存活者永久失聪),
+ // 仅断开changed回调静默本对象,监视器交由注册表持有
+ g_signal_handlers_disconnect_by_data(d->gmonitor, this);
+ d->gmonitor = nullptr;
+ } else {
</code_context>
<issue_to_address>
**Concurrent stops crash**
When two watchers for the same inode are stopped concurrently, `retainOrPark()` returns a parked monitor without protecting it from a concurrent final `stop()`, which can cancel and unref it before `g_signal_handlers_disconnect_by_data()` uses it, crashing the process.
Keep a strong reference to the monitor until its signal handler is disconnected, or serialize disconnection with final removal.
Also at `src/dfm-io/dfm-io/dwatcher.cpp:297`.
</issue_to_address>
### Comment 2
<location path="src/dfm-io/dfm-io/dwatcher.cpp" line_range="60" />
<code_context>
+ return true;
+ }
+ refCounts.remove(key);
+ *doomed = parked.value(key);
+ parked.remove(key);
+ return false;
</code_context>
<issue_to_address>
**New watcher stops receiving events**
When a watcher starts while another thread stops the last watcher for the same inode, `start()` creates or registers its monitor while `retainOrPark()` has removed the last count and `stop()` is canceling outside the mutex, so canceling the old monitor disables the shared backend watch and the new watcher stops receiving events.
Serialize start registration and last-monitor cancellation so a new watcher cannot join a watch that is being canceled.
Also at `src/dfm-io/dfm-io/dwatcher.cpp:276-278`, `src/dfm-io/dfm-io/dwatcher.cpp:286-299`.
</issue_to_address>
### Comment 3
<location path="src/dfm-io/dfm-io/dwatcher.cpp" line_range="276" />
<code_context>
g_signal_connect(d->gmonitor, "changed", G_CALLBACK(&DWatcherPrivate::watchCallback), this);
+ // key在start时缓存:stop时目标可能已被删除重建(inode变化),重stat会减错计数
+ d->inodeKey = inodeKeyOf(d->gfile);
+ MonitorKeepAliveRegistry::instance().addActive(d->inodeKey);
+
</code_context>
<issue_to_address>
**Replacement path cancels active watch**
When the watched path is replaced between monitor creation and the subsequent `stat()`, `inodeKeyOf()` records the replacement inode's key for a monitor still attached to the original inode, so stopping that watcher can cancel the shared watch used by another watcher of the original inode, which then stops receiving events.
Derive the registry key from the inode actually attached to the monitor rather than statting the path after monitor creation.
</issue_to_address>
### Comment 4
<location path="src/dfm-io/dfm-io/dwatcher.cpp" line_range="80-81" />
<code_context>
+ if (!path)
+ return 0;
+ struct stat st;
+ if (::stat(path, &st) != 0)
+ return 0;
+ return (quint64(st.st_dev) << 32) | quint32(st.st_ino);
+}
</code_context>
<issue_to_address>
**Missing files bypass shared registry**
When a file watcher successfully monitors a nonexistent local file through one of multiple paths to the same parent directory, `inodeKeyOf` returns zero for a nonexistent local target, so `stop()` directly cancels its monitor; when GLib monitors that missing file through its parent directory, canceling one alias also removes the shared inotify watch and the other watcher misses the file’s creation.
For local paths whose target does not exist, key the monitor by the inode of the directory GLib watches, or otherwise coordinate cancellation of monitors sharing that parent watch.
</issue_to_address>
### Comment 5
<location path="src/dfm-io/dfm-io/dwatcher.cpp" line_range="82-83" />
<code_context>
+ struct stat st;
+ if (::stat(path, &st) != 0)
+ return 0;
+ return (quint64(st.st_dev) << 32) | quint32(st.st_ino);
+}
+
+} // namespace
</code_context>
<issue_to_address>
**Colliding inodes retain monitors**
When distinct inodes have the same encoded low 32-bit device and inode values and one watcher remains active, `inodeKeyOf()` truncates `st_dev` and `st_ino` to 32 bits, so distinct inodes share a registry key; stopping one watcher parks its monitor while the unrelated watcher remains active, retaining monitors and kernel watches and potentially exhausting watcher resources.
Use a registry key that preserves the full device and inode values.
</issue_to_address>
### Comment 6
<location path="src/dfm-io/dfm-io/dwatcher.cpp" line_range="56" />
<code_context>
+ const int remaining = refCounts.value(key) - 1;
+ if (remaining > 0) {
+ refCounts[key] = remaining;
+ parked[key].append(monitor);
+ return true;
+ }
</code_context>
<issue_to_address>
**Repeated restarts accumulate monitors**
When one watcher is repeatedly restarted while another watcher for the same inode stays active, `DWatcher::start()` first calls `stop()`, which parks the old monitor whenever another watcher for the inode remains active. Each subsequent restart creates another monitor, so the registry retains one more `GFileMonitor` per restart until the other watcher stops; a long-lived sibling therefore lets repeated restarts grow retained objects without bound.
Avoid retaining a new parked monitor for every restart, for example by deduplicating or reusing retained monitors for that inode.
</issue_to_address>| if (MonitorKeepAliveRegistry::instance().retainOrPark(d->inodeKey, d->gmonitor, &doomed)) { | ||
| // 同inode仍有其它路径在监视:不cancel(会误删GLib共享订阅使存活者永久失聪), | ||
| // 仅断开changed回调静默本对象,监视器交由注册表持有 | ||
| g_signal_handlers_disconnect_by_data(d->gmonitor, this); |
There was a problem hiding this comment.
🟠 High · Concurrent stops crash
When two watchers for the same inode are stopped concurrently, retainOrPark() returns a parked monitor without protecting it from a concurrent final stop(), which can cancel and unref it before g_signal_handlers_disconnect_by_data() uses it, crashing the process.
Keep a strong reference to the monitor until its signal handler is disconnected, or serialize disconnection with final removal.
Also at src/dfm-io/dfm-io/dwatcher.cpp:297.
Prompt for AI agents
In `src/dfm-io/dfm-io/dwatcher.cpp` at line 289:
**Concurrent stops crash**
When two watchers for the same inode are stopped concurrently, `retainOrPark()` returns a parked monitor without protecting it from a concurrent final `stop()`, which can cancel and unref it before `g_signal_handlers_disconnect_by_data()` uses it, crashing the process.
Keep a strong reference to the monitor until its signal handler is disconnected, or serialize disconnection with final removal.
Also at `src/dfm-io/dfm-io/dwatcher.cpp:297`.| return true; | ||
| } | ||
| refCounts.remove(key); | ||
| *doomed = parked.value(key); |
There was a problem hiding this comment.
🟠 High · New watcher stops receiving events
When a watcher starts while another thread stops the last watcher for the same inode, start() creates or registers its monitor while retainOrPark() has removed the last count and stop() is canceling outside the mutex, so canceling the old monitor disables the shared backend watch and the new watcher stops receiving events.
Serialize start registration and last-monitor cancellation so a new watcher cannot join a watch that is being canceled.
Also at src/dfm-io/dfm-io/dwatcher.cpp:276-278, src/dfm-io/dfm-io/dwatcher.cpp:286-299.
Prompt for AI agents
In `src/dfm-io/dfm-io/dwatcher.cpp` at line 60:
**New watcher stops receiving events**
When a watcher starts while another thread stops the last watcher for the same inode, `start()` creates or registers its monitor while `retainOrPark()` has removed the last count and `stop()` is canceling outside the mutex, so canceling the old monitor disables the shared backend watch and the new watcher stops receiving events.
Serialize start registration and last-monitor cancellation so a new watcher cannot join a watch that is being canceled.
Also at `src/dfm-io/dfm-io/dwatcher.cpp:276-278`, `src/dfm-io/dfm-io/dwatcher.cpp:286-299`.| g_signal_connect(d->gmonitor, "changed", G_CALLBACK(&DWatcherPrivate::watchCallback), this); | ||
|
|
||
| // key在start时缓存:stop时目标可能已被删除重建(inode变化),重stat会减错计数 | ||
| d->inodeKey = inodeKeyOf(d->gfile); |
There was a problem hiding this comment.
🟠 High · Replacement path cancels active watch
When the watched path is replaced between monitor creation and the subsequent stat(), inodeKeyOf() records the replacement inode's key for a monitor still attached to the original inode, so stopping that watcher can cancel the shared watch used by another watcher of the original inode, which then stops receiving events.
Derive the registry key from the inode actually attached to the monitor rather than statting the path after monitor creation.
Prompt for AI agents
In `src/dfm-io/dfm-io/dwatcher.cpp` at line 276:
**Replacement path cancels active watch**
When the watched path is replaced between monitor creation and the subsequent `stat()`, `inodeKeyOf()` records the replacement inode's key for a monitor still attached to the original inode, so stopping that watcher can cancel the shared watch used by another watcher of the original inode, which then stops receiving events.
Derive the registry key from the inode actually attached to the monitor rather than statting the path after monitor creation.| if (::stat(path, &st) != 0) | ||
| return 0; |
There was a problem hiding this comment.
🟡 Medium · Missing files bypass shared registry
When a file watcher successfully monitors a nonexistent local file through one of multiple paths to the same parent directory, inodeKeyOf returns zero for a nonexistent local target, so stop() directly cancels its monitor; when GLib monitors that missing file through its parent directory, canceling one alias also removes the shared inotify watch and the other watcher misses the file’s creation.
For local paths whose target does not exist, key the monitor by the inode of the directory GLib watches, or otherwise coordinate cancellation of monitors sharing that parent watch.
Prompt for AI agents
In `src/dfm-io/dfm-io/dwatcher.cpp` at lines 80-81:
**Missing files bypass shared registry**
When a file watcher successfully monitors a nonexistent local file through one of multiple paths to the same parent directory, `inodeKeyOf` returns zero for a nonexistent local target, so `stop()` directly cancels its monitor; when GLib monitors that missing file through its parent directory, canceling one alias also removes the shared inotify watch and the other watcher misses the file’s creation.
For local paths whose target does not exist, key the monitor by the inode of the directory GLib watches, or otherwise coordinate cancellation of monitors sharing that parent watch.| return (quint64(st.st_dev) << 32) | quint32(st.st_ino); | ||
| } |
There was a problem hiding this comment.
🟡 Medium · Colliding inodes retain monitors
When distinct inodes have the same encoded low 32-bit device and inode values and one watcher remains active, inodeKeyOf() truncates st_dev and st_ino to 32 bits, so distinct inodes share a registry key; stopping one watcher parks its monitor while the unrelated watcher remains active, retaining monitors and kernel watches and potentially exhausting watcher resources.
Use a registry key that preserves the full device and inode values.
Prompt for AI agents
In `src/dfm-io/dfm-io/dwatcher.cpp` at lines 82-83:
**Colliding inodes retain monitors**
When distinct inodes have the same encoded low 32-bit device and inode values and one watcher remains active, `inodeKeyOf()` truncates `st_dev` and `st_ino` to 32 bits, so distinct inodes share a registry key; stopping one watcher parks its monitor while the unrelated watcher remains active, retaining monitors and kernel watches and potentially exhausting watcher resources.
Use a registry key that preserves the full device and inode values.| const int remaining = refCounts.value(key) - 1; | ||
| if (remaining > 0) { | ||
| refCounts[key] = remaining; | ||
| parked[key].append(monitor); |
There was a problem hiding this comment.
🟡 Medium · Repeated restarts accumulate monitors
When one watcher is repeatedly restarted while another watcher for the same inode stays active, DWatcher::start() first calls stop(), which parks the old monitor whenever another watcher for the inode remains active. Each subsequent restart creates another monitor, so the registry retains one more GFileMonitor per restart until the other watcher stops; a long-lived sibling therefore lets repeated restarts grow retained objects without bound.
Avoid retaining a new parked monitor for every restart, for example by deduplicating or reusing retained monitors for that inode.
Prompt for AI agents
In `src/dfm-io/dfm-io/dwatcher.cpp` at line 56:
**Repeated restarts accumulate monitors**
When one watcher is repeatedly restarted while another watcher for the same inode stays active, `DWatcher::start()` first calls `stop()`, which parks the old monitor whenever another watcher for the inode remains active. Each subsequent restart creates another monitor, so the registry retains one more `GFileMonitor` per restart until the other watcher stops; a long-lived sibling therefore lets repeated restarts grow retained objects without bound.
Avoid retaining a new parked monitor for every restart, for example by deduplicating or reusing retained monitors for that inode.|
deepin-community/glib2.0@dc7aa53 通过上游补丁解决 |
Added reference-counted registry to prevent GLib's inotify backend
from canceling shared watch descriptors when multiple paths point
to the same inode (via symlinks or bind mounts). Previously, if two
DWatcher instances monitored the same directory through different
paths (e.g., directory and symlink), stopping one would cancel the
underlying inotify watch, leaving the other watcher permanently deaf.
The new MonitorKeepAliveRegistry tracks active watchers per inode,
only canceling the shared monitor when the last watcher for that inode
stops. This ensures remaining watchers continue receiving events. Two
test cases verify both that stopping one path does not affect others
and that a fresh watcher works after all previous watchers for an inode
are stopped.
Influence:
one watcher
events
directory
work correctly
cancel without regression
fix: 修复停止同一inode路径之一后文件监视器失效的问题
添加了引用计数注册表,以防止在多个路径指向同一inode(通过符号链接或绑定
挂载)时,GLib的inotify后端取消共享监视描述符。之前,如果两个DWatcher实
例通过不同路径(例如目录和符号链接)监视同一目录,停止其中一个会取消底层
的inotify监视,导致另一个监视器永久失效。新的MonitorKeepAliveRegistry按
inode跟踪活动监视器,仅在该inode的最后一个监视器停止时才取消共享监视器。
这确保了剩余的监视器能继续接收事件。两个测试用例验证了停止一个路径不影响
其他路径,以及在该inode的所有先前监视器停止后,新的监视器仍能正常工作。
Influence:
Log:
PMS: Task-398995
Summary by Sourcery
Keep file watchers active when multiple paths reference the same inode by coordinating shared monitor lifetime.
Bug Fixes:
Enhancements:
Tests: