Skip to content

Commit 19eea27

Browse files
committed
Bug fixes
1 parent 1a1979d commit 19eea27

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ def __init__(
586586
self._max_retries = max_retries
587587
self._last_activity = asyncio.Event()
588588
self._last_activity.set()
589+
self._waiting_for_response = 0
589590

590591
@property
591592
def state(self):
@@ -599,6 +600,14 @@ async def __aenter__(self):
599600
await self.connect()
600601
return self
601602

603+
async def mark_waiting_for_response(self):
604+
async with self._lock:
605+
self._waiting_for_response += 1
606+
607+
async def mark_response_received(self):
608+
async with self._lock:
609+
self._waiting_for_response -= 1
610+
602611
@staticmethod
603612
async def loop_time() -> float:
604613
return asyncio.get_running_loop().time()
@@ -793,7 +802,12 @@ async def _handler(self, ws: ClientConnection) -> Union[None, Exception]:
793802

794803
async def __aexit__(self, exc_type, exc_val, exc_tb):
795804
if self.shutdown_timer is not None:
796-
if self.state != State.CONNECTING:
805+
if (
806+
self.state != State.CONNECTING
807+
and self._sending.qsize() == 0
808+
and not self._received_subscriptions
809+
and self._waiting_for_response <= 0
810+
):
797811
if self._exit_task is not None:
798812
self._exit_task.cancel()
799813
try:
@@ -812,6 +826,7 @@ async def _exit_with_timer(self):
812826
try:
813827
if self.shutdown_timer is not None:
814828
await asyncio.sleep(self.shutdown_timer)
829+
logger.debug("Exiting with timer")
815830
await self.shutdown()
816831
except asyncio.CancelledError:
817832
pass
@@ -2495,6 +2510,7 @@ async def _make_rpc_request(
24952510
logger.debug(
24962511
f"Submitted payload ID {payload['id']} with websocket ID {item_id}: {output_payload}"
24972512
)
2513+
await ws.mark_waiting_for_response()
24982514

24992515
while True:
25002516
for item_id in request_manager.unresponded():
@@ -2552,6 +2568,7 @@ async def _make_rpc_request(
25522568
)
25532569

25542570
if request_manager.is_complete:
2571+
await ws.mark_response_received()
25552572
break
25562573
else:
25572574
await asyncio.sleep(0.01)
@@ -3948,6 +3965,7 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
39483965
}
39493966

39503967
if "finalized" in message_result and wait_for_finalization:
3968+
logger.debug("Extrinsic finalized. Unsubscribing.")
39513969
async with self.ws as ws:
39523970
await ws.unsubscribe(subscription_id)
39533971
return {
@@ -3956,14 +3974,17 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
39563974
"finalized": True,
39573975
}, True
39583976
elif (
3959-
"inblock" in message_result
3977+
any(x in message_result for x in ["inblock", "inBlock"])
39603978
and wait_for_inclusion
39613979
and not wait_for_finalization
39623980
):
3981+
logger.debug("Extrinsic included. Unsubscribing.")
39633982
async with self.ws as ws:
39643983
await ws.unsubscribe(subscription_id)
39653984
return {
3966-
"block_hash": message_result["inblock"],
3985+
"block_hash": message_result.get(
3986+
"inblock", message_result.get("inBlock")
3987+
),
39673988
"extrinsic_hash": "0x{}".format(extrinsic.extrinsic_hash.hex()),
39683989
"finalized": False,
39693990
}, True

0 commit comments

Comments
 (0)