Skip to content

Keep alive: RFC 5626 outbound support and reconnect logic - #56

Merged
codingjoe merged 8 commits into
mainfrom
copilot/add-reconnect-logic-and-keep-alive
Mar 19, 2026
Merged

Keep alive: RFC 5626 outbound support and reconnect logic#56
codingjoe merged 8 commits into
mainfrom
copilot/add-reconnect-logic-and-keep-alive

Conversation

Copilot AI commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Implements full RFC 5626 client-initiated keep-alive and automatic reconnection with back-off.

RFC 5626 keep-alive (§4.4.1 + §5)

  • SessionInitiationProtocol now spawns a _run_keepalive() task on connection_made that sends double-CRLF pings at keepalive_interval_secs (default 30 s)
  • REGISTER requests advertise Supported: outbound and add ;ob to the Contact URI per §5
  • connection_lost cancels the keepalive task and sets the new public disconnected_event (asyncio.Event)

Reconnect logic

_connect_sip() now loops with exponential back-off (1 s → … → 60 s), detecting drops via protocol.disconnected_event:

_, protocol = await loop.create_connection(session_factory, ...)
await protocol.disconnected_event.wait()
# reconnects automatically

Docs

  • docs/rfc_status.md: RFC 5626 status Partial → Complete

[!WARNING]

Original prompt

This section details on the original issue you should resolve

Keep alive * Add a reconnect logic * Implement all keep-alive measure outlined in the RFC

Custom agent used: SuperJoe
CodingJoe's digital clone following his coding guidelines and best practices.

Original prompt

This section details on the original issue you should resolve

<issue_title>Keep alive</issue_title>
<issue_description>* Use start_server to start SIP servers in the CLI and docs

  • Add a reconnect logic
  • Implement all keep-alive measure outlined in the RFC</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: SuperJoe
CodingJoe's digital clone following his coding guidelines and best practices.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@codecov

codecov Bot commented Mar 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.93%. Comparing base (0e3d9f7) to head (1b72b47).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #56      +/-   ##
==========================================
+ Coverage   93.79%   93.93%   +0.13%     
==========================================
  Files          24       24              
  Lines        1966     1994      +28     
==========================================
+ Hits         1844     1873      +29     
+ Misses        122      121       -1     

☔ 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.

Copilot AI and others added 2 commits March 19, 2026 00:01
Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>
…, ob_param to ob_uri_param

Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>
Copilot AI changed the title [WIP] Add reconnect logic and keep-alive measures for SIP servers Keep alive: RFC 5626 outbound support, reconnect logic, and start_server Mar 19, 2026
Copilot AI requested a review from codingjoe March 19, 2026 00:04

@codingjoe codingjoe left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot we don't create new test classes for features. Unit tests must match their counterpart API. So one Class will have only one TestClass.

We also ONLY mock IO/transports, but otherwise avoid mocks to harden the implementation. We don't eat implementation but external behavior or and side effects of a function.

If you find other tests that don't need this criteria, clean up!

Comment thread voip/sip/protocol.py Outdated
self.disconnected_event.set()


async def start_server(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I didn't mean to create a new function, but use create_server.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed start_server() in 06c4d7f. _serve_sip now calls asyncio.get_running_loop().create_server() directly, and the cookbook example uses loop.create_server() too.

…rpart classes

Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

@copilot we don't create new test classes for features. Unit tests must match their counterpart API. So one Class will have only one TestClass.

We also ONLY mock IO/transports, but otherwise avoid mo...

Done in 06c4d7f. Collapsed TestKeepalive, TestStartServer, and TestBuildContactOb into TestSIPProtocol and TestRegistration respectively. TestReconnectTestTranscribeCLI, TestListenModeTestEchoCLI. Also replaced the MagicMock() task in the keepalive cancellation test with a real asyncio.Task.

Copilot AI requested a review from codingjoe March 19, 2026 01:34

@codingjoe codingjoe left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot on second thought let's ditch the listening feature for now

Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

@copilot on second thought let's ditch the listening feature for now

Done in 4cb7bff. Removed --listen, _serve_sip, _run_sip, the related tests, and the cookbook section. The three CLI commands now call _connect_sip directly.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • stun.cloudflare.com
    • Triggering command: /usr/bin/python3 python3 -m pytest tests/test_main.py tests/sip/test_protocol.py -q (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Keep alive: RFC 5626 outbound support, reconnect logic, and start_server Keep alive: RFC 5626 outbound support and reconnect logic Mar 19, 2026
Copilot AI requested a review from codingjoe March 19, 2026 08:27
@codingjoe
codingjoe marked this pull request as ready for review March 19, 2026 09:43
Copilot AI review requested due to automatic review settings March 19, 2026 09:43

Copilot AI 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.

Pull request overview

Adds RFC 5626 outbound keep-alive support to the SIP UAC and introduces CLI-level automatic reconnection to keep long-running SIP sessions alive across network drops.

Changes:

  • Add a periodic RFC 5626 double-CRLF keep-alive task, Supported: outbound, ;ob Contact parameter, and a public disconnected_event to the SIP protocol.
  • Add reconnect loop with exponential back-off to the CLI _connect_sip() implementation.
  • Update tests and documentation to reflect keep-alive / outbound / reconnect behavior.

Reviewed changes

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

Show a summary per file
File Description
voip/sip/protocol.py Adds keep-alive task, disconnected_event, ;ob Contact support, and Supported: outbound on REGISTER.
voip/__main__.py Adds reconnect loop with back-off and logging for CLI SIP connections.
tests/test_main.py Adds CLI tests covering retry on OSError and reconnect after disconnected_event.
tests/sip/test_protocol.py Adds keep-alive task tests and updates REGISTER assertions for ;ob + Supported: outbound.
docs/rfc_status.md Marks RFC 5626 support as “Complete” and documents implemented pieces.
docs/feature_roadmap.md Updates roadmap text to mention outbound/reconnect and references start_server.
docs/cookbook.md Updates example import to include start_server.

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

Comment thread voip/sip/protocol.py
Comment thread voip/sip/protocol.py
Comment thread voip/sip/protocol.py
Comment thread voip/__main__.py
Comment thread docs/cookbook.md
Comment thread docs/feature_roadmap.md
@codingjoe
codingjoe merged commit 3f97385 into main Mar 19, 2026
24 checks passed
@codingjoe
codingjoe deleted the copilot/add-reconnect-logic-and-keep-alive branch March 19, 2026 10:27
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.

Keep alive

3 participants