From 927baf3b9e314a9d815b778a9e6467e8a2239200 Mon Sep 17 00:00:00 2001 From: dengzhongyuan365-dev Date: Fri, 20 Nov 2026 22:47:13 +0800 Subject: [PATCH] fix: skip fsync on network filesystem paths to prevent UI freeze MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: DPdfDoc::save() and saveAs() call fsync() on the main thread for files on SMB/CIFS network mounts, blocking the Qt event loop for seconds while waiting for remote disk sync 2. Fix: add isNetworkFileSystem() helper using QStorageInfo to detect cifs/smb/fuse.gvfs paths, skip fsync for network paths while keeping flush() for userspace-to-kernel buffer transfer 3. Impact: local disk save behavior unchanged (fsync still called); network path save no longer blocks UI; temp-file-then-rename preserves atomicity and backup recovery Log: 修复SMB网络路径保存PDF时文档查看器卡死问题 Influence: 1. Test saving PDF with bookmarks/annotations on local disk 2. Test saving PDF on SMB mount and verify no UI freeze 3. Test saveAs to SMB path 4. Test save failure recovery on network disconnect fix: 跳过网络文件系统路径的fsync以防止界面卡死 1. 根因:DPdfDoc::save()和saveAs()在主线程对SMB/CIFS网络挂载 路径的文件调用fsync(),等待远端刷盘时阻塞Qt事件循环数秒 2. 方案:新增isNetworkFileSystem()辅助函数,使用QStorageInfo检测 cifs/smb/fuse.gvfs路径,网络路径跳过fsync,保留flush()完成 用户态到内核态的缓冲刷入 3. 影响:本地磁盘保存行为不变(仍执行fsync);网络路径保存不再 阻塞界面;temp-file-then-rename保证原子性和备份恢复 Log: 修复SMB网络路径保存PDF时文档查看器卡死问题 Influence: 1. 测试本地磁盘上保存带书签/注释的PDF 2. 测试SMB挂载路径保存PDF,验证不再卡死 3. 测试另存为到SMB路径 4. 测试网络断开时保存失败的恢复 PMS: BUG-380239 --- 3rdparty/deepin-pdfium/src/dpdfdoc.cpp | 20 ++++++++- tests/document/ut_networkfilesystem.cpp | 57 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/document/ut_networkfilesystem.cpp diff --git a/3rdparty/deepin-pdfium/src/dpdfdoc.cpp b/3rdparty/deepin-pdfium/src/dpdfdoc.cpp index d1e22586..fd668b0a 100755 --- a/3rdparty/deepin-pdfium/src/dpdfdoc.cpp +++ b/3rdparty/deepin-pdfium/src/dpdfdoc.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include /** @@ -232,6 +233,19 @@ bool DPdfDoc::isLinearized(const QString &fileName) static QFile saveWriter; +static bool isNetworkFileSystem(const QString &path) +{ + if (path.contains(QLatin1String("/gvfs/"))) + return true; + + QStorageInfo storage(path); + QByteArray fsType = storage.fileSystemType(); + if (fsType.contains("cifs") || fsType.contains("smb") || fsType.contains("fuse.gvfs")) + return true; + + return false; +} + int writeFile(struct FPDF_FILEWRITE_* pThis, const void *pData, unsigned long size) { Q_UNUSED(pThis) @@ -309,7 +323,8 @@ bool DPdfDoc::save() } newFile.flush(); // Flush user buffer to kernel buffer - fsync(newFile.handle()); // Sync kernel buffer to disk + if (!isNetworkFileSystem(targetPath)) + fsync(newFile.handle()); // Sync kernel buffer to disk (skip on network FS to avoid UI freeze) newFile.close(); // Remove backup file after successful write @@ -336,7 +351,8 @@ bool DPdfDoc::saveAs(const QString &filePath) locker.unlock(); saveWriter.flush(); // Flush user buffer to kernel buffer - fsync(saveWriter.handle()); // Sync kernel buffer to disk + if (!isNetworkFileSystem(filePath)) + fsync(saveWriter.handle()); // Sync kernel buffer to disk (skip on network FS to avoid UI freeze) saveWriter.close(); return result; diff --git a/tests/document/ut_networkfilesystem.cpp b/tests/document/ut_networkfilesystem.cpp new file mode 100644 index 00000000..2343f1d1 --- /dev/null +++ b/tests/document/ut_networkfilesystem.cpp @@ -0,0 +1,57 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include + +#include +#include +#include +#include + +// Replicate the static helper from dpdfdoc.cpp for unit testing. +// The original isNetworkFileSystem() is file-local (static) in +// 3rdparty/deepin-pdfium/src/dpdfdoc.cpp and cannot be linked from +// test code; this copy verifies the decision logic. +static bool isNetworkFileSystem(const QString &path) +{ + if (path.contains(QLatin1String("/gvfs/"))) + return true; + + QStorageInfo storage(path); + QByteArray fsType = storage.fileSystemType(); + if (fsType.contains("cifs") || fsType.contains("smb") || fsType.contains("fuse.gvfs")) + return true; + + return false; +} + +TEST(TestNetworkFileSystem, LocalPathIsNotNetwork) +{ + QTemporaryFile tmp; + ASSERT_TRUE(tmp.open()); + EXPECT_FALSE(isNetworkFileSystem(tmp.fileName())); +} + +TEST(TestNetworkFileSystem, GvfsPathIsNetwork) +{ + QString gvfsPath = QStringLiteral("/run/user/1000/gvfs/smb-share:server=fileserver,share=public/doc.pdf"); + EXPECT_TRUE(isNetworkFileSystem(gvfsPath)); +} + +TEST(TestNetworkFileSystem, RootIsNotNetwork) +{ + EXPECT_FALSE(isNetworkFileSystem(QStringLiteral("/"))); +} + +TEST(TestNetworkFileSystem, TmpDirIsNotNetwork) +{ + EXPECT_FALSE(isNetworkFileSystem(QStringLiteral("/tmp"))); +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}