Skip to content

fix: replace WebSocket ping with inactivity watchdog; extend NoteSync timeout#8

Merged
Go1c merged 1 commit into
mainfrom
fix/heartbeat-and-timeout
Apr 14, 2026
Merged

fix: replace WebSocket ping with inactivity watchdog; extend NoteSync timeout#8
Go1c merged 1 commit into
mainfrom
fix/heartbeat-and-timeout

Conversation

@Go1c
Copy link
Copy Markdown
Owner

@Go1c Go1c commented Apr 14, 2026

Problem

PR #6 applied ping_interval=heartbeat_interval to detect dead connections. Real-environment testing revealed the server does not support WebSocket ping frames — it responded with code 1011 and closed the connection every 30 seconds.

Additionally, _initial_sync used a 60s timeout for NoteSync. On a full sync (lastTime=0) the server sends NoteSyncEnd immediately but delivers actual note data over a minute later — causing NoteSync to abort before any files were written.

Fix

Heartbeat: Reverted to ping_interval=None. Added _inactivity_watchdog() — an asyncio background task that checks elapsed time since last received message every heartbeat_interval seconds. If no data received for 2 × heartbeat_interval, it closes the connection, which triggers the existing reconnect loop.

Timeout: Extended _initial_sync NoteSync timeout from 60s → 300s.

Verified

Real-server pull test:

  • 24 notes received (including previously missing Test-456/123.md, 456.md, 789.md) ✓
  • No ping disconnect ✓
  • No echo push-back (NoteModify → absent from logs) ✓

Summary by Sourcery

Replace WebSocket ping-based heartbeats with an inactivity watchdog and extend the initial NoteSync timeout to better handle real-world server behavior.

Bug Fixes:

  • Avoid server-side disconnects by disabling WebSocket ping frames and relying on an inactivity-based reconnect mechanism.
  • Prevent premature NoteSync termination on full syncs by increasing the wait timeout from 60 to 300 seconds.

Enhancements:

  • Track last-received WebSocket message time and introduce a watchdog task that closes idle connections after a configurable inactivity period.

… timeout

PR #6 used ping_interval=30 but the server does not respond to WebSocket
ping frames — confirmed by real-environment test: connection dropped every
30s with "keepalive ping timeout". Reverted to ping_interval=None.

Instead, an asyncio background task (_inactivity_watchdog) tracks the
last received message time and closes the connection if no data arrives
for 2 × heartbeat_interval (60s default), triggering the reconnect loop.
_last_received_at is updated on every text or binary frame received.

Also extended the NoteSync timeout in _initial_sync from 60s to 300s.
The server sends NoteSyncEnd immediately but may delay actual note
delivery by over a minute on full syncs — the 60s window was too short
and caused NoteSync to abort before any notes were written.

Verified with real-server pull: 24 notes (including Test-456/123.md,
456.md, 789.md) fully synced, no ping disconnects, no echo push-backs.
@Go1c Go1c merged commit bcc2792 into main Apr 14, 2026
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 14, 2026

Reviewer's Guide

Replaces WebSocket-level pings with an application-side inactivity watchdog and extends the initial note sync timeout to handle slow, real-world server behavior without premature disconnects or sync aborts.

Sequence diagram for WebSocket inactivity watchdog and reconnect

sequenceDiagram
    actor User
    participant Client
    participant InactivityWatchdog
    participant WebSocket
    participant Server

    User->>Client: start
    Client->>WebSocket: connect(ping_interval=None, ping_timeout=None)
    WebSocket-->>Client: connection_open
    Client->>Client: _last_received_at = time.monotonic()
    Client->>Client: start _listen()
    Client->>InactivityWatchdog: start _inactivity_watchdog()

    loop normal_message_flow
        Server-->>WebSocket: message
        WebSocket-->>Client: raw
        Client->>Client: _handle_text or _handle_binary
        Client->>Client: _last_received_at = time.monotonic()
    end

    loop watchdog_check_every_heartbeat_interval
        InactivityWatchdog->>InactivityWatchdog: sleep(heartbeat_interval)
        InactivityWatchdog->>InactivityWatchdog: idle = now - _last_received_at
        alt idle < 2 * heartbeat_interval
            InactivityWatchdog-->>InactivityWatchdog: continue
        else idle >= 2 * heartbeat_interval
            InactivityWatchdog->>Client: log idle warning
            InactivityWatchdog->>WebSocket: close()
            WebSocket-->>Client: connection_closed
            Client->>Client: existing reconnect loop triggered
            InactivityWatchdog-->>InactivityWatchdog: return
        end
    end
Loading

Updated class diagram for WebSocket client watchdog and timestamp tracking

classDiagram
    class AppConfig {
    }

    class Client {
        - config : AppConfig
        - _on_reconnect : Callable
        - _msg_queue : list
        - _ready_event : asyncio.Event
        - _last_received_at : float
        - ws
        + __init__(config : AppConfig) : None
        + on_reconnect(handler : Callable) : None
        + _connect() : Coroutine
        + _listen() : Coroutine
        + _inactivity_watchdog() : Coroutine
        + _handle_text(raw : str) : Coroutine
        + _handle_binary(raw : bytes) : Coroutine
    }

    Client --> AppConfig
Loading

File-Level Changes

Change Details Files
Replace WebSocket ping/pong-based liveness detection with an inactivity watchdog driven by last-received-message timestamps.
  • Disable WebSocket ping_interval and ping_timeout by setting them to None when establishing the connection.
  • Track last received message time using a new _last_received_at attribute initialized on connect and updated in both text and binary handlers.
  • Introduce _inactivity_watchdog background task in _listen that periodically checks idle time against 2×heartbeat_interval and closes the socket if exceeded, relying on existing reconnect logic.
  • Ensure the watchdog task is cancelled when the listen loop exits to avoid orphaned tasks.
fns_cli/client.py
Increase tolerance for slow NoteSync operations during initial sync.
  • Extend the _wait_note_sync timeout used by _initial_sync from 60 seconds to 300 seconds to accommodate servers that deliver data long after sending NoteSyncEnd.
fns_cli/sync_engine.py

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

@Go1c Go1c deleted the fix/heartbeat-and-timeout branch April 14, 2026 09:52
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In _listen, you cancel the watchdog task in finally but never await it; consider calling watchdog.cancel() followed by await asyncio.gather(watchdog, return_exceptions=True) to ensure it finishes cleanly and to avoid potential "Task was destroyed but it is pending" warnings.
  • In _inactivity_watchdog, interval = self.config.client.heartbeat_interval is assumed to be a positive value; adding a guard (e.g., raising or falling back to a sane default when it is 0 or negative) would prevent a potential busy loop or invalid sleep.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `_listen`, you cancel the `watchdog` task in `finally` but never await it; consider calling `watchdog.cancel()` followed by `await asyncio.gather(watchdog, return_exceptions=True)` to ensure it finishes cleanly and to avoid potential "Task was destroyed but it is pending" warnings.
- In `_inactivity_watchdog`, `interval = self.config.client.heartbeat_interval` is assumed to be a positive value; adding a guard (e.g., raising or falling back to a sane default when it is 0 or negative) would prevent a potential busy loop or invalid sleep.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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