From 9e09d3496ca22cc944819dbb54ff12591a9ff05e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E5=AD=90=E6=81=92?= Date: Thu, 10 Jul 2025 19:06:12 +0800 Subject: [PATCH] watcher bugfix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 魏子恒 --- .../mod_fs/class_watcher/watcher_entity.cpp | 10 +++++ .../class_watcher/watcher_entity_test.cpp | 45 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index 89501e4e9..adb1db80e 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -150,6 +150,15 @@ int FileWatcher::CloseNotifyFdLocked() int FileWatcher::StopNotify(shared_ptr arg) { + while (true) { + lock_guard lock(readMutex_); + if (reading_) { + HILOGI("Watcher is reading, retry ReadMutex"); + } else { + break; + } + }; + unique_lock lock(watchMutex_); if (notifyFd_ < 0) { HILOGE("Failed to stop notify notifyFd_:%{public}d", notifyFd_); @@ -168,6 +177,7 @@ int FileWatcher::StopNotify(shared_ptr arg) lock_guard lock(readMutex_); if (!(closed_ && reading_)) { oldWd = inotify_rm_watch(notifyFd_, arg->wd); + HILOGE("rm watch failed, err: %{public}d", errno); } else { HILOGE("rm watch fail"); } diff --git a/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp b/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp index 5d5a50ed3..d72883f60 100644 --- a/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp @@ -16,6 +16,7 @@ #include "watcher_entity.h" #include +#include namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -105,4 +106,48 @@ HWTEST_F(WatcherEntityTest, WatcherEntityTest_CloseNotifyFd_003, testing::ext::T GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_CloseNotifyFd_003"; } +/** + * @tc.name: WatcherEntityTest_StopNotify_001 + * @tc.desc: Test function of WatcherEntityTest::StopNotify interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_StopNotify_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_StopNotify_001"; + + auto &watcher = FileWatcher::GetInstance(); + watcher.notifyFd_ = -1; + watcher.reading_ = false; + EXPECT_EQ(watcher.StopNotify(nullptr), 5); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_StopNotify_001"; +} + +/** + * @tc.name: WatcherEntityTest_StopNotify_002 + * @tc.desc: Test function of WatcherEntityTest::StopNotify interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_StopNotify_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_StopNotify_002"; + + auto &watcher = FileWatcher::GetInstance(); + watcher.notifyFd_ = -1; + watcher.reading_ = true; + std::thread recordThread([this, &watcher]() { + watcher.StopNotify(nullptr); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(600)); + watcher.reading_ = false; + recordThread.join(); + EXPECT_EQ(watcher.StopNotify(nullptr), 5); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_StopNotify_002"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee