Skip to content

fix(index): remove redundant setsockopt that shrinks receive buffer - #285

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
wangrong1069:agent/pms-bug-bot/f7d6ac00c07c
Oct 15, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
wangrong1069:agent/pms-bug-bot/f7d6ac00c07c

Conversation

@wangrong1069

@wangrong1069 wangrong1069 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

The dispatcher (event_relay_dispatcher.c) already sets the socketpair receive buffer to 8 MiB via SO_RCVBUFFORCE (with SO_RCVBUF fallback) before passing the fd through D-Bus. The client's connectDispatcherSocket() re-issued setsockopt(SO_RCVBUF, 8MB) on the same fd, which overrides the server's setting with a value capped by rmem_max — on systems where rmem_max < 8 MiB (common default 4 MiB) this shrinks the buffer from the intended 8 MiB down to rmem_max, negating the server's effort. Key evidence: vfsmonitorwatcher.cpp:604-609 (redundant client-side setsockopt) and event_relay_dispatcher.c:110-118 (server-side SO_RCVBUFFORCE setting).

Fix

Removed the redundant setsockopt(SO_RCVBUF) block and the now-unused kReceiveBufSize constant from connectDispatcherSocket(). The server-side SO_RCVBUFFORCE ensures the 8 MiB buffer survives across the D-Bus fd handoff without client intervention. Implementation matches the analysis recommendation with no deviation.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Only removes a redundant setsockopt block; no change to function signature, return value, or control flow. Callers initDispatcher() and attemptReconnect() only check fd < 0 and are unaffected.
  • Historical insight: the removed code was introduced in commit 36b42fb5 (PMS 370779, enlarge buffers to prevent EAGAIN disconnection). After the D-Bus event relay integration (08f68770), the server already sets SO_RCVBUFFORCE(8MB), making the client call redundant and harmful. This fix better achieves 370779's goal by ensuring the 8 MiB buffer is not shrunk.

Business Impact Scope

Affected module: VFS file monitoring — the client receives filesystem change events via the dispatcher socket obtained through D-Bus fd passing. Both initial connection (initDispatcher) and reconnection (attemptReconnect) paths are affected. Users may experience fewer unexpected disconnections and full-index rebuilds during burst file operations on systems with rmem_max < 8 MiB.

Verification Suggestion

Perform burst file operations (30k+ create/delete) and confirm no "event dispatcher connection closed" log or full rebuild trigger. On a system with sysctl net.core.rmem_max=4194304, verify the receive buffer remains 8 MiB after client connection.


根因分析

服务端 event_relay_dispatcher.c 在创建 socketpair 时已通过 SO_RCVBUFFORCE(含 SO_RCVBUF fallback)将接收端缓冲区设为 8MB,再经 D-Bus fd passing 传给客户端。客户端 connectDispatcherSocket() 获取 fd 后又调用 setsockopt(SO_RCVBUF, 8MB),该调用使用受 rmem_max 限制的 SO_RCVBUF 而非 SO_RCVBUFFORCE,在 rmem_max < 8MB 的系统上会将缓冲区从 8MB 缩小为 rmem_max 值,抵消服务端的缓冲区扩大努力。关键证据:vfsmonitorwatcher.cpp:604-609(冗余的客户端 setsockopt)和 event_relay_dispatcher.c:110-118(服务端 SO_RCVBUFFORCE 设置)。

修复方案

移除 connectDispatcherSocket() 中冗余的 setsockopt(SO_RCVBUF) 调用块及不再使用的 kReceiveBufSize 常量。服务端 SO_RCVBUFFORCE 确保的 8MB 缓冲区在 D-Bus fd 传递后无需客户端干预即可保留。实际实现与分析建议一致,无偏离。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 仅移除冗余 setsockopt 代码块,不改变函数签名、返回值或控制流。调用者 initDispatcher() 和 attemptReconnect() 仅检查 fd < 0,不受影响。
  • 历史洞察:被移除的代码在 commit 36b42fb5(PMS 370779,增大缓冲区防止 EAGAIN 断连)中引入。D-Bus event relay 集成(08f68770)后服务端已通过 SO_RCVBUFFORCE(8MB) 设置缓冲区,客户端调用变为冗余且有害。本次修复更好地实现了 370779 的目标——确保 8MB 缓冲区不被缩小。

业务影响范围

受影响模块:VFS 文件监控——客户端通过 D-Bus fd passing 获取的事件通道 socket 接收文件系统变更事件。初始化连接(initDispatcher)和断线重连(attemptReconnect)路径均受影响。在 rmem_max < 8MB 的系统上,用户在大量文件操作时将减少意外断连和全量重建索引的情况。

验证建议

