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

# Empty traces with flush race conditions

> [!NOTE]
>
> **Applies to:**
>
> * Plan - Starter, Pro, Enterprise
> * Deployment - Braintrust-hosted, Self-hosted
> *
> * Use case - Logging with async batch operations in short-lived processes (serverless functions, scripts, containers)

## 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 ](https://docs.braintrust.dev/instrument/advanced-tracing.md)
* 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


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.