Skip to content

Regression: sys.exit(0) sets span error to SystemExit #4493

Closed
@grihabor

Description

@grihabor

Describe your environment

OS: (e.g, Ubuntu)
Python version: (e.g., Python 3.13.2)
SDK version: (e.g., 1.31.0)
API version: (e.g., 1.31.0)

What happened?

After upgrade from 1.30.0 to 1.31.0 this code started producing a span with SystemExit error:

with tracer.start_as_current_span("test-exit-code"):
    sys.exit(0)

Related to #4406

Steps to Reproduce

def main():
    resource = Resource(attributes={SERVICE_NAME: "test-exit-code"})
    tracerProvider = TracerProvider(resource=resource)
    stream = StringIO()
    processor = BatchSpanProcessor(ConsoleSpanExporter(out=stream))
    tracerProvider.add_span_processor(processor)
    trace.set_tracer_provider(tracerProvider)
    tracer = trace.get_tracer(__name__)
    try:
        with tracer.start_as_current_span("test-exit-code"):
            sys.exit(0)
    except SystemExit:
        pass

    while stream.getvalue() == "":
        time.sleep(0.5)

    span = json.loads(stream.getvalue())
    print(span)

Expected Result

  {
      "name": "test-exit-code",
      "context": {
          "trace_id": "0x644b4d5832f13a490133e4b44d72a476",
          "span_id": "0x34b0785025ce90ab",
          "trace_state": "[]"
      },
      "kind": "SpanKind.INTERNAL",
      "parent_id": null,
      "start_time": "2025-03-18T13:02:28.415717Z",
      "end_time": "2025-03-18T13:02:28.415733Z",
      "status": {
          "status_code": "UNSET"
      },
      "attributes": {},
      "events": [],
      "links": [],
      "resource": {
          "attributes": {
              "service.name": "test-exit-code"
          },
          "schema_url": ""
      }
  }

Actual Result

  {
      "name": "test-exit-code",
      "context": {
          "trace_id": "0xaa4145b80399a253002020b3afc45226",
          "span_id": "0xe492dbcd05893ead",
          "trace_state": "[]"
      },
      "kind": "SpanKind.INTERNAL",
      "parent_id": null,
      "start_time": "2025-03-18T13:03:48.253080Z",
      "end_time": "2025-03-18T13:03:48.254344Z",
      "status": {
          "status_code": "ERROR",
          "description": "SystemExit: 0"
      },
      "attributes": {},
      "events": [
          {
              "name": "exception",
              "timestamp": "2025-03-18T13:03:48.254331Z",
              "attributes": {
                  "exception.type": "SystemExit",
                  "exception.message": "0",
                  "exception.stacktrace": "Traceback (most recent call last):\n  File \"/home/grihabor/.cache/pants/named_caches/pex_root/venvs/1/s/7156ea6e/venv/lib/python3.11/site-packages/opentelemetry/ace/__init__.py\", line 587, in use_span\n    yield span\n  File \"/home/grihabor/.cache/pants/named_caches/pex_root/venvs/1/s/7156ea6e/venv/lib/python3.11/site-packages/opentelemetry/sdk/trace/__init__.\", line 1105, in start_as_current_span\n    yield span\n  File \"src/python/observability/otel/exit_test.py\", line 21, in test_exit_code\n    sys.exit(0)\nSystemExit: 0\n",
                  "exception.escaped": "False"
              }
          }
      ],
      "links": [],
      "resource": {
          "attributes": {
              "service.name": "test-exit-code"
          },
          "schema_url": ""
      }
  }

Additional context

No response

Would you like to implement a fix?

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions