Skip to content

usb-device-cdc: Fix default timeout in CDCInterface.init().#1080

Merged
dpgeorge merged 1 commit into
micropython:masterfrom
andrewleech:fix-cdc-default-timeout
Jul 11, 2026
Merged

usb-device-cdc: Fix default timeout in CDCInterface.init().#1080
dpgeorge merged 1 commit into
micropython:masterfrom
andrewleech:fix-cdc-default-timeout

Conversation

@andrewleech

Copy link
Copy Markdown
Contributor

Summary

CDCInterface is broken when constructed with default arguments — calling read(), write(), readinto(), or flush() raises TypeError: unsupported types for __ge__: 'int', 'NoneType' because self._timeout ends up as None.

__init__ sets self._timeout = 1000 then immediately calls self.init(**kwargs). The init() method has timeout=None as default and unconditionally assigns self._timeout = timeout, overwriting the 1000 with None. All the stream methods then fail on time.ticks_diff(...) >= self._timeout.

Fix is changing the default parameter from timeout=None to timeout=1000, consistent with how the other init parameters (baudrate, bits, parity, stop) all carry their real defaults in the signature.

Testing

Tested on ESP32-S3 as USB CDC device with an MIMXRT1170-EVK as USB host. Confirmed cdc._timeout is now 1000 after default construction and read(), write(), readinto() all work without error. Also verified explicit timeout= parameter still takes effect.

@dpgeorge dpgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix looks correct.

The reason it works with the provided CDC example (examples/device/cdc_repl_example.py) is because that example does:

cdc = CDCInterface()
cdc.init(timeout=0)  # zero timeout makes this non-blocking, suitable for os.dupterm()

CDCInterface.__init__() sets self._timeout = 1000, then calls
self.init(**kwargs). The init() method had timeout=None as default,
which unconditionally overwrites self._timeout with None. This causes
TypeError in read(), write(), readinto(), and ioctl() which all compare
int >= self._timeout.

Set the default timeout=1000 in init() to match the intended default,
consistent with how other parameters (baudrate, bits, etc.) have their
defaults specified directly in the init() signature.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@dpgeorge dpgeorge force-pushed the fix-cdc-default-timeout branch from 8803586 to fb3efd9 Compare July 11, 2026 13:41
@dpgeorge dpgeorge merged commit fb3efd9 into micropython:master Jul 11, 2026
5 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.

3 participants