Mirror linked task behaviour from other implementations: default to terminating facet on task end, and also terminate facet on task exception

This commit is contained in:
Tony Garnock-Jones 2024-03-30 09:07:09 +01:00
parent 08e49fd14e
commit 3035b43941
2 changed files with 9 additions and 3 deletions

View File

@ -29,6 +29,8 @@ def main():
@syndicate.relay.connect(args.address, sturdy.SturdyRef.decode(syndicate.parse(args.cap)))
def on_connected(ds):
turn.on_stop(lambda: turn.stop(root_facet))
me = 'user_' + str(random.randint(10, 1000))
turn.publish(ds, Present(me))
@ -48,4 +50,3 @@ def main():
await f.loop.connect_read_pipe(lambda: asyncio.StreamReaderProtocol(reader), sys.stdin)
while line := (await reader.readline()).decode('utf-8'):
turn.external(f, lambda: turn.send(ds, Says(me, line.strip())))
turn.external(f, lambda: turn.stop(root_facet))

View File

@ -251,15 +251,20 @@ class Facet:
task.cancel()
task = None
async def guarded_task():
should_terminate_facet = True
try:
await coro_fn(self)
if await coro_fn(self) is True:
should_terminate_facet = False
except asyncio.CancelledError:
pass
except:
import traceback
traceback.print_exc()
finally:
Turn.external(self, cancel_linked_task)
if should_terminate_facet:
Turn.external(self, lambda: Turn.active.stop())
else:
Turn.external(self, cancel_linked_task)
task = self.loop.create_task(guarded_task())
self.linked_tasks.append(task)