Skip to content
Closed
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
6 changes: 5 additions & 1 deletion src/agents/tracing/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ class TraceImpl(Trace):
"_prev_context_token",
"_processor",
"_started",
"_finished",
)

def __init__(
Expand All @@ -516,6 +517,7 @@ def __init__(
self._prev_context_token: contextvars.Token[Trace | None] | None = None
self._processor = processor
self._started = False
self._finished = False

@property
def trace_id(self) -> str:
Expand Down Expand Up @@ -544,7 +546,9 @@ def finish(self, reset_current: bool = False):
if not self._started:
return

self._processor.on_trace_end(self)
if not self._finished:
self._processor.on_trace_end(self)
self._finished = True

if reset_current and self._prev_context_token is not None:
Scope.reset_current_trace(self._prev_context_token)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_trace_finish_idempotent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

from unittest.mock import MagicMock

from agents.tracing.scope import Scope
from agents.tracing.traces import TraceImpl


def test_trace_finish_notifies_processor_once_and_can_reset_later() -> None:
processor = MagicMock()
trace = TraceImpl(
name="workflow",
trace_id="trace_test",
group_id=None,
metadata=None,
processor=processor,
)

trace.start(mark_as_current=True)
trace.finish()
trace.finish(reset_current=True)

processor.on_trace_end.assert_called_once_with(trace)
assert Scope.get_current_trace() is None