Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed .close() method for TonLibClient #16

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions ton/tonlibjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def __init__(self, loop, ls_index, cdll_path=None, verbosity_level=0):
self._state = None # None, "finished", "crashed", "stuck"

self.is_dead = False

# creating tasks
self.read_results_task = self.loop.create_task(self.read_results())
self.del_expired_futures_task = self.loop.create_task(self.del_expired_futures_loop())
Expand Down Expand Up @@ -161,7 +160,6 @@ def _execute(self, query, timeout=10):

future_result = self.loop.create_future()
self.futures[extra_id] = future_result

self.loop.run_in_executor(None, lambda: self.send(query))
return future_result

Expand All @@ -181,11 +179,16 @@ async def execute(self, query, timeout=10):
def _is_working(self):
return self._state not in ('crashed', 'stuck', 'finished')

@property
def _is_closing(self):
return self._state == 'finishing'

async def close(self):
try:
self._state = 'finishing'
await self.read_results_task
await self.del_expired_futures_task
self._state = 'finished'
await self.read_results_task()
await self.del_expired_futures_task()
except Exception as ee:
logger.error(f"Exception in tonlibjson.close: {traceback.format_exc()}")
raise RuntimeError(f'Error in tonlibjson.close: {ee}')
Expand All @@ -207,7 +210,7 @@ async def read_results(self):
delta = 5
receive_func = functools.partial(self.receive, timeout)
try:
while self._is_working:
while self._is_working and not self._is_closing:
# return reading result
result = None
try:
Expand Down Expand Up @@ -235,10 +238,10 @@ async def read_results(self):

async def del_expired_futures_loop(self):
try:
while self._is_working:
while self._is_working and not self._is_closing:
self.cancel_futures()
await asyncio.sleep(1)

self.cancel_futures(cancel_all=True)
except Exception as ee:
logger.critical(f'Task del_expired_futures_loop failed: {ee}')
logger.critical(f'Task del_expired_futures_loop failed: {ee}')