Skip to content
Merged
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: 5 additions & 0 deletions python/nemo_relay/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from __future__ import annotations

import asyncio
from contextlib import contextmanager
from datetime import datetime
from typing import Iterator
Expand Down Expand Up @@ -249,6 +250,10 @@ def scope(
)
yield pushed_handle
status_code = "OK"
except asyncio.CancelledError as e:
status_code = "ERROR"
status_message = str(e) or "cancelled"
raise
except Exception as e:
status_code = "ERROR"
status_message = str(e)
Expand Down
41 changes: 41 additions & 0 deletions python/tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

"""Tests for NeMo Relay scope operations."""

import asyncio

import pytest

from nemo_relay import (
ScopeAttributes,
ScopeHandle,
ScopeType,
scope,
subscribers,
)


Expand Down Expand Up @@ -100,6 +103,44 @@ def test_scope_ctx_mgr(self):

assert scope.get_handle().name == "root"

@pytest.mark.parametrize(
("cancel_message", "expected_status_description"),
[
(None, "cancelled"),
("shutdown", "shutdown"),
],
)
async def test_scope_ctx_mgr_cancelled_task_sets_error_status(
self,
subscribed_events,
cancel_message,
expected_status_description,
):
async def cancel_within_scope() -> None:
with scope.scope("cancelled_scope", ScopeType.Agent):
task = asyncio.current_task()
assert task is not None
if cancel_message is None:
task.cancel()
else:
task.cancel(cancel_message)
await asyncio.sleep(0)

with pytest.raises(asyncio.CancelledError):
await cancel_within_scope()

await subscribers.flush_async()
assert len(subscribed_events) == 2
end = subscribed_events[-1]
assert end.name == "cancelled_scope"
assert end.kind == "scope"
assert end.category == "agent"
assert end.scope_category == "end"
assert end.metadata == {
"otel.status_code": "ERROR",
"otel.status_description": expected_status_description,
}

Comment thread
mnajafian-nv marked this conversation as resolved.

class TestAllScopeTypes:
@pytest.mark.parametrize(
Expand Down
Loading