Skip to content

Fix CPX UART flow-control handling#600

Open
ataffanel wants to merge 2 commits into
masterfrom
ataffane/fix-cpx-uart-flow-control
Open

Fix CPX UART flow-control handling#600
ataffanel wants to merge 2 commits into
masterfrom
ataffane/fix-cpx-uart-flow-control

Conversation

@ataffanel

Copy link
Copy Markdown
Member

Summary

Fix CPX UART flow-control handling in UARTTransport.

The previous implementation used one Lock both to serialize outgoing writes and to represent incoming UART CTS (0xFF 0x00). This could crash with:

RuntimeError: release unlocked lock

when duplicate or unsolicited CTS frames arrived while no write was pending.

This PR replaces that lock-as-CTS mechanism with explicit flow-control state:

  • Use threading.Event to track peer CTS
  • Use a separate TX lock to serialize outgoing packets
  • Use a raw serial write lock to avoid interleaving data frames and CTS replies
  • Treat duplicate/unsolicited 0xFF 0x00 frames as harmless CTS events
  • Discard CRC-bad frames and continue reading until a valid CPX packet arrives
  • Add fake-serial unit tests for CTS handling, CRC recovery, and CTS-gated writes

Fixes #599.

Testing

  • python -m unittest test.cpx.test_uart_transport
  • python -m unittest discover test

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 fixes CPX UART flow-control handling in UARTTransport by replacing the previous “lock-as-CTS” mechanism (which could trigger RuntimeError: release unlocked lock on duplicate/unsolicited CTS frames) with explicit flow-control state, and adds unit tests to validate the new behavior.

Changes:

  • Replace CTS signaling based on a shared Lock with a threading.Event (_tx_ready) and a separate TX serialization lock (_tx_lock).
  • Add _serial_write_lock + _write_raw() to prevent interleaved raw serial writes between CTS replies and data frames.
  • Update UART read loop to treat unsolicited CTS as harmless and to discard CRC-bad frames until a valid CPX packet is parsed.
  • Add fake-serial unit tests covering unsolicited CTS handling, CRC recovery, and CTS-gated writes.

Reviewed changes

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

File Description
cflib/cpx/transports.py Reworks UART CTS flow-control and improves read robustness (CRC discard + CTS handling).
test/cpx/test_uart_transport.py Adds focused unit tests for the updated UART flow-control and CRC recovery behavior.
test/cpx/__init__.py Marks test.cpx as a package to support test discovery/imports.

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

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.

Exception while reading transport, link probably closed? - Bolt UART CFLib Connection Issues

3 participants