> ## Documentation Index
> Fetch the complete documentation index at: https://braintrust.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Empty traces with flush race conditions

export const plans_0 = "Starter, Pro, Enterprise"

export const deployments_0 = "Braintrust-hosted, Self-hosted"

export const data_plane_version_0 = undefined

export const use_case_0 = "Use case - Logging with async batch operations in short-lived processes (serverless functions, scripts, containers)"

<Note>
  **Applies to:**

  * Plan - {plans_0}
  * Deployment - {deployments_0}
  * {data_plane_version_0}
  * {use_case_0}
</Note>

## Summary

**Issue:** Traces display empty fields or remain "in progress" indefinitely.

**Cause:** Application terminates before `flush()` completes or spans aren't properly ended, particularly in serverless/container environments.

**Resolution:** Explicitly call `flush()` before termination and ensure all spans are ended.

## Resolution Steps

### Step 1: Use context managers (recommended)

#### Python

```text theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
with logger.start_span(name="operation") as span:
    span.log(input=input_data, output=output_data, metadata=metadata)
    # span.end() called automatically

```

#### TypeScript

```javascript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
await logger.traced(async (span) => {
  span.log({ input: inputData, output: outputData, metadata });
  // span.end() called automatically
});

```

### Step 2: Call flush() explicitly before process exit

#### Python

```python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
def cleanup_braintrust():
    try:
        project.logger.flush()  # Blocks until upload completes
    except Exception:
        logging.exception("Failed to flush Braintrust project")

```

#### TypeScript

```javascript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
async function cleanupBraintrust() {
  try {
    await project.logger.flush(); // Waits until upload completes
  } catch (error) {
    console.error("Failed to flush Braintrust project:", error);
  }
}

```

### Key Points

* `flush()` [waits for completion ](/instrument/advanced-tracing)
* Context managers automatically call `span.end()`
* Manual spans: call `span.end()` in finally blocks or they remain incomplete
* For serverless functions, call `flush()` before the handler returns
