Skip to content

fix: add memory check before opening files - #704

Draft
pppanghu77 wants to merge 1 commit into
linuxdeepin:masterfrom
pppanghu77:fix/pms-111510-memory-check
Draft

pppanghu77 wants to merge 1 commit into
linuxdeepin:masterfrom
pppanghu77:fix/pms-111510-memory-check

Conversation

@pppanghu77

@pppanghu77 pppanghu77 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

deepin-editor crashes (OOM-kill) when opening multiple large files. The root cause is that Window::addTab() and EditWrapper::openFile() load entire file content into memory (QByteArray + QTextDocument UTF-16 dual storage, ~5.68x memory amplification) without any pre-check on available system memory. When multiple large files are opened across tabs, memory consumption exceeds system limits, triggering the OOM-killer.

Fix

Added a pre-memory check using the existing Utils::isMemorySufficientForOperation() function (with RawOperation type) in both Window::addTab() (after readability check, before StartManager::checkPath()) and EditWrapper::openFile() (before creating FileLoadThread). When memory is insufficient, a warning message is shown and the file open is aborted instead of crashing.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Only adds boundary check + early return; no function signature changes, no public member changes
  • Reuses existing Utils::isMemorySufficientForOperation() already used in copy/paste paths — consistent with existing patterns
  • No historical fix reverted; target lines are not products of previous bug fixes

Business Impact Scope

  • File Open: Files are blocked from opening when system memory is insufficient, showing a warning instead of crashing
  • All file open paths covered: addTab() (primary entry) and openFile() (safety net for other callers like addTemFileTab)
  • Normal file opening (sufficient memory) is unaffected

Verification Suggestion

Test opening large files under low memory conditions to verify the warning appears and no crash occurs. Verify normal file opening is unaffected.


根因分析

deepin-editor 打开多个大文件时崩溃(OOM-kill)。根因是 Window::addTab() 和 EditWrapper::openFile() 在加载文件时将整个文件内容读入内存(QByteArray + QTextDocument UTF-16 双份存储,约 5.68 倍内存放大),未做可用内存前置检查。多标签页叠加打开大文件时,内存消耗超过系统限制,触发 OOM-killer。

修复方案

在 Window::addTab()(可读性检查之后、StartManager::checkPath() 之前)和 EditWrapper::openFile()(创建 FileLoadThread 之前)两个入口点添加前置内存检查,复用已有的 Utils::isMemorySufficientForOperation() 函数(RawOperation 类型)。内存不足时显示警告提示并中止打开,而非崩溃。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 仅添加边界检查 + early return,无函数签名变更,无公开成员变更
  • 复用已有 Utils::isMemorySufficientForOperation(),与复制/粘贴路径的内存检查模式一致
  • 未撤销任何历史修复,目标行非历史修复产物

业务影响范围

  • 文件打开: 内存不足时阻止文件打开,显示警告而非崩溃
  • 覆盖所有文件打开路径:addTab()(主入口)和 openFile()(覆盖 addTemFileTab 等其他调用方)
  • 正常文件打开(内存充足)不受影响

验证建议

测试低内存条件下打开大文件,验证提示出现且不崩溃。验证正常文件打开不受影响。

Summary by Sourcery

Prevent out-of-memory crashes by checking available memory before loading files.

Bug Fixes:

  • Prevent opening files when available memory is insufficient, avoiding out-of-memory crashes and displaying a warning to the user.

Enhancements:

  • Apply memory checks across both primary and secondary file-opening paths while preserving normal file opening when sufficient memory is available.

1. Root cause: Window::addTab() and EditWrapper::openFile() load
   entire file content into memory without checking available RAM,
   causing OOM-kill when opening multiple large files
2. Fix: call existing Utils::isMemorySufficientForOperation() with
   RawOperation type before file loading in both entry points,
   show warning and abort if memory is insufficient
3. Impact: files are blocked from opening when memory is low instead
   of crashing; no behavior change when memory is sufficient

Log: Prevent crash when opening large files with insufficient memory

Influence:
1. Test opening large files under low memory conditions
2. Verify normal file opening is not affected
3. Test opening multiple files simultaneously

fix: 打开文件前增加内存检查

1. 根因:Window::addTab() 和 EditWrapper::openFile() 在加载文件时
   将整个文件内容读入内存,未检查可用内存,打开多个大文件时触发
   OOM-kill 崩溃
2. 方案:在两个入口点加载文件前调用已有的
   Utils::isMemorySufficientForOperation()(RawOperation 类型),
   内存不足时显示警告并中止打开
3. 影响:内存不足时阻止文件打开而非崩溃;内存充足时行为不变

Log: 打开大文件时内存不足改为提示而非崩溃

Influence:
1. 测试低内存条件下打开大文件
2. 验证正常文件打开不受影响
3. 测试同时打开多个文件

PMS: BUG-111510
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pppanghu77

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

@sourcery-ai

sourcery-ai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Reviewer's Guide

Prevents OOM-related crashes when opening large files by reusing the existing memory check at both the tab-level and editor-level open paths, warning users and returning early when available memory is insufficient while leaving normal opens unchanged.

Sequence diagram for memory-checked file opening

sequenceDiagram
    actor User
    participant Window
    participant Utils
    participant EditWrapper
    participant FileLoadThread
    participant MessageManager

    User->>Window: addTab(filepath, activeTab)
    Window->>Utils: isMemorySufficientForOperation(RawOperation, fileSize, 0)
    alt memory insufficient
        Window->>MessageManager: sendMessage(...)
        Window-->>User: Open aborted with warning
    else memory sufficient
        Window->>EditWrapper: openFile(filepath, ...)
        EditWrapper->>Utils: isMemorySufficientForOperation(RawOperation, fileSize, 0)
        alt memory insufficient
            EditWrapper->>MessageManager: sendMessage(...)
            EditWrapper-->>User: Open aborted with warning
        else memory sufficient
            EditWrapper->>FileLoadThread: FileLoadThread(filepath)
            FileLoadThread-->>User: File loading proceeds
        end
    end
Loading

File-Level Changes

Change Details Files
Add early memory-capacity checks to both file-opening entry points before allocating file-loading resources.
  • Check the target file size with the existing RawOperation memory-sufficiency utility.
  • Log a warning, display a user-facing insufficient-memory message, and abort opening when the check fails.
  • Keep the primary tab-opening check after readability validation and add a lower-level safety check before creating FileLoadThread.
src/editor/editwrapper.cpp
src/widgets/window.cpp

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

@pppanghu77
pppanghu77 marked this pull request as draft October 29, 2026 19:31

@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 reviewed your changes and they look great!


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

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