Skip to content

fix(net): Stop() 幂等化, 解决请求后 server 崩溃#38

Merged
NeverENG merged 1 commit into
mainfrom
fix/conn-stop-once
May 21, 2026
Merged

fix(net): Stop() 幂等化, 解决请求后 server 崩溃#38
NeverENG merged 1 commit into
mainfrom
fix/conn-stop-once

Conversation

@NeverENG
Copy link
Copy Markdown
Owner

@NeverENG NeverENG commented May 21, 2026

Summary

  • Connection.Stop()sync.Once 改成幂等, 替代 isClose 标志
  • 移除 Stop 末尾的裸 recover()close(msgChan)/close(msgBuffChan): worker 仍可能在 SendBuffMsg, close 会触发 panic: send on closed channel (= "数据静态" 现象, server 处理首个请求后崩溃)
  • ExitBuffChan 在 Stop 内改为非阻塞 send, 防止 buffer 已满时永阻塞

Background

原 Stop 末尾用 defer recover() 吞掉 close 已关闭 channel 的 panic, 同时 close 了 msgChan/msgBuffChan — 但 worker 还在通过 SendBuffMsgmsgBuffChan 写数据。删除 recover 后这个 panic 暴露, 修复方案是不 close 这两个 channel, 让它们随 Connection GC 即可。

Test plan

  • go build ./... 通过
  • go vet ./network/... 通过
  • 本地试运行: 起服务 + 客户端 PUT/GET/DEL 多轮, server 不再崩溃 (4 次连接周期都 clean exit, 无 panic)

试运行发现一个独立 bug ("Error getting value: incomplete response"), 与 msgID 解析相关, 属于 PR-7 协议改造范畴, 本 PR 不处理。

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced connection shutdown mechanism for improved stability and reliability during graceful cleanup operations.

Review Change Stack

- stopOnce 替代 isClose 标志, 自然防止 double-Stop
- 删除裸 recover() 与 close(msgChan)/close(msgBuffChan): worker 仍可能在 SendBuffMsg, close 触发 send on closed channel
- ExitBuffChan 改非阻塞 send, 防止 Stop 在 buffer 已满时永阻塞

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 21, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b778b5e1-7082-4691-831a-4c7556591f34

📥 Commits

Reviewing files that changed from the base of the PR and between 33e4b8e and 8a66edd.

📒 Files selected for processing (1)
  • network/banNet/connection.go

📝 Walkthrough

Walkthrough

The PR refactors Connection's shutdown mechanism from a simple boolean flag to a sync.Once guard, making Stop() idempotent and eliminating "send on closed channel" panics during concurrent sender activity. The method now signals shutdown via non-blocking send rather than closing channels.

Changes

Idempotent Connection Shutdown

Layer / File(s) Summary
Shutdown guard and initialization
network/banNet/connection.go
Connection replaces the isClose bool field with stopOnce sync.Once to guard one-time stop execution. NewConnection reorganizes initialization to place ctx, cancel, and msgChan in the same control fields block.
Idempotent Stop() method
network/banNet/connection.go
Stop() wraps all shutdown steps in stopOnce.Do(...) to ensure single execution. Channel closing logic is removed entirely; instead, a non-blocking send notifies ExitBuffChan to trigger worker shutdown without risking panics from concurrent senders.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through channels with grace,
No more closed doors in the shutdown race,
With stopOnce guarding the final call,
One gentle signal ends it all. 🐰

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/conn-stop-once

Comment @coderabbitai help to get the list of available commands and usage tips.

@NeverENG NeverENG merged commit 8686436 into main May 21, 2026
1 of 3 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.

1 participant