﻿# Asynchronous cleanup after a request span ends: retain a separate observation

A practical investigation of asynchronous cleanup after a request span ends: retain a separate observation, with explicit observations, control cases and evidence limits.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/field-observe-ended-request-asynchronous-cleanup/
Language: en

## The diagnostic situation

A streaming handler closes its request span when the client-facing operation finishes. Resource cleanup continues asynchronously. Instrumentation later tries to add cleanup events to the ended request span, so exported traces omit the very interval investigators need. The server can be behaving as designed while the diagnostic record falsely suggests that cleanup happened immediately or never ran.

## Why the observation changes meaning

The tracing API makes span completion a boundary for recording updates. Ending a span has no effect on its child spans, and an ended context can still participate in later relationships. None of this executes cancellation or resource release. Application task lifetime, span lifetime and export lifetime are separate mechanisms; treating them as one creates misleading empty tails.

## Define the instrumentation contract

Define whether the main span represents client response delivery or the full lifecycle including cleanup. If cleanup intentionally outlives it, give that work a separate span with a documented parent or link relationship. Preserve safe operation identity and cleanup outcome. Do not extend client-response duration merely to make the trace tree look visually nested when that changes the metric definition.

## A bounded verification procedure

Use a synthetic request whose cleanup waits on a controlled signal. End the response observation, release the cleanup signal, then inspect the test export. Verify that cleanup start, completion and failure belong to a recording span rather than late updates on an ended one. Repeat when the cleanup fails and when the worker terminates before it can finish.

## Evidence required for acceptance

Acceptance evidence shows the application task transitions and corresponding exported spans side by side. State which event ends each observation and which component owns it. Include an unresolved cleanup outcome for the interrupted fixture. The diagnostic must distinguish no recorded cleanup from a confirmed cleanup failure; an exporter or sampler can hide an otherwise executed asynchronous operation.

## Failure cases and limits

A child span extending beyond its parent is not automatically a broken trace or a resource leak. Conversely, a neatly closed trace is not evidence that memory was released. If cleanup is handed to another process, correlation and delivery need their own verification. This procedure tests the observation lifecycle and cannot replace an independent resource-state check after cancellation or completion.

## Primary references and related reading

Ending the request span does not end child work; cleanup needs its own lifecycle and correlation. [OpenTelemetry tracing API specification](https://opentelemetry.io/docs/specs/otel/trace/api/); [OpenTelemetry tracing SDK specification](https://opentelemetry.io/docs/specs/otel/trace/sdk/).

The proposed investigation of ended request asynchronous cleanup is not an executed deployment benchmark. Related reading: [Request cancellation: testing the full client-to-engine chain](/journal/provider-client-disconnect-cancellation-chain/); [Phoenix tracing in short-lived workers: ending a span does not guarantee export](/journal/fg2-phoenix-short-lived-worker-span-export-shutdown/).
