Skip to content

Bug: nested subsegments don't work across threads #388

Open
@rubenfonseca

Description

@rubenfonseca

We're tracking a bug where nested subsegments don't work across threads, even when using the official suggestion in the documentation.

We've managed to reproduce the issue with some minimal code:

Code snippet

from concurrent.futures import ThreadPoolExecutor

from aws_xray_sdk.core import xray_recorder

@xray_recorder.capture("## lambda handler")
def lambda_handler(event, context):
    with ThreadPoolExecutor() as pool:
        @xray_recorder.capture("## f")
        def f():
            @xray_recorder.capture("## h")
            def h():
                pass

            def g(trace_entity):
                xray_recorder.set_trace_entity(trace_entity)
                h()
                xray_recorder.clear_trace_entities()

            curr = xray_recorder.get_trace_entity()
            pool.submit(g, curr)

This produces the following XRay traces:

XRay trace

XRay Event at (2023-04-05T16:32:52.012000) with id (1-642d8694-731f0a1a6d204264677086db) and duration (0.388s)
 - 0.388s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP [HTTP: 200]
 - 0.021s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP
   - 0.223s - Initialization
   - 0.020s - Invocation
     - 0.019s - ## lambda handler
       - 0.018s - ## f
     - 0.000s - ## h
   - 0.000s - Overhead

The expected output would be for the "h" trace to be nested under "f":

Expected output

XRay Event at (2023-04-05T16:32:52.012000) with id (1-642d8694-731f0a1a6d204264677086db) and duration (0.388s)
 - 0.388s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP [HTTP: 200]
 - 0.021s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP
   - 0.223s - Initialization
   - 0.020s - Invocation
     - 0.019s - ## lambda handler
       - 0.018s - ## f
         - 0.000s - ## h
   - 0.000s - Overhead

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions