Skip to content

J1939SoftSocket: expand edge-case unit tests, soft↔native interop coverage, and pgn filter#33

Draft
Copilot wants to merge 8 commits into
masterfrom
copilot/add-j1939-softsocket-class
Draft

J1939SoftSocket: expand edge-case unit tests, soft↔native interop coverage, and pgn filter#33
Copilot wants to merge 8 commits into
masterfrom
copilot/add-j1939-softsocket-class

Conversation

Copy link
Copy Markdown

Copilot AI commented May 14, 2026

  • Investigate CI failures
  • Fix flake8 E501
  • Fix TX race condition in J1939SoftSocket close() drain loop
  • Fix _tx_start_bam/_tx_start_rts race: set tx_state before CAN send so close() drain loop doesn't break early when can_socket has per-frame delay (SlowTestSocket)
  • Add more unit tests using TestSocket, SlowTestSocket, and USBTestSocket
    • pgn filter for RTS/CTS session (matching PGN delivered)
    • pgn filter for RTS/CTS session (non-matching PGN silently dropped)
    • pgn filter: 17-bit PGN (data_page=1) matched correctly
    • send() on closed socket returns 0 without raising
    • ABORT from sender resets RTS/CTS RX session; fresh session accepted
    • Duplicate TP.DT sequence number triggers ABORT
    • SlowTestSocket: BAM RX through serial buffer (internal _mux() path)
    • SlowTestSocket: BAM TX with 1ms per-frame delay
    • SlowTestSocket: BAM RX with 30 background frames filling serial buffer
    • SlowTestSocket: RTS/CTS receive with CTS response over slow CAN socket
    • SlowTestSocket: soft-to-soft BAM through slow socket
    • SlowTestSocket: listen_only — RTS/CTS passively reassembled without CTS/ACK emitted
    • SlowTestSocket: pgn filter — non-matching BAM dropped, matching BAM delivered through serial buffer
    • SlowTestSocket: RTS/CTS TX — sock as sender; peer responds CTS+ACK; validates full TX state machine
    • USBTestSocket: BAM receive through USB hardware FIFO
    • USBTestSocket: multiple sequential sessions survive background-frame FIFO drain between sessions
  • Verify 185 non-root J1939 tests pass with 0 failures
  • Verify no new flake8 issues introduced

Copilot AI and others added 2 commits May 1, 2026 05:25
@polybassa
Copy link
Copy Markdown
Owner

@copilot Analyze the J1939 SoftSocket implementation from BenGardiner:j1939_scanning https://github.com/secdev/scapy/pull/4961/changes#diff-5d6504c729d8bfec31ff30aec28e76f32ae70eba914b2338540fe2b13a221f34

Check for useful modifications

Copilot AI and others added 2 commits May 16, 2026 20:10
Copy link
Copy Markdown
Author

Copilot AI commented May 16, 2026

@copilot Analyze the J1939 SoftSocket implementation from BenGardiner:j1939_scanning https://github.com/secdev/scapy/pull/4961/changes#diff-5d6504c729d8bfec31ff30aec28e76f32ae70eba914b2338540fe2b13a22...

Analyzed PR secdev#4961. Summary of what BenGardiner's implementation adds over ours, and what we incorporated:

Already in our implementation (confirmed equivalent):

  • listen_only parameter – identical concept, fully implemented and tested
  • Timeout extension factor (TP_DT_TIMEOUT_EXTENSION_FACTOR = 10) – we have _J1939_TP_DT_TIMEOUT_EXTENSION = 10 with the same total-elapsed-time logic; I clarified the comments to accurately describe the wall-clock ceiling approach (commit 63faffb)

Incorporated from BenGardiner's approach:

  • pgn filter parameter (rx_pgn in his code) – added pgn_filter to J1939TPImplementation and pgn to J1939SoftSocket; pgn=0 accepts all PGNs (identical semantics to his rx_pgn=0); filters single-frame, BAM, and RTS/CTS sessions. Three new tests added (commit ea28d5a).

