Skip to content

Commit 33f6573

Browse files
committed
Prevent race condition
1 parent d387d8f commit 33f6573

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,15 @@ async def _exit_with_timer(self):
842842
"""
843843
try:
844844
if self.shutdown_timer is not None:
845+
logger.debug("Exiting with timer")
845846
await asyncio.sleep(self.shutdown_timer)
846-
logger.debug("Exiting with timer")
847-
await self.shutdown()
847+
if (
848+
self.state != State.CONNECTING
849+
and self._sending.qsize() == 0
850+
and not self._received_subscriptions
851+
and self._waiting_for_response <= 0
852+
):
853+
await self.shutdown()
848854
except asyncio.CancelledError:
849855
pass
850856

@@ -985,6 +991,7 @@ async def unsubscribe(
985991
original_id = get_next_id()
986992
while original_id in self._in_use_ids:
987993
original_id = get_next_id()
994+
logger.debug(f"Unwatched extrinsic subscription {subscription_id}")
988995
self._received_subscriptions.pop(subscription_id, None)
989996

990997
to_send = {

tests/helpers/proxy_server.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99

1010

1111
class ProxyServer:
12-
def __init__(self, upstream: str, time_til_pause: float, time_til_resume: float, port: int = 8080):
12+
def __init__(
13+
self,
14+
upstream: str,
15+
time_til_pause: float,
16+
time_til_resume: float,
17+
port: int = 8080,
18+
):
1319
self.upstream_server = upstream
1420
self.time_til_pause = time_til_pause
1521
self.time_til_resume = time_til_resume

tests/integration_tests/test_async_substrate_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ async def test_query_map_with_odd_number_of_params():
200200
async def test_improved_reconnection():
201201
def get_free_port():
202202
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
203-
s.bind(('', 0)) # Bind to port 0 = OS picks free port
203+
s.bind(("", 0)) # Bind to port 0 = OS picks free port
204204
s.listen(1)
205205
port_ = s.getsockname()[1]
206206
return port_

0 commit comments

Comments
 (0)