执行大量文件操作(3 万+ 创建/删除),确认日志无 "event dispatcher connection closed" 且未触发全量重建。在 sysctl net.core.rmem_max=4194304 的系统上,验证客户端连接后接收缓冲区保持 8MB。

Summary by Sourcery

Bug Fixes:

  • Prevent filesystem monitoring socket receive buffers from being unintentionally reduced below the dispatcher’s configured size on systems with a lower receive-buffer limit.

@github-actions

Copy link
Copy Markdown

TAG Bot

TAG: 7.1.0
EXISTED: no
DISTRIBUTION: unstable

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

Sorry, we are unable to review this pull request

The GitHub API does not allow us to fetch diffs exceeding 300 files, and this pull request has 421

@wangrong1069
wangrong1069 changed the base branch from master to develop/snipe October 14, 2026 06:32
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 100 分,大于 70 分通过阈值,代码质量符合要求。本次变更移除冗余的 setsockopt 调用,修复了接收缓冲区被意外缩小的 bug,代码逻辑正确,注释详尽,无安全漏洞。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

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

建议: 代码变更语法正确,移除 setsockopt 调用块及其错误处理逻辑后,函数控制流保持完整,return fd 语句正常返回。已确认 kReceiveBufSize 常量无其他引用,所有 include 头文件(sys/socket.h、cstring、cerrno)仍被其他代码使用,无遗留未使用依赖。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

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

建议: 移除冗余代码后新增的注释详尽说明了移除原因,包括 SO_RCVBUFFORCE、rmem_max 限制、缓冲区缩小风险等关键技术细节,可维护性良好。无重复代码,无残留调试代码。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

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

建议: 移除冗余的系统调用 setsockopt 略微减少了系统调用开销。更重要的是,该修复确保了服务端通过 SO_RCVBUFFORCE 设置的 8MB 接收缓冲区不会被客户端的 SO_RCVBUF 调用覆盖缩小,避免了在 rmem_max < 8MB 系统上因缓冲区不足导致的 EAGAIN 断连和全量索引重建,显著提升了突发文件操作场景下的性能和稳定性。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

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

建议: 本次变更为纯代码移除操作,未引入新的攻击面。不涉及用户输入处理、文件路径操作、网络通信等安全敏感场景。无命令注入、SQL注入、路径遍历、缓冲区溢出等安全风险。


💡 改进建议代码示例

// 修复后的代码(已移除冗余 setsockopt 调用)
int VfsMonitorFileSystemWatcherPrivate::connectDispatcherSocket()
{
    // ... 获取 fd 的逻辑 ...

    // The dispatcher already sets the receive buffer via SO_RCVBUFFORCE
    // (with SO_RCVBUF fallback) on the socketpair fd before passing it
    // through D-Bus.  Re-issuing SO_RCVBUF here would override that with
    // a value capped by rmem_max, potentially *shrinking* the buffer on
    // systems where rmem_max < 8 MiB.

    return fd;
}

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

@wangrong1069

Copy link
Copy Markdown
Contributor Author

/retest

The dispatcher (event_relay_dispatcher.c) already sets the socketpair
receive buffer to 8 MiB via SO_RCVBUFFORCE (with SO_RCVBUF fallback)
before passing the fd through D-Bus.  The client's connectDispatcherSocket()
re-issued setsockopt(SO_RCVBUF, 8MB) on the same fd, which overrides the
server's setting with a value capped by rmem_max — on systems where
rmem_max < 8 MiB (common default 4 MiB) this *shrinks* the buffer from
the intended 8 MiB down to rmem_max, negating the server's effort.

Remove the redundant setsockopt block and the now-unused kReceiveBufSize
constant.  The server-side SO_RCVBUFFORCE ensures the 8 MiB buffer
survives across the D-Bus fd handoff without client intervention.

Log: 移除客户端冗余的 setsockopt(SO_RCVBUF) 调用,防止覆盖服务端 SO_RCVBUFFORCE 设置的 8MB 缓冲区

Influence:
1. connectDispatcherSocket 返回值和控制流不变,调用者无感知
2. 在 rmem_max < 8MB 的系统上,接收缓冲区不再被缩小
3. event_receiver.c 的 setsockopt(SO_RCVBUF) 不受影响(作用于自行创建的 socket)
PMS: TASK-395961
@wangrong1069
wangrong1069 force-pushed the agent/pms-bug-bot/f7d6ac00c07c branch from 1cd7950 to 3611ff3 Compare October 14, 2026 09:34
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wangrong1069

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

@wangrong1069

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit c7aaed5 into linuxdeepin:develop/snipe Oct 15, 2026
17 checks passed
@wangrong1069
wangrong1069 deleted the agent/pms-bug-bot/f7d6ac00c07c branch October 15, 2026 02:27
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.

3 participants