Skip to content

fix: destroy DOcr engine on quit to avoid SIGSEGV - #289

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:agent/bugfix/c3ba44e4e7d7
Oct 21, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:agent/bugfix/c3ba44e4e7d7

Conversation

@Johnson-zs

@Johnson-zs Johnson-zs commented Oct 21, 2026 •

Copy link
Copy Markdown

fix: destroy DOcr engine on quit to avoid SIGSEGV

  1. Root cause: sharedOcrEngine() creates a DOcr singleton that is
    never destroyed; on process exit DOcr's internal worker threads
    still have posted events targeting deleted QObjects, causing
    SIGSEGV in sendPostedEvents
  2. Fix: connect to QCoreApplication::aboutToQuit to delete the DOcr
    engine before process exit, ensuring internal worker threads are
    stopped and event queues drained
  3. Impact: only affects process exit path, no change to normal OCR
    extraction functionality

Log: Fixed SIGSEGV crash on extractor process exit

Influence:

  1. Test extractor exit on idle timeout, verify no SIGSEGV
  2. Test extractor exit on stdin close, verify no SIGSEGV
  3. Verify normal OCR image extraction still works correctly

fix: 退出时销毁DOcr引擎避免段错误

  1. 根因:sharedOcrEngine()创建的DOcr单例永不销毁,进程退出时
    DOcr内部工作线程事件队列中残留已投递事件,其接收者QObject已
    被销毁,sendPostedEvents派发到悬垂指针导致SIGSEGV
  2. 方案:连接QCoreApplication::aboutToQuit信号,在进程退出前
    销毁DOcr引擎,确保内部工作线程正确停止并清空事件队列
  3. 影响:仅影响进程退出路径,不影响正常OCR提取功能

Log: 修复extractor进程退出时的段错误崩溃

Influence:

  1. 测试extractor空闲超时退出,确认无段错误
  2. 测试extractor stdin关闭退出,确认无段错误
  3. 验证正常OCR图片提取功能不受影响

Summary by Sourcery

Bug Fixes:

  • Prevent extractor shutdown SIGSEGV crashes by destroying the shared OCR engine before application exit.

1. Root cause: sharedOcrEngine() creates a DOcr singleton that is
   never destroyed; on process exit DOcr's internal worker threads
   still have posted events targeting deleted QObjects, causing
   SIGSEGV in sendPostedEvents
2. Fix: connect to QCoreApplication::aboutToQuit to delete the DOcr
   engine before process exit, ensuring internal worker threads are
   stopped and event queues drained
3. Impact: only affects process exit path, no change to normal OCR
   extraction functionality

Log: Fixed SIGSEGV crash on extractor process exit

Influence:
1. Test extractor exit on idle timeout, verify no SIGSEGV
2. Test extractor exit on stdin close, verify no SIGSEGV
3. Verify normal OCR image extraction still works correctly

fix: 退出时销毁DOcr引擎避免段错误

1. 根因:sharedOcrEngine()创建的DOcr单例永不销毁,进程退出时
   DOcr内部工作线程事件队列中残留已投递事件,其接收者QObject已
   被销毁,sendPostedEvents派发到悬垂指针导致SIGSEGV
2. 方案:连接QCoreApplication::aboutToQuit信号,在进程退出前
   销毁DOcr引擎,确保内部工作线程正确停止并清空事件队列
3. 影响:仅影响进程退出路径,不影响正常OCR提取功能

Log: 修复extractor进程退出时的段错误崩溃

Influence:
1. 测试extractor空闲超时退出,确认无段错误
2. 测试extractor stdin关闭退出,确认无段错误
3. 验证正常OCR图片提取功能不受影响
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs

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 21, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The PR fixes extractor shutdown crashes by registering the lazily created shared DOcr engine for deletion when QCoreApplication emits aboutToQuit, preventing worker-thread events from targeting destroyed QObjects while leaving normal OCR extraction behavior unchanged.

Sequence diagram for safe OCR engine shutdown

sequenceDiagram
    participant Extractor
    participant QCoreApplication
    participant DOcr
    participant WorkerThreads

    Extractor->>DOcr: sharedOcrEngine()
    DOcr-->>Extractor: return engine
    Extractor->>QCoreApplication: connect(aboutToQuit, delete engine)
    QCoreApplication-->>QCoreApplication: aboutToQuit
    QCoreApplication->>DOcr: delete engine
    DOcr->>WorkerThreads: stop internal worker threads
    DOcr->>WorkerThreads: drain posted event queues
    WorkerThreads-->>QCoreApplication: shutdown safely
Loading

Flow diagram for extractor exit crash prevention

flowchart TD
    A[Extractor begins shutdown] --> B[QCoreApplication emits aboutToQuit]
    B --> C[Delete shared DOcr engine]
    C --> D[Stop worker threads and drain event queues]
    D --> E[Process exits without SIGSEGV]
Loading

File-Level Changes

Change Details Files
Ensure the shared OCR engine is destroyed during application shutdown to prevent exit-time SIGSEGVs.
  • Include QCoreApplication for shutdown lifecycle handling.
  • Connect aboutToQuit to a cleanup lambda that deletes the singleton DOcr engine.
  • Allow DOcr worker threads to stop and drain pending events before process termination.
src/extractor/plugins/ocr-extractor/ocrextractor.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

@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 ✨

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 100 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交为修复 SIGSEGV 崩溃的 Bug 修复,在 sharedOcrEngine() 中通过监听 QCoreApplication::aboutToQuit 信号,在应用退出时销毁 DOcr 引擎,使其内部 工作线程的事件队列在进程退出前被正确清理。改动范围小、注释完整、逻辑正确, 启发式扫描、AI 四维度分析与 OCR 审查均未发现安全漏洞或质量缺陷。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['当前实现已能正确解决 SIGSEGV 问题,无需额外修改。']


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['建议保持当前注释风格,便于后续维护者理解退出时销毁引擎的必要性。']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['无需性能优化。']


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: ['无需安全加固。']


💡 改进建议代码示例

// 当前实现已为正确修复方式,无需改动。完整示例: // // #include <QCoreApplication> // // Dtk::Ocr::DOcr *sharedOcrEngine() // { //     static Dtk::Ocr::DOcr *engine = []() { //         auto *e = new Dtk::Ocr::DOcr(); //         if (!e->loadEngine()) { //             delete e; //             return nullptr; //         } //         QObject::connect(qApp, &QCoreApplication::aboutToQuit, qApp, [e]() { //             delete e; //         }); //         return e; //     }(); //     return engine; // }

本报告由 AI 代码审查工具自动生成

@Johnson-zs

Copy link
Copy Markdown
Author

/forcemerge

@deepin-bot
deepin-bot Bot merged commit 746bf3f into linuxdeepin:develop/snipe Oct 21, 2026
17 checks passed
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