Skip to content

Bug: @trace ends async function spans before the coroutine runs #2016

Backlog

Description

@Twiink

Component

OpenLIT Python SDK

What happened?

The decorator exports the manual span before the coroutine runs, records a coroutine object as output, and loses child-span parentage and later exceptions. Real OpenTelemetry spans on current main report one span ended before await and a child with no parent.

Steps to reproduce

On main at 463a1c1, run:

import asyncio
import json
import openlit
openlit.OpenlitConfig()
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
provider=TracerProvider(); exporter=InMemorySpanExporter(); provider.add_span_processor(SimpleSpanProcessor(exporter)); trace.set_tracer_provider(provider)
@openlit.trace
async def booking():
    await asyncio.sleep(0)
    with provider.get_tracer('booking').start_as_current_span('reserve-room'):
        return 'booked'
async def main():
    operation=booking()
    ended_before_execution=len(exporter.get_finished_spans())
    assert await operation=='booked'
    spans=exporter.get_finished_spans()
    child=next(s for s in spans if s.name=='reserve-room')
    print(json.dumps({'spans_ended_before_await':ended_before_execution,'child_has_parent':child.parent is not None,'names':[s.name for s in spans]}))
    assert ended_before_execution==0
asyncio.run(main())

Actual output is recorded by the assertions. No provider requests are needed.

Expected behavior

Keep the manual span active until the coroutine completes, record its resolved output/errors, and retain parent/child relationships.

Environment

Python 3.12.14, OpenLIT SDK 1.45.0 from current main, OpenTelemetry SDK 1.45.2, Linux.

Additional context

Checked all-state trace/manual tracing/async PRs. #2010 concerns the separate using_attributes decorator and leaves trace unchanged.

Investigation and execution used OpenAI Codex assistance. I can prepare a focused fix with regression tests.

  • I searched existing issues and did not find a duplicate
  • Yes, I am willing to submit a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions