Close tracing exporter during BatchTraceProcessor.shutdown()#3470
Close tracing exporter during BatchTraceProcessor.shutdown()#3470mshsheikh wants to merge 1 commit into
BatchTraceProcessor.shutdown()#3470Conversation
### Summary
This PR ensures that `BatchTraceProcessor.shutdown()` closes the tracing exporter after the final batch has been flushed.
### Problem
`BackendSpanExporter` creates a persistent `httpx.Client` for trace ingestion. Before this change, shutdown drained the queue but did not explicitly close the exporter, which left the HTTP client and connection pool open.
That can leave resources allocated longer than necessary, especially in:
* long-running services
* tests that repeatedly create and tear down tracing components
* worker processes or environments with frequent startup and shutdown cycles
### Change
After the final synchronous flush, the shutdown path now looks up `close()` on the exporter and calls it when available:
```python
close_fn = getattr(self._exporter, "close", None)
if callable(close_fn):
try:
close_fn()
except Exception as exc:
logger.warning(
"[non-fatal] Tracing: exporter close failed: %s",
exc,
)
```
### Behavior
* preserves the existing final flush behavior
* closes exporter resources after export completes
* keeps cleanup non-fatal
* remains compatible with custom exporters that do not implement `close()`
### Notes
This change assumes the exporter is safe to close at processor shutdown time. If exporter ownership is later shared more broadly, that lifecycle may need to be made explicit.
|
Hi @seratch , just wanted to give this a friendly ping since it's been open for a few weeks. All checks are passing, and this should safely clean up the HTTP client resources during |
|
Thanks for the contribution. I confirmed that More importantly, I am going to close this PR. If a minimal repeated in-process reproduction shows measurable resource growth or warnings, we can revisit a narrower fix for the SDK-owned default exporter after its worker has stopped. |
Summary
This PR ensures that
BatchTraceProcessor.shutdown()closes the tracing exporter after the final batch has been flushed.Problem
BackendSpanExportercreates a persistenthttpx.Clientfor trace ingestion. Before this change, shutdown drained the queue but did not explicitly close the exporter, which left the HTTP client and connection pool open.That can leave resources allocated longer than necessary, especially in:
Change
After the final synchronous flush, the shutdown path now looks up
close()on the exporter and calls it when available:Behavior
close()Notes
This change assumes the exporter is safe to close at processor shutdown time. If exporter ownership is later shared more broadly, that lifecycle may need to be made explicit.