Skip to content

Commit 6395e9d

Browse files
committed
Test stop of server task.
1 parent b80ce22 commit 6395e9d

File tree

12 files changed

+354
-84
lines changed

12 files changed

+354
-84
lines changed

API_changes.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ PyModbus - API changes.
55
-------------
66
Version 3.1.0
77
-------------
8-
Added --host to client_* examples, to allow easier use.
9-
unit= in client calls are no longer converted to slave=, but raises a runtime exception.
10-
Added missing client calls (all standard request are not available as methods).
11-
client.mask_write_register() changed parameters.
8+
- Added --host to client_* examples, to allow easier use.
9+
- unit= in client calls are no longer converted to slave=, but raises a runtime exception.
10+
- Added missing client calls (all standard request are not available as methods).
11+
- client.mask_write_register() changed parameters.
12+
- server classes no longer accept reuse_port= (the socket do not accept it)
1213

1314
---------------------
1415
Version 3.0.1 / 3.0.2

check_ci.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -e
4+
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
5+
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
6+
7+
8+
codespell
9+
black --safe --quiet examples/ pymodbus/ test/
10+
isort .
11+
pylint --recursive=y examples pymodbus test
12+
flake8
13+
pytest
14+
echo "Ready to push"

pymodbus/client/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ async def close(self): # pylint: disable=invalid-overridden-method
315315
"""Close connection."""
316316
if self.transport:
317317
self.transport.close()
318+
while self.transport is not None:
319+
await asyncio.sleep(0.1)
318320
self._connected = False
319321

320322
def connection_lost(self, reason):

pymodbus/client/serial.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ def __init__(
7676

7777
async def close(self): # pylint: disable=invalid-overridden-method
7878
"""Stop connection."""
79-
if self.connected and self.protocol and self.protocol.transport:
80-
self.protocol.transport.close()
79+
if self.protocol:
80+
await self.protocol.close()
81+
await asyncio.sleep(0.1)
8182

8283
def _create_protocol(self):
8384
"""Create protocol."""
@@ -132,7 +133,9 @@ def protocol_lost_connection(self, protocol):
132133
_logger.error("Serial: protocol is not self.protocol.")
133134

134135
self._connected_event.clear()
135-
self.protocol = None
136+
if self.protocol is not None:
137+
del self.protocol
138+
self.protocol = None
136139
# if self.host:
137140
# asyncio.asynchronous(self._reconnect())
138141
else:

pymodbus/client/tcp.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ async def close(self): # pylint: disable=invalid-overridden-method
7070

7171
# prevent reconnect.
7272
self.delay_ms = 0
73-
74-
if self.connected and self.protocol and self.protocol.transport:
75-
self.protocol.transport.close()
73+
if self.protocol:
74+
await self.protocol.close()
75+
await asyncio.sleep(0.1)
7676

7777
def _create_protocol(self):
7878
"""Create initialized protocol instance with factory function."""
@@ -130,7 +130,9 @@ def protocol_lost_connection(self, protocol):
130130
)
131131

132132
self.connected = False
133-
self.protocol = None
133+
if self.protocol is not None:
134+
del self.protocol
135+
self.protocol = None
134136
if self.delay_ms > 0:
135137
asyncio.ensure_future(self._reconnect())
136138

pymodbus/client/udp.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ async def close(self): # pylint: disable=invalid-overridden-method
8989
"""
9090
# prevent reconnect:
9191
self.delay_ms = 0
92-
93-
if self.connected and self.protocol and self.protocol.transport:
94-
self.protocol.transport.close()
92+
if self.protocol:
93+
await self.protocol.close()
94+
await asyncio.sleep(0.1)
9595

9696
def _create_protocol(self, host=None, port=0):
9797
"""Create initialized protocol instance with factory function."""
@@ -157,7 +157,9 @@ def protocol_lost_connection(self, protocol):
157157
)
158158

159159
self.connected = False
160-
self.protocol = None
160+
if self.protocol is not None:
161+
del self.protocol
162+
self.protocol = None
161163
if self.delay_ms > 0:
162164
asyncio.create_task(self._reconnect())
163165
else:
@@ -201,7 +203,7 @@ def __init__(
201203
source_address: typing.Tuple[str, int] = None,
202204
**kwargs: any,
203205
) -> None:
204-
"""Initialize Asyncio Modbus UDP Client."""
206+
"""Initialize Modbus UDP Client."""
205207
super().__init__(framer=framer, **kwargs)
206208
self.params.host = host
207209
self.params.port = port

0 commit comments

Comments
 (0)