Skip to content

Raise SerialException when using a port that is not open - #158

Open
Happyfield7 wants to merge 3 commits into
puddly:devfrom
Happyfield7:fix-closed-port-errors
Open

Happyfield7 wants to merge 3 commits into
puddly:devfrom
Happyfield7:fix-closed-port-errors

Conversation

@Happyfield7

@Happyfield7 Happyfield7 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Reading, writing, flushing, resetting buffers, reading byte counts or using modem pins on a sync port that is not open (never opened, or closed) fails with a bare AssertionError from an internal assert, which tells the caller nothing. Under python -O the asserts are removed and the same calls fail with TypeError or AttributeError instead. Some socket, RFC 2217 and ESPHome calls (flush, the byte counts, reset_write_buffer) silently succeed, so the mistake goes unnoticed.

Change

BaseSerial._check_broken() now first raises SerialException("Attempting to use a port that is not open"), pyserial's message, before re-raising a stored disconnect error; it runs at the start of the public sync methods and each backend's num_unread_bytes / num_unwritten_bytes. Async transports check only the stored disconnect error, since some never open their internal sync object. dtr / rts on a closed port keep their on-open values, and reconfigure_port keeps its own message.

Testing

Pair tests cover ten operations on a never-opened and a closed port; a compat test checks that serial.Serial raises serial.SerialException with the same message. Full suite passes on macOS and Debian sid, and python -O gives the same SerialException.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.40%. Comparing base (2c500d7) to head (5496120).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #158      +/-   ##
==========================================
+ Coverage   94.38%   94.40%   +0.01%     
==========================================
  Files          25       25              
  Lines        4437     4451      +14     
==========================================
+ Hits         4188     4202      +14     
  Misses        249      249              

☔ View full report in Codecov by Harness.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Happyfield7
Happyfield7 force-pushed the fix-closed-port-errors branch from f2c3e99 to fac2c20 Compare November 20, 2026 17:47
Comment thread serialx/common.py Resolved
@Happyfield7
Happyfield7 force-pushed the fix-closed-port-errors branch from 7f65f7b to 3b54103 Compare November 20, 2026 18:04
Comment thread serialx/common.py Resolved

This branch has not been deployed

No deployments
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