Skip to content

Make atexit flush registration optional #29

@AbhiPrasad

Description

Summary

Add an option to disable the automatic atexit.register() flush handler in the Python SDK's _HTTPBackgroundLogger, allowing users who manage their own program lifecycle to control when buffered spans are flushed.

Context

Use case: Users running Braintrust in Kubernetes environments with self-restart mechanisms want explicit control over when the final flush happens. The current hardcoded atexit.register(self._finalize) at logger.py:1038 runs unconditionally, blocking pod restarts during peak hours while the SDK waits for buffered spans to flush (including any masking API calls). Example scenario: K8s pods have a self-restart mechanism, and during peak workload there are many buffered logs + pseudo-masking calls (~1.5s/call to an external API). The atexit flush delays pod restarts significantly.

Current workaround: Call braintrust.flush() explicitly before pod exit (e.g., in a pre-stop hook) so the queue is empty when atexit fires. This gives partial control but doesn't fully disable the hook. There's no supported way to fully disable atexit registration without accessing private internals (_internal_get_global_state()._global_bg_logger.get()._finalize) and calling atexit.unregister() manually — which is fragile and unsupported.

Component: Python SDK (braintrust/sdk/py/src/braintrust/logger.py:1038)

Requirements

Must have:

  • A way to opt out of atexit registration (e.g., env var BRAINTRUST_DISABLE_ATEXIT_FLUSH=true, or parameter on login()/init())
  • When opted out, the SDK should not register any atexit handler; user is responsible for calling braintrust.flush() at shutdown

Should, could, or won't have:

  • A public API to re-register/unregister the atexit handler at runtime

Linear issue: https://linear.app/braintrustdata/issue/BRA-4222/python-sdk-make-atexit-flush-registration-optional

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions