From 15cbcd6ab80baa0c3e8e77280d1c502779c05b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E5=AD=90=E6=81=92?= Date: Mon, 21 Jul 2025 10:08:19 +0800 Subject: [PATCH] watcher bug fix 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 | 8 +--- .../class_watcher/watcher_entity_test.cpp | 44 ------------------- 2 files changed, 1 insertion(+), 51 deletions(-) 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 7aa1644f1..187683a0f 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,13 +150,6 @@ int FileWatcher::CloseNotifyFdLocked() int FileWatcher::StopNotify(shared_ptr arg) { - while (true) { - lock_guard lock(readMutex_); - if (!reading_) { - break; - } - }; - unique_lock lock(watchMutex_); if (notifyFd_ < 0) { HILOGE("Failed to stop notify notifyFd_:%{public}d", notifyFd_); @@ -231,6 +224,7 @@ void FileWatcher::ReadNotifyEventLocked(WatcherCallback callback) } ReadNotifyEvent(callback); { + lock_guard watcherLock(watchMutex_); lock_guard lock(readMutex_); reading_ = false; if (closed_) { 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 d72883f60..04dd5cd0f 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 @@ -106,48 +106,4 @@ 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