Skip to content

Commit 6062ff8

Browse files
fix: handle timeout on connection close (#1)
1 parent 633342f commit 6062ff8

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

examples/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def example() -> None:
1414
response = await conn.send_modbus_message(example_message)
1515

1616
assert response is not None, "we expect a response from ReadCoils"
17-
print(response.data)
17+
print(response.data) # noqa: T201
1818

1919

2020
if __name__ == "__main__":

tcp_modbus_aio/client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
import time
77
import uuid
88
from dataclasses import dataclass
9-
from types import TracebackType
10-
from typing import Any, ClassVar
9+
from typing import TYPE_CHECKING, Any, ClassVar
1110

1211
from cachetools import TTLCache
13-
from typing_extensions import Self
14-
from umodbus.functions import ModbusFunction
1512

1613
from tcp_modbus_aio.exceptions import (
1714
ModbusCommunicationFailureError,
@@ -24,6 +21,12 @@
2421
create_function_from_response_pdu,
2522
)
2623

24+
if TYPE_CHECKING:
25+
from types import TracebackType
26+
27+
from typing_extensions import Self
28+
from umodbus.functions import ModbusFunction
29+
2730

2831
@dataclass
2932
class CoilWatchStatus:
@@ -308,7 +311,16 @@ async def clear_tcp_connection(self) -> None:
308311
)
309312

310313
self._writer.close()
311-
await self._writer.wait_closed()
314+
315+
try:
316+
await self._writer.wait_closed()
317+
except TimeoutError:
318+
if self.logger is not None:
319+
self.logger.warning(
320+
f"[{self}][clear_tcp_connection] connection close timed out, continuing anyway"
321+
)
322+
323+
pass
312324

313325
self._reader = None
314326
self._writer = None

0 commit comments

Comments
 (0)