Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 1 addition & 4 deletions src/ghoshell_moss/core/concepts/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,9 @@ async def recursive_start(_chan: Channel) -> None:

async def recursive_close(_chan: Channel) -> None:
children = _chan.children()
if len(children) == 0:
return
group_stop = []
for child in children.values():
if not child.is_running():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if-condition is inverted

group_stop.append(recursive_close(child))
group_stop.append(recursive_close(child))
await asyncio.gather(*group_stop)
if _chan.is_running():
await _chan.broker.close()
Expand Down
30 changes: 29 additions & 1 deletion tests/channels/test_thread_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ async def test_thread_channel_start_and_close():
assert not provider.is_running()


@pytest.mark.asyncio
async def test_channel_run_in_ctx_closes_child_channels():
root = PyChannel(name="root")
child = root.new_child("child")
grandchild = child.new_child("grandchild")

async with root.run_in_ctx():
assert root.is_running()
assert child.is_running()
assert grandchild.is_running()

assert not root.is_running()
assert not child.is_running()
assert not grandchild.is_running()


@pytest.mark.asyncio
async def test_thread_channel_raise_in_proxy():
provider, proxy = create_thread_channel("client")
Expand All @@ -33,13 +49,25 @@ async def test_thread_channel_run_in_thread():
provider, proxy = create_thread_channel("client")
chan = PyChannel(name="provider")
provider.run_in_thread(chan)

await provider.aclose()
await provider.wait_closed()
assert not chan.is_running()
assert not provider.is_running()


@pytest.mark.asyncio
async def test_thread_channel_run_in_ctx():
provider, proxy = create_thread_channel("client")
chan = PyChannel(name="provider")
async with provider.run_in_ctx(chan):
async with proxy.run_in_ctx():
await proxy.broker.wait_connected()
assert chan.is_running()
assert provider.is_running()
assert not chan.is_running()
assert not provider.is_running()


@pytest.mark.asyncio
async def test_thread_channel_run_in_tasks():
provider, proxy = create_thread_channel("client")
Expand Down
Loading