Skip to content

fix: skip fsync on network filesystems to prevent UI freeze - #451

Draft
dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:agent/bugfix/380239
Draft

dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:agent/bugfix/380239

Conversation

@dengzhongyuan365-dev

@dengzhongyuan365-dev dengzhongyuan365-dev commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Root Cause Analysis

DPdfDoc::save() and DPdfDoc::saveAs() call fsync() synchronously on the Qt main thread. On local filesystems fsync is negligible (~3ms measured), but on SMB/CIFS mounts fsync triggers a network round-trip to the SMB server (SMB SET_FILE_INFO + wait for server flush confirmation), blocking the event loop for seconds or longer, causing the document viewer UI to freeze completely. The save is triggered by a 100ms auto-save timer after annotation changes and on document close.

Key evidence: blocking point at 3rdparty/deepin-pdfium/src/dpdfdoc.cpp:293 (save) and :353 (saveAs); reproduction with FUSE-simulated slow fsync showed ~3.2s UI freeze before fix, ~3ms after skipping fsync.

Fix

Added a file-scope static helper isOnNetworkFileSystem(int fd) using fstatfs() to detect SMB/CIFS filesystem types (SMB_SUPER_MAGIC, SMB2_SUPER_MAGIC, CIFS_MAGIC_NUMBER). Both save() and saveAs() now skip fsync() on network filesystems while keeping it on local filesystems to preserve crash durability (BUG-88915/BUG-351271).

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • isOnNetworkFileSystem is a file-scope static function with no external callers; change-safety-raw.json reference list is empty (reference_count=0)
  • Historical fixes preserved: BUG-88915 (power-loss durability) keeps fsync on local FS; BUG-351271 (SMB save failure) rename-to-backup mode unchanged; BUG-370563 (NAME_MAX overflow) UUID-only backup naming unchanged

Business Impact Scope

  • PDF file save — auto-save after annotation changes and save-on-close
  • Affected callers: DPdfDoc::save() (dpdfdoc.cpp:241), DPdfDoc::saveAs() (dpdfdoc.cpp:330)
  • Local filesystem behavior unchanged; network filesystem saves no longer block the UI

Verification Suggestion

  • Test annotation save on local filesystem (regression for BUG-88915/BUG-351271)
  • Test annotation save on SMB/CIFS mount (verify no UI freeze)
  • Power-loss durability test on local filesystem

根因分析

DPdfDoc::save() 和 DPdfDoc::saveAs() 在 Qt 主线程同步调用 fsync()。在本地文件系统上 fsync 耗时可忽略(实测 ~3ms),但在 SMB/CIFS 网络挂载上 fsync 触发到 SMB 服务器的网络往返(SMB SET_FILE_INFO + 等待服务器确认刷盘),阻塞事件循环数秒甚至更久,导致文档查看器界面完全冻结。保存动作在添加注释后 100ms 自动保存定时器触发及关闭文档时触发。

关键证据:阻塞点位于 3rdparty/deepin-pdfium/src/dpdfdoc.cpp:293(save)和 :353(saveAs);复现实验用 FUSE 模拟慢速 fsync,修复前 ~3.2s UI 冻结,跳过 fsync 后 ~3ms。

修复方案

新增 file-scope static 函数 isOnNetworkFileSystem(int fd),使用 fstatfs() 检测 SMB/CIFS 文件系统类型(SMB_SUPER_MAGIC、SMB2_SUPER_MAGIC、CIFS_MAGIC_NUMBER),在 save() 和 saveAs() 两处 fsync 调用前判断:网络文件系统跳过 fsync,本地文件系统保留 fsync 以维持崩溃持久性(BUG-88915/BUG-351271)。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • isOnNetworkFileSystem 为 file-scope static 函数,无外部调用者;change-safety-raw.json 引用列表为空(reference_count=0)
  • 历史修复不受影响:BUG-88915(断电损坏)本地 fsync 保留;BUG-351271(SMB 保存失败)rename-to-backup 模式不变;BUG-370563(NAME_MAX 溢出)UUID-only 命名不变

业务影响范围

  • PDF 文件保存功能 — 添加注释后自动保存、关闭文档时保存
  • 涉及调用者:DPdfDoc::save()(dpdfdoc.cpp:241)、DPdfDoc::saveAs()(dpdfdoc.cpp:330)
  • 本地文件系统行为不变;网络文件系统保存不再阻塞界面

验证建议

  • 本地文件系统添加 PDF 注释后保存(回归 BUG-88915/BUG-351271)
  • SMB/CIFS 网络路径添加 PDF 注释后自动保存(验证不再卡死)
  • 断电/强制关机场景下本地保存的 PDF 文件完整性

Summary by Sourcery

Avoid UI freezes during PDF saves by skipping fsync on network filesystems while retaining local filesystem durability.

Bug Fixes:

  • Prevent PDF saves on network-mounted filesystems from blocking the UI by skipping synchronous fsync operations on detected network paths.

Enhancements:

  • Preserve fsync-based durability for local filesystem saves while recognizing GVFS, SMB, CIFS, and related network paths.

Tests:

  • Add unit coverage for local, root, temporary, and GVFS path filesystem classification.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dengzhongyuan365-dev
dengzhongyuan365-dev marked this pull request as draft November 13, 2026 18:32
@sourcery-ai

sourcery-ai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Detect network filesystems and skip synchronous fsync during saves there while retaining fsync for local filesystems.

  • 3rdparty/deepin-pdfium/src/dpdfdoc.cpp L236-248 (+3 small edits)

Tests

Add unit tests for network-filesystem detection on GVFS and local paths.

  • tests/document/ut_networkfilesystem.cpp L1-57

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="3rdparty/deepin-pdfium/src/dpdfdoc.cpp" line_range="325-326" />
<code_context>

     newFile.flush(); // Flush user buffer to kernel buffer
-    fsync(newFile.handle()); // Sync kernel buffer to disk
+    if (!isOnNetworkFileSystem(newFile.handle()))
+        fsync(newFile.handle()); // Sync kernel buffer to disk (skip on network FS)
     newFile.close();

</code_context>
<issue_to_address>
**Saved changes disappear after a crash**

When the network filesystem has acknowledged writes into a cache but has not committed them to stable storage, `save()` and `saveAs()` skip `fsync()` and can return success before the writes are durable; `save()` then removes its backup, so a client or server crash can lose the latest PDF changes.

Ensure the replacement reaches stable storage before reporting success or removing the backup, using a durability mechanism that works for network filesystems.

Also at `3rdparty/deepin-pdfium/src/dpdfdoc.cpp:353-354`.
</issue_to_address>

### Comment 2
<location path="3rdparty/deepin-pdfium/src/dpdfdoc.cpp" line_range="31" />
<code_context>
+static bool isOnNetworkFileSystem(int fd)
+{
+    struct statfs buf;
+    if (fstatfs(fd, &buf) != 0)
+        return false;
+
</code_context>
<issue_to_address>
**SMB saves still freeze the UI**

When an SMB server is slow or unresponsive during the filesystem-stat lookup, `isOnNetworkFileSystem()` calls synchronous `fstatfs()` before the guarded `fsync()`, so the save blocks the caller and freezes the UI.

Avoid performing a potentially blocking remote `fstatfs()` lookup on the save thread.
</issue_to_address>

### Comment 3
<location path="3rdparty/deepin-pdfium/src/dpdfdoc.cpp" line_range="34-36" />
<code_context>
+    if (fstatfs(fd, &buf) != 0)
+        return false;
+
+    return buf.f_type == SMB_SUPER_MAGIC
+        || buf.f_type == SMB2_SUPER_MAGIC
+        || buf.f_type == 0xFF534D42; // CIFS_MAGIC_NUMBER (not in userspace headers)
+}

</code_context>
<issue_to_address>
**Network saves still freeze the UI**

When a document is saved on NFS or another network filesystem not identified by the SMB/CIFS magic values, or `fstatfs()` fails, `isOnNetworkFileSystem()` returns false, so `save()` and `saveAs()` still call synchronous `fsync()` and can freeze the UI.

Handle unrecognized and unclassifiable network filesystems so their saves do not perform a blocking synchronous `fsync()` on the UI thread.

Also at `3rdparty/deepin-pdfium/src/dpdfdoc.cpp:31-33`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread 3rdparty/deepin-pdfium/src/dpdfdoc.cpp Outdated
Comment on lines +325 to +326
if (!isOnNetworkFileSystem(newFile.handle()))
fsync(newFile.handle()); // Sync kernel buffer to disk (skip on network FS)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Saved changes disappear after a crash

When the network filesystem has acknowledged writes into a cache but has not committed them to stable storage, save() and saveAs() skip fsync() and can return success before the writes are durable; save() then removes its backup, so a client or server crash can lose the latest PDF changes.

Ensure the replacement reaches stable storage before reporting success or removing the backup, using a durability mechanism that works for network filesystems.

Also at 3rdparty/deepin-pdfium/src/dpdfdoc.cpp:353-354.

Prompt for AI agents
In `3rdparty/deepin-pdfium/src/dpdfdoc.cpp` at lines 325-326:

**Saved changes disappear after a crash**

When the network filesystem has acknowledged writes into a cache but has not committed them to stable storage, `save()` and `saveAs()` skip `fsync()` and can return success before the writes are durable; `save()` then removes its backup, so a client or server crash can lose the latest PDF changes.

Ensure the replacement reaches stable storage before reporting success or removing the backup, using a durability mechanism that works for network filesystems.

Also at `3rdparty/deepin-pdfium/src/dpdfdoc.cpp:353-354`.

Comment thread 3rdparty/deepin-pdfium/src/dpdfdoc.cpp Outdated
static bool isOnNetworkFileSystem(int fd)
{
struct statfs buf;
if (fstatfs(fd, &buf) != 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · SMB saves still freeze the UI

When an SMB server is slow or unresponsive during the filesystem-stat lookup, isOnNetworkFileSystem() calls synchronous fstatfs() before the guarded fsync(), so the save blocks the caller and freezes the UI.

Avoid performing a potentially blocking remote fstatfs() lookup on the save thread.

Prompt for AI agents
In `3rdparty/deepin-pdfium/src/dpdfdoc.cpp` at line 31:

**SMB saves still freeze the UI**

When an SMB server is slow or unresponsive during the filesystem-stat lookup, `isOnNetworkFileSystem()` calls synchronous `fstatfs()` before the guarded `fsync()`, so the save blocks the caller and freezes the UI.

Avoid performing a potentially blocking remote `fstatfs()` lookup on the save thread.

Comment thread 3rdparty/deepin-pdfium/src/dpdfdoc.cpp Outdated
Comment on lines +34 to +36
return buf.f_type == SMB_SUPER_MAGIC
|| buf.f_type == SMB2_SUPER_MAGIC
|| buf.f_type == 0xFF534D42; // CIFS_MAGIC_NUMBER (not in userspace headers)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Network saves still freeze the UI

When a document is saved on NFS or another network filesystem not identified by the SMB/CIFS magic values, or fstatfs() fails, isOnNetworkFileSystem() returns false, so save() and saveAs() still call synchronous fsync() and can freeze the UI.

Handle unrecognized and unclassifiable network filesystems so their saves do not perform a blocking synchronous fsync() on the UI thread.

Also at 3rdparty/deepin-pdfium/src/dpdfdoc.cpp:31-33.

Prompt for AI agents
In `3rdparty/deepin-pdfium/src/dpdfdoc.cpp` at lines 34-36:

**Network saves still freeze the UI**

When a document is saved on NFS or another network filesystem not identified by the SMB/CIFS magic values, or `fstatfs()` fails, `isOnNetworkFileSystem()` returns false, so `save()` and `saveAs()` still call synchronous `fsync()` and can freeze the UI.

Handle unrecognized and unclassifiable network filesystems so their saves do not perform a blocking synchronous `fsync()` on the UI thread.

Also at `3rdparty/deepin-pdfium/src/dpdfdoc.cpp:31-33`.

@github-actions

Copy link
Copy Markdown

TAG Bot

TAG: 6.5.70
EXISTED: no
DISTRIBUTION: unstable

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants