Skip to content

pyview connection tracking hooks#161

Open
ogrodnek wants to merge 2 commits intomainfrom
add_connection_hooks
Open

pyview connection tracking hooks#161
ogrodnek wants to merge 2 commits intomainfrom
add_connection_hooks

Conversation

@ogrodnek
Copy link
Copy Markdown
Owner

No description provided.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 27, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 79.04762% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.14%. Comparing base (bb29dbe) to head (f398c41).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pyview/ws_handler.py 20.00% 14 Missing and 2 partials ⚠️
tests/test_connection_tracker.py 91.42% 6 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #161      +/-   ##
==========================================
+ Coverage   79.93%   80.14%   +0.21%     
==========================================
  Files         104      106       +2     
  Lines        8576     8678     +102     
  Branches      628      633       +5     
==========================================
+ Hits         6855     6955     +100     
  Misses       1588     1588              
- Partials      133      135       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Adds an optional connection tracking hook API to PyView’s WebSocket lifecycle so integrators can observe connects/disconnects and event handling timing.

Changes:

  • Introduces a ConnectionTracker runtime-checkable Protocol and re-exports it from pyview.
  • Threads an optional connection_tracker through PyViewLiveSocketHandler and emits connect/event/disconnect callbacks.
  • Adds a PyView.registered_routes convenience property plus initial unit tests for plumbing/protocol conformance.

Reviewed changes

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

Show a summary per file
File Description
pyview/connection_tracker.py Defines the ConnectionTracker protocol for lifecycle callbacks.
pyview/ws_handler.py Wires tracker callbacks into WebSocket connect/event/disconnect flow.
pyview/pyview.py Accepts connection_tracker, passes it to handler, adds registered_routes.
pyview/__init__.py Exports ConnectionTracker in the public package API.
tests/test_connection_tracker.py Adds tests for protocol runtime-checkability and plumbing.

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

Comment on lines +151 to +156
if self.connection_tracker:
with suppress(Exception):
self.connection_tracker.on_connect(
topic, socket, lv_class, url.path, session
)

Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

on_connect is only invoked for the initial phx_join in handle(). When the client navigates within the same WebSocket (the phx_join handled inside _handle_connected_loop), a new LiveView is mounted and first-rendered but the tracker never receives on_connect, so connection accounting will be incomplete. Consider invoking connection_tracker.on_connect(...) for navigation joins as well (and likewise for any mount path that results in a new ConnectedLiveViewSocket).

Copilot uses AI. Check for mistakes.

await self.manager.send_personal_message(json.dumps(resp), websocket)

if self.connection_tracker:
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

Use if self.connection_tracker is not None: rather than a truthiness check. As written, a tracker instance that defines __bool__/__len__ and evaluates to False would silently disable tracking.

Suggested change
if self.connection_tracker:
if self.connection_tracker is not None:

Copilot uses AI. Check for mistakes.
Comment on lines +470 to +472
if self.connection_tracker and topic:
with suppress(Exception):
self.connection_tracker.on_disconnect(topic)
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

on_disconnect(topic) is called here on phx_leave, but handle() also unconditionally calls on_disconnect(topic) in its finally block when the WebSocket closes. If the client sends phx_leave and then disconnects without re-joining a new topic, the tracker will receive a duplicate disconnect for the same topic. Consider clearing/resetting the tracked topic (or tracking an "already disconnected" flag) after phx_leave so the finally block doesn’t emit a second disconnect for the same view.

Suggested change
if self.connection_tracker and topic:
with suppress(Exception):
self.connection_tracker.on_disconnect(topic)

Copilot uses AI. Check for mistakes.
Comment on lines +118 to +123
def test_pyview_passes_tracker_to_handler():
"""PyView should pass connection_tracker through to LiveSocketHandler."""
tracker = FakeTracker()
app = PyView(connection_tracker=tracker)
assert app.live_handler.connection_tracker is tracker

Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

These tests validate protocol conformance and that the tracker is plumbed through, but they don’t assert the new runtime behavior (that on_connect, on_event, and on_disconnect are actually invoked by LiveSocketHandler, and that exceptions from the tracker are suppressed). Adding an async WebSocket handler test (similar to existing test_ws_handler_cleanup.py) that drives a phx_join + event + disconnect (and a phx_leave path) would prevent regressions in the hook semantics.

Copilot uses AI. Check for mistakes.
@ogrodnek ogrodnek force-pushed the add_connection_hooks branch from 7cd7075 to 0ae5b80 Compare April 3, 2026 22:02
@ogrodnek ogrodnek marked this pull request as ready for review April 3, 2026 22:38
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f398c4123d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +174 to +176
if self.connection_tracker and topic:
with suppress(Exception):
self.connection_tracker.on_disconnect(topic)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use active topic when reporting disconnects

The finally block always emits on_disconnect(topic) using the topic captured from the initial phx_join, but _handle_connected_loop can switch LiveViews via later phx_join messages. In a navigation flow (phx_leave old topic -> phx_join new topic -> socket closes), this reports the old topic again and never reports disconnect for the active topic, which can leave trackers with incorrect connection state/counts.

Useful? React with 👍 / 👎.

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.

3 participants