Not incorporated (out of scope / too complex for this PR):

  • Configurable CMDT block size (bs parameter) – requires multi-block CTS state tracking
  • J1939-81 address claiming (name parameter) – separate feature, significant complexity

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:

  • https://api.github.com/repos/secdev/scapy/pulls/4961/files
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (http block)
    • Triggering command: `/usr/bin/python3 python3 -c
      import urllib.request

Try different URL formats for BenGardiner's PR

urls = [
'REDACTED',
'REDACTED',
]
for url i iptables -w -t security ndor/bin/bash -nxv` (http block)

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

Copilot AI changed the title J1939SoftSocket: expand edge-case unit tests and soft↔native interop coverage J1939SoftSocket: expand edge-case unit tests, soft↔native interop coverage, and pgn filter May 16, 2026
Copilot AI requested a review from polybassa May 16, 2026 20:13
@polybassa
Copy link
Copy Markdown
Owner

@copilot a few CI jobs fail because of the J1939 SoftSocket. fix them

@polybassa
Copy link
Copy Markdown
Owner

@copilot Continue

- J1939TPImplementation.close() now waits up to 2s for the TX queue
  to drain and tx_state to return to IDLE before setting closed=True
  and cancelling scheduler handles. This ensures that send() followed
  immediately by close() (e.g. inside a 'with J1939SoftSocket(...) as s:'
  block) reliably delivers every queued message.

- J1939TPImplementation.send() now cancels the current pending TX poll
  and reschedules it with timeout=0, so the message is dispatched
  within microseconds rather than waiting up to 5ms for the next poll.

- Fix flake8 E501: add '# noqa: E501' to the overlong type comment
  on J1939SoftSocket.select().

Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/36ad6c78-0858-434c-bf72-83f61e3da9b8

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented May 25, 2026

@copilot Continue

Fixed in commit ae5bf26.

Two root causes identified and fixed:

  1. TX race condition (caused 5 of the 8 CI failures — all "J1939SoftSocket TX → NativeJ1939Socket RX" tests): send() enqueues the message but close() was immediately setting closed=True and cancelling the TX scheduler before _tx_poll had a chance to dequeue and transmit it. Fixed by:

    • close() now waits (up to 2 s) for tx_state to return to IDLE and the tx_queue to drain before cancelling scheduler handles.
    • send() now cancels the pending TX poll and reschedules it with timeout=0, so the message is dispatched almost immediately rather than waiting up to 5 ms.
  2. Flake8 E501 (code health check failure): the type comment on J1939SoftSocket.select() was 121 chars. Added # noqa: E501 to suppress it.

@polybassa
Copy link
Copy Markdown
Owner

@copilot add more unit tests, using testsocket and slowtestsocket

Copilot AI and others added 2 commits May 25, 2026 07:21
…am/_tx_start_rts

Agent-Logs-Url: https://github.com/polybassa/scapy-1/sessions/5c82c8d1-e2dd-48bb-967f-02c01c3675cd

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
@polybassa
Copy link
Copy Markdown
Owner

@copilot continue

Copy link
Copy Markdown
Author

Copilot AI commented May 25, 2026

@copilot continue

Added 5 more tests in commit c7cb328, bringing total non-root J1939 tests from 180 → 185 (all passing):

  • listen_only via SlowTestSocket – RTS/CTS session passively reassembled; verified no CTS/ACK emitted
  • pgn filter via SlowTestSocket – non-matching BAM silently dropped through serial buffer; matching BAM delivered
  • RTS/CTS TX via SlowTestSocket – sock as sender over slow socket; peer thread responds CTS+ACK; validates full TX state machine
  • USBTestSocket: BAM receive – frames buffer in the USB hardware FIFO until select() drains it
  • USBTestSocket: sequential sessions – background frames between two sessions drained correctly by the CAN receive loop

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.

2 participants