Skip to content

Fix[denumerator]: close GFileEnumerator when enumeration finishes#281

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
GongHeng2017:202604231043-dev-eagle-fix
Apr 29, 2026
Merged

Fix[denumerator]: close GFileEnumerator when enumeration finishes#281
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
GongHeng2017:202604231043-dev-eagle-fix

Conversation

@GongHeng2017
Copy link
Copy Markdown
Contributor

Close and release GFileEnumerator after all entries are consumed to prevent file descriptor leak on GVFS mount points (e.g. Vault).

枚举完成后关闭并释放GFileEnumerator,防止GVFS挂载点(如保险箱)的文件描述符泄漏。

Log: 修复枚举器耗尽后fd未释放的问题
Influence: 修复遍历Vault等GVFS目录后文件描述符一直被占用的问题,枚举完成后正确释放资源。

@GongHeng2017 GongHeng2017 force-pushed the 202604231043-dev-eagle-fix branch 5 times, most recently from c2115c1 to c01c699 Compare April 28, 2026 13:32
Comment thread include/dfm-io/dfm-io/denumerator.h Outdated
@GongHeng2017 GongHeng2017 force-pushed the 202604231043-dev-eagle-fix branch from c01c699 to f31b10f Compare April 28, 2026 13:42
@GongHeng2017 GongHeng2017 force-pushed the 202604231043-dev-eagle-fix branch from f31b10f to 25d69d1 Compare April 28, 2026 13:50
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码修改主要涉及文件枚举器(DEnumerator)的资源管理和错误处理逻辑。以下是对该 diff 的详细审查意见:

1. 语法逻辑

审查结果:通过

  • 代码语法正确,符合 C++ 和 GLib/GIO 的 API 使用规范。
  • 逻辑流程清晰:处理错误 -> 处理完成 -> 释放资源。

2. 代码质量

审查结果:良好,但有改进空间

  • 优点

    • 将错误处理逻辑移至 nextInfo == NULL 的判断块内,逻辑更加紧凑。
    • 在枚举完成时显式关闭枚举器并释放资源,这是一个好的实践。
    • 在发生错误时重置 d->nextUrld->dfileInfoNext,避免残留数据。
  • 改进建议

    • 错误处理逻辑的变更:原代码在 gerror 发生时会立即处理并释放错误对象,而新代码将其移到了 nextInfo == NULL 的判断块内。需要确认这是否符合预期行为,因为原代码可能在 nextInfo 有效但 gerror 也存在的情况下处理错误(虽然这种情况较少见)。
    • 资源释放的时机:在 hasNext() 中释放枚举器资源可能会导致状态不一致,建议在析构函数或显式的 close() 方法中释放资源,以确保资源释放的时机可控。

3. 代码性能

审查结果:无显著影响

  • 新增的 g_file_enumerator_closeg_object_unref 调用会增加少量开销,但这是必要的资源管理操作,不会对整体性能产生负面影响。
  • d->stackEnumerator.pop()QUrl() 的重置操作开销很小,可以忽略。

4. 代码安全

审查结果:良好,但需注意线程安全

  • 资源泄漏风险

    • 新代码在 gerror 发生时没有释放 gerror 对象,可能导致内存泄漏。建议在 d->setErrorFromGError(gerror) 后添加 g_error_free(gerror)
    • 原代码在 gerror 发生时会释放错误对象,而新代码没有,这是潜在的内存泄漏点。
  • 线程安全

    • d->stackEnumerator 是一个栈结构,如果 hasNext() 可能被多线程调用,需要确保线程安全(例如通过互斥锁保护)。
  • 空指针检查

    • g_file_enumerator_close(enumerator, nullptr, nullptr) 的第二个参数是 GCancellable*,如果需要支持取消操作,建议传入有效的 GCancellable 对象。

改进后的代码建议

if (gerror) {
    d->setErrorFromGError(gerror);
    g_error_free(gerror);  // 释放错误对象
    gerror = nullptr;
    d->nextUrl = QUrl();
    d->dfileInfoNext.reset();
    return true;
}

// 枚举完成,关闭并释放当前枚举器,释放 fd
if (!d->stackEnumerator.isEmpty()) {
    GFileEnumerator *enumerator = d->stackEnumerator.pop();
    if (enumerator) {
        g_file_enumerator_close(enumerator, nullptr, nullptr);
        g_object_unref(enumerator);
    }
}

总结

  • 主要问题:新代码在 gerror 发生时没有释放错误对象,可能导致内存泄漏。
  • 建议:在 d->setErrorFromGError(gerror) 后添加 g_error_free(gerror)
  • 其他:资源释放逻辑合理,但需注意线程安全和取消操作的支持。

Close and release GFileEnumerator after all entries are consumed
to prevent file descriptor leak on GVFS mount points (e.g. Vault).

枚举完成后关闭并释放GFileEnumerator,防止GVFS挂载点(如保险箱)的文件描述符泄漏。

Log: 修复枚举器耗尽后fd未释放的问题
Bug: https://pms.uniontech.com/bug-view-358881.html
Influence: 修复遍历Vault等GVFS目录后文件描述符一直被占用的问题,枚举完成后正确释放资源。
@GongHeng2017 GongHeng2017 force-pushed the 202604231043-dev-eagle-fix branch from 25d69d1 to 6b5529c Compare April 29, 2026 01:14
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017, liyigang1, max-lvs

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

@GongHeng2017
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown

deepin-bot Bot commented Apr 29, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 66ea1a8 into linuxdeepin:develop/eagle Apr 29, 2026
19 of 21 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.

4 participants