Feature/general threading improvements#18
Open
khauersp wants to merge 27 commits into
Open
Conversation
To support agentic development
Extend the DTC class and Dm1 sender/receiver to handle SAE J1939-73 SPN conversion methods 1, 2, 3, and 4 (previously only CM 4 / CM-bit-clear was supported and other methods were logged as errors on receive). TX takes an optional per-DTC 'cm' key (default 4); RX disambiguates the CM-bit-set case via a new Dm1(rx_cm_bit_set=...) constructor arg.
Add claude init
Add support for all four DM1 SPN conversion methods
06adcdf to
d4cb879
Compare
* feat: remove numpy * test: add coverage for j1939_22 logic * test: clean up docs and remove constant * feat: use constants instead of hardcoded numbers
improvements
test latency
d4cb879 to
950845d
Compare
With the two-thread model introduced in this branch, the protocol thread iterates _snd_buffer in async_job_thread concurrently with send_pgn being called from a user/timer thread. The check-then-write on _snd_buffer was unprotected, creating a live race (RuntimeError: dictionary changed size during iteration on CPython). j1939_22.py already wraps its send_pgn buffer writes with _buffer_lock; this commit brings j1939_21.py to the same standard. CAN I/O (_send_tp_bam / _send_tp_rts) is intentionally kept outside the lock to avoid holding it during I/O.
Two new tests in test_threading.py covering the race condition fixed in the previous commit: - test_send_pgn_concurrent_no_crash: 4 threads hammer send_pgn while the protocol thread is running; verifies no RuntimeError or crash. - test_send_pgn_j1939_21_buffer_lock_no_race: two threads race to send to the same src/dst pair simultaneously; verifies the check-then-write is atomic (exactly one succeeds, one is rejected).
Fix/pr5 buffer lock and tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces significant improvements to the threading, timer management, and lifecycle handling in the J1939 ECU implementation. The main changes include splitting protocol and timer handling into separate threads for better isolation, introducing thread-safe mechanisms for timer events and subscriber management, and adding a robust dependent lifecycle registry to ensure proper shutdown of helper objects. Additionally, all timeouts and deadlines now use
time.monotonic()for improved reliability, and critical shared resources are protected with appropriate locks.Threading and Timer Management Improvements:
heapq) for efficient scheduling. [1] [2] [3]time.monotonic()instead oftime.time()to avoid issues with system clock changes. [1] [2] [3] [4] [5]Thread Safety and Synchronization:
threading.RLockandthreading.Lock) to protect access to timer events, subscribers, and protocol buffers, ensuring safe concurrent access from multiple threads. [1] [2] [3] [4] [5] [6] [7]Lifecycle and Shutdown Handling:
register_dependentandunregister_dependentmethods to both the ECU and ControllerApplication classes for managing dependent helpers. [1] [2]General Codebase Improvements:
These changes collectively enhance the robustness, correctness, and maintainability of the J1939 ECU implementation, especially in multi-threaded and complex application scenarios.