Skip to content

fix: clean up CA subscription management and ensure proper lifecycle management - #74

Merged
drewr95 merged 3 commits into
masterfrom
fix/72-ca-subscription-cleanup
Aug 21, 2026
Merged

fix: clean up CA subscription management and ensure proper lifecycle management#74
drewr95 merged 3 commits into
masterfrom
fix/72-ca-subscription-cleanup

Conversation

@drewr95

@drewr95 drewr95 commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes: #72

This pull request introduces improved lifecycle management for message subscriptions associated with ControllerApplication instances in the J1939 stack. Subscriptions registered through a ControllerApplication are now automatically cleaned up when the CA is removed from its ElectronicControlUnit (ECU), while subscriptions registered directly on the ECU remain unaffected. The changes also ensure thread safety during CA lifecycle operations and add comprehensive tests to verify the new behavior.

Lifecycle management and subscription cleanup:

  • Subscriptions registered through a ControllerApplication are now tracked with an owner reference and are automatically removed when the CA is detached from the ECU. Direct ECU subscriptions remain until explicitly removed. (j1939/controller_application.py, j1939/electronic_control_unit.py) [1] [2] [3]
  • The remove_ca method in ElectronicControlUnit now stops the CA, removes all its owned subscriptions, and detaches it from the ECU. The underlying data-link layer's remove_ca returns the removed CA instance for this purpose. (j1939/electronic_control_unit.py, j1939/j1939_21.py, j1939/j1939_22.py) [1] [2] [3]

Thread safety improvements:

  • Added reentrant locks (RLock) to both ControllerApplication and ElectronicControlUnit to protect CA lifecycle operations and prevent race conditions when starting, stopping, or removing CAs. (j1939/controller_application.py, j1939/electronic_control_unit.py) [1] [2]
  • Timer scheduling and CA state transitions are now performed in a thread-safe manner, capturing the current ECU reference under lock to avoid lock inversion. (j1939/controller_application.py) [1] [2]

Testing and validation:

  • Added new tests to verify that removing a CA cleans up only its subscriptions, that replacement CAs require explicit re-subscription, that unsubscribe behavior is preserved, and that timers are stopped before detaching a CA. Tests also ensure correct behavior across both J1939-21 and J1939-22 data-link layers. (test/test_ecu.py)

These changes increase the robustness and predictability of subscription management, especially in dynamic scenarios where CAs are frequently added, removed, or replaced.

@drewr95
drewr95 requested review from RaulSMS and khauersp and a lite review from Copilot August 20, 2026 22:46

Copilot AI left a comment

Copy link
Copy Markdown

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 addresses issue #72 by tying ControllerApplication-registered subscriptions to the CA lifecycle, so removing/replacing a CA no longer leaves behind stale subscriber entries bound to defunct CA instances. It also introduces additional locking around CA lifecycle operations and adds tests that validate subscription cleanup and timer shutdown behavior.

Changes:

  • Track ECU subscriptions with an optional owner and automatically remove CA-owned subscriptions when a CA is removed.
  • Update DLL remove_ca to return the removed CA instance so the ECU can stop/detach it and clean up owned subscriptions.
  • Add tests covering CA removal cleanup, replacement behavior, legacy unsubscribe semantics, and timer shutdown across J1939-21/22 receive paths.

Reviewed changes

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

Show a summary per file
File Description
j1939/electronic_control_unit.py Adds owner-aware subscription management, CA lifecycle lock, and CA removal cleanup.
j1939/controller_application.py Routes CA subscriptions through ECU with ownership and adds lifecycle locking around start/stop/claim scheduling.
j1939/j1939_21.py Changes DLL remove_ca to return the removed CA instance (or None).
j1939/j1939_22.py Changes DLL remove_ca to return the removed CA instance (or None).
test/test_ecu.py Adds tests validating CA-owned subscription cleanup, resubscribe requirements, legacy unsubscribe behavior, timer shutdown, and both receive paths.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread j1939/electronic_control_unit.py
Comment thread j1939/controller_application.py Outdated
Comment thread j1939/electronic_control_unit.py Outdated
@drewr95
drewr95 merged commit 2b908c1 into master Aug 21, 2026
17 checks passed
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.

Subscriber entries aren't tied to ControllerApplication lifecycle, making CA-recreating address changes error-prone

3 participants