Skip to content

[client] Fix peer close deadlock #3904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged

[client] Fix peer close deadlock #3904

merged 1 commit into from
May 30, 2025

Conversation

pappz
Copy link
Contributor

@pappz pappz commented May 30, 2025

Describe your changes

If the guard calls isConnectedOnAllWay at the wrong time when Close has already been called, it can result in a deadlock.

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

@Copilot Copilot AI review requested due to automatic review settings May 30, 2025 21:08
Copy link

Copy link
Contributor

@Copilot 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 removes the mutex lock in isConnectedOnAllWay to avoid a deadlock scenario with the Close function.

  • Removed mu.Lock/Unlock around status check in isConnectedOnAllWay
  • Added a comment acknowledging the ideal mutex protection and potential deadlock
Comments suppressed due to low confidence (1)

client/internal/peer/conn.go:693

  • Add a concurrency-focused test that invokes isConnectedOnAllWay and Close in parallel to verify there is no data race or deadlock.
func (conn *Conn) isConnectedOnAllWay() (connected bool) {

@@ -691,8 +691,7 @@ func (conn *Conn) evalStatus() ConnStatus {
}

func (conn *Conn) isConnectedOnAllWay() (connected bool) {
conn.mu.Lock()
defer conn.mu.Unlock()
// would be better to protect this with a mutex, but it could cause deadlock with Close function
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

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

Consider using an atomic variable or a try-lock mechanism (e.g., sync/atomic or TryLock) to safely read the connected state without introducing a potential deadlock.

Suggested change
// would be better to protect this with a mutex, but it could cause deadlock with Close function
// Use atomic variables to safely read the connection states without risking deadlocks

Copilot uses AI. Check for mistakes.

@pappz pappz merged commit aa07b3b into main May 30, 2025
35 of 36 checks passed
@pappz pappz deleted the fix/peer-close-deadlock branch May 30, 2025 21:38
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