Repository navigation
Conversation
…(issue pymodbus-dev#3109) - Add CancelIoEx Windows API integration to cancel pending overlapped I/O operations - Define OVERLAPPED structure for Windows I/O completion - Update close() to cancel pending I/O before closing the port - Update flush() to wait for pending writes on Windows - Fixes Windows serial port lock issue when USB adapter is unplugged Fixes pymodbus-dev#3109
janiversen
requested changes
Dec 5, 2026
janiversen
left a comment
Collaborator
There was a problem hiding this comment.
pylint fails, please run ./check_ci.sh locally that way you detect problems much earlier.
janiversen
requested changes
Dec 5, 2026
| with contextlib.suppress(ImportError): | ||
| import serial | ||
|
|
||
| # Windows-specific imports for canceling I/O |
Collaborator
There was a problem hiding this comment.
This will not pass CI, due to the 100% test coverage demand. It needs to be marked as not relevant for the coverage run.
Where are the test cases, that ensures this actually works ??
This branch has not been deployed
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.
Summary
Fixed Windows serial port lock issue when USB adapter is unplugged. The problem was that
SerialTransportopens the port withwrite_timeout=0, making each write an overlappedWriteFilethat is not waited for. When the USB adapter is unplugged while the client is polling, pending writes never complete. When the client is closed, pyserial only cancels the last pending I/O operation, leaving others pending and keeping the port locked until the process exits.Root Cause
On Windows,
SerialTransportuseswrite_timeout=0which makes each write an overlappedWriteFileoperation. When a USB-serial adapter is unplugged during polling:client.close()is called, pyserial'sclose()only cancels the last pending I/OSolution
Modified
SerialTransportinpymodbus/transport/serialtransport.py:Added Windows CancelIoEx integration: Uses
kernel32.CancelIoEx()to cancel all pending overlapped I/O operations when the transport is closedDefined OVERLAPPED structure: Added proper
OVERLAPPEDstructure definition for Windows I/O completion (usingctypes.c_ulonglongforULONG_PTRsincewintypes.ULONG_PTRis not available)Enhanced
close()method: Before closing the serial port, callsCancelIoEx(handle, NULL)to cancel all pending I/O operations on the portEnhanced
flush()method: On Windows, waits up to 1 second for pending writes to complete before clearing the bufferChanges
pymodbus/transport/serialtransport.py: Added Windows CancelIoEx integration, OVERLAPPED structure definition, and enhanced close/flush methodsTesting
os.name == "nt"Testing Notes
Fixes #3109