Skip to content

fix: avoid WebSocket reconnect deadlock - #1161

Open
mikamikasuki wants to merge 1 commit into
davidusb-geek:masterfrom
mikamikasuki:fix/1140-websocket-reconnect-deadlock
Open

mikamikasuki wants to merge 1 commit into
davidusb-geek:masterfrom
mikamikasuki:fix/1140-websocket-reconnect-deadlock

Conversation

@mikamikasuki

@mikamikasuki mikamikasuki commented Oct 3, 2026 •

Copy link
Copy Markdown

Summary

  • Avoid reacquiring the client's asyncio.Lock when send() reconnects a disconnected client.
  • Add a regression test for sending after disconnection.

Fixes #1140

Validation

  • pytest tests/test_websocket_client.py -q — 8 passed.
  • pytest tests/ -q — 1254 passed, 1 skipped, 32 xfailed, and 56 subtests passed. One existing failure and its teardown error in TestConnectionManager.test_lock_prevents_race_conditions also reproduce on the base commit.
  • ruff check . and ruff format --check passed.

Summary by Sourcery

Prevent WebSocket send operations from deadlocking during automatic reconnection.

Bug Fixes:

  • Prevent WebSocket sends from deadlocking when they trigger reconnection after the client disconnects.

Enhancements:

  • Separate lock-aware reconnection logic for safe reuse by locked operations.

Tests:

  • Add regression coverage for sending from a disconnected WebSocket client.

@sourcery-ai

sourcery-ai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

The PR prevents a WebSocket send/reconnect deadlock by separating lock acquisition from reconnection work and using the lock-safe helper from send(), with a regression test covering sends initiated while disconnected.

Sequence diagram for deadlock-free WebSocket send reconnect

sequenceDiagram
    participant Caller
    participant Client
    participant WebSocket

    Caller->>Client: send(msg_type, kwargs)
    Client->>Client: acquire _lock
    alt disconnected
        Client->>Client: _reconnect_locked()
        Client->>WebSocket: _cleanup()
        Client->>WebSocket: _connect()
        WebSocket-->>Client: connected
    end
    Client->>Client: _next_id()
    Client->>WebSocket: send command
    WebSocket-->>Client: response
    Client-->>Caller: response
Loading

File-Level Changes

Change Details Files
Refactor reconnection so callers that already hold the client lock can reconnect without attempting to acquire it again.
  • Extract the cleanup, delay, connection, timeout, and error handling into a lock-assuming helper.
  • Keep the public reconnect path responsible for acquiring the lock.
  • Update send() to invoke the lock-assuming helper while handling a disconnected client.
src/emhass/websocket_client.py
Add regression coverage for sending through a disconnected client.
  • Mock reconnection and WebSocket send/response handling.
  • Verify send() completes, reconnects once, and sends once within the test timeout.
tests/test_websocket_client.py

Assessment against linked issues

Issue Objective Addressed Explanation
#1140 Prevent AsyncWebSocketClient.send() from deadlocking when called while the client is disconnected and reconnection is required. ✅
#1140 Structure reconnection locking so that public reconnect() remains lock-protected while send() can perform reconnection without recursively acquiring the same asyncio.Lock. ✅
#1140 Add regression coverage demonstrating that sending from a disconnected client reconnects and dispatches the message successfully. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

This branch has not been deployed

No deployments
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.

Deterministic deadlock in AsyncWebSocketClient.send() when disconnected

1 participant