Skip to content

Commit 73bda8a

Browse files
committed
Feat: Make atexit flush registration optional
1 parent 992e036 commit 73bda8a

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

py/src/braintrust/logger.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,11 @@ def __init__(self, api_conn: LazyValue[HTTPConnection]):
10261026
except:
10271027
self.all_publish_payloads_dir = None
10281028

1029+
try:
1030+
disable_atexit_flush = os.environ["BRAINTRUST_DISABLE_ATEXIT_FLUSH"].lower() in ("true", "1", "yes")
1031+
except:
1032+
disable_atexit_flush = False
1033+
10291034
self.start_thread_lock = threading.RLock()
10301035
self.thread = threading.Thread(target=self._publisher, daemon=True)
10311036
self.started = False
@@ -1036,7 +1041,8 @@ def __init__(self, api_conn: LazyValue[HTTPConnection]):
10361041
# Counter for tracking overflow uploads (useful for testing)
10371042
self._overflow_upload_count = 0
10381043

1039-
atexit.register(self._finalize)
1044+
if not disable_atexit_flush:
1045+
atexit.register(self._finalize)
10401046

10411047
def enforce_queue_size_limit(self, enforce: bool) -> None:
10421048
"""

py/src/braintrust/test_logger.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,41 @@ def test_init_with_repo_info_does_not_raise(self):
124124
assert metadata.project.id == "test-project-id"
125125
assert metadata.experiment.name == "test-exp"
126126

127+
def test_init_enable_atexit_flush(self):
128+
from braintrust.logger import _HTTPBackgroundLogger
129+
130+
api_con_response = lambda: {
131+
"project": {"id": "test-project-id", "name": "test-project"},
132+
"experiment": {"id": "test-exp-id", "name": "test-exp"},
133+
}
134+
135+
with patch("atexit.register") as mock_register:
136+
_HTTPBackgroundLogger(LazyValue(api_con_response, use_mutex=False)) # type: ignore
137+
mock_register.assert_called()
138+
139+
def test_init_disable_atexit_flush(self):
140+
from braintrust.logger import _HTTPBackgroundLogger
141+
142+
api_con_response = lambda: {
143+
"project": {"id": "test-project-id", "name": "test-project"},
144+
"experiment": {"id": "test-exp-id", "name": "test-exp"},
145+
}
146+
147+
with patch.dict(os.environ, {"BRAINTRUST_DISABLE_ATEXIT_FLUSH": "True"}):
148+
with patch("atexit.register") as mock_register:
149+
_HTTPBackgroundLogger(LazyValue(api_con_response, use_mutex=False)) # type: ignore
150+
mock_register.assert_not_called()
151+
152+
with patch.dict(os.environ, {"BRAINTRUST_DISABLE_ATEXIT_FLUSH": "1"}):
153+
with patch("atexit.register") as mock_register:
154+
_HTTPBackgroundLogger(LazyValue(api_con_response, use_mutex=False)) # type: ignore
155+
mock_register.assert_not_called()
156+
157+
with patch.dict(os.environ, {"BRAINTRUST_DISABLE_ATEXIT_FLUSH": "yes"}):
158+
with patch("atexit.register") as mock_register:
159+
_HTTPBackgroundLogger(LazyValue(api_con_response, use_mutex=False)) # type: ignore
160+
mock_register.assert_not_called()
161+
127162

128163
class TestLogger(TestCase):
129164
def test_extract_attachments_no_op(self):

0 commit comments

Comments
 (0)