Skip to content

Commit

Permalink
Delay self.loop until connect(). (#1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Jun 5, 2023
1 parent a428399 commit 2affedc
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ def __init__(
self.reconnect_delay_current: float = 0
self.transport: asyncio.BaseTransport | asyncio.Server = None
self.protocol: asyncio.BaseProtocol = None
self.loop: asyncio.AbstractEventLoop = None
with suppress(RuntimeError):
self.loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
self.loop = asyncio.get_running_loop()
self.reconnect_timer: asyncio.Task = None
self.recv_buffer: bytes = b""
self.call_connect_listen: Callable[[], Coroutine[Any, Any, Any]] = lambda: None
Expand Down Expand Up @@ -263,6 +264,8 @@ def setup_serial(
async def transport_connect(self):
"""Handle generic connect and call on to specific transport connect."""
Log.debug("Connecting {}", self.comm_params.comm_name)
if not self.loop:
self.loop = asyncio.get_running_loop()
self.transport, self.protocol = None, None
try:
self.transport, self.protocol = await asyncio.wait_for(
Expand Down Expand Up @@ -296,6 +299,8 @@ def connection_made(self, transport: asyncio.BaseTransport):
:param transport: socket etc. representing the connection.
"""
Log.debug("Connected to {}", self.comm_params.comm_name)
if not self.loop:
self.loop = asyncio.get_running_loop()
self.transport = transport
self.reset_delay()
self.cb_connection_made()
Expand Down

0 comments on commit 2affedc

Please sign in to comment.