Skip to content

fix: avoid stop race on component stop channel#354

Merged
zouyx merged 1 commit intofeature/mergeMasterfrom
codex/fix-issues-in-pr-review
Mar 14, 2026
Merged

fix: avoid stop race on component stop channel#354
zouyx merged 1 commit intofeature/mergeMasterfrom
codex/fix-issues-in-pr-review

Conversation

@zouyx
Copy link
Copy Markdown
Member

@zouyx zouyx commented Mar 14, 2026

Motivation

  • Prevent races and panics when Start() and Stop() concurrently access a component's stopCh, which can lead to closing a nil/already-closed channel or inconsistent channel reference.

Description

  • Add stopMu sync.Mutex to ConfigComponent and SyncServerIPListComponent to protect stopCh lifecycle.
  • Add ensureStopCh() to safely initialize stopCh under lock and return a local snapshot used by Start() to avoid reading a field that may be closed concurrently.
  • Change Start() to read from the local stopCh snapshot and change Stop() to lock before closing the channel (kept stopOnce to ensure idempotence).
  • Apply the same fixes to both component/notify/componet_notify.go and component/serverlist/sync.go and run gofmt on modified files.

Testing

  • Ran go test ./component/notify ./component/serverlist and both packages passed.
  • Ran go test ./... which failed during module download due to proxy.golang.org returning HTTP 403, which is an external network/proxy issue and not related to these code changes.

Codex Task

Copilot AI review requested due to automatic review settings March 14, 2026 12:02
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 14, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 398c02ce-99a3-4f65-8e50-c35fc39047dd

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/fix-issues-in-pr-review
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Tip

CodeRabbit can scan for known vulnerabilities in your dependencies using OSV Scanner.

OSV Scanner will automatically detect and report security vulnerabilities in your project's dependencies. No additional configuration is required.

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Mar 14, 2026

感谢您提出Pull Request,我会尽快Review。我会在1-2日内进行查看或者回复,如果遇到节假日可能会处理较慢,敬请谅解。

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates two background “refresh” components to more safely manage their stop channels, aiming to prevent nil stop channels from making goroutines un-stoppable and to reduce data races around stop-channel initialization/closure.

Changes:

  • Add a mutex (stopMu) and an ensureStopCh() helper to lazily initialize stopCh.
  • In Start(), capture the stop channel once (stopCh := ensureStopCh()) and select on that local reference.
  • In Stop(), guard close logic with stopOnce and stopMu.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
component/serverlist/sync.go Adds mutex + ensureStopCh() and uses a local stopCh in the sync loop.
component/notify/componet_notify.go Adds mutex + ensureStopCh() and uses a local stopCh in the long-poll loop.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +87 to 91
s.stopMu.Lock()
defer s.stopMu.Unlock()
if s.stopCh != nil {
close(s.stopCh)
}
Comment on lines 80 to 82
if c.stopCh != nil {
close(c.stopCh)
}
Copy link
Copy Markdown
Member Author

@zouyx zouyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@zouyx zouyx changed the base branch from develop to feature/mergeMaster March 14, 2026 13:03
@zouyx zouyx merged commit 721e75a into feature/mergeMaster Mar 14, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants