Skip to content

Commit 265a3a5

Browse files
committed
fix: no guard against closed file
1 parent 7fa465f commit 265a3a5

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

‎py/src/braintrust/auto.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
import logging
88
from contextlib import contextmanager
99

10-
from braintrust.integrations import ADKIntegration, AgnoIntegration, AnthropicIntegration, ClaudeAgentSDKIntegration
10+
from braintrust.integrations import (
11+
ADKIntegration,
12+
AgnoIntegration,
13+
AnthropicIntegration,
14+
ClaudeAgentSDKIntegration,
15+
LangChainIntegration,
16+
)
1117

1218

1319
__all__ = ["auto_instrument"]

‎py/src/braintrust/logger.py‎

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,11 @@ def _get_max_request_size(self) -> dict[str, Any]:
11421142
if isinstance(limit, (int, float)) and int(limit) > 0:
11431143
server_limit = int(limit)
11441144
except Exception as e:
1145-
print(f"Failed to fetch version info for payload limit: {e}", file=self.outfile)
1145+
try:
1146+
print(f"Failed to fetch version info for payload limit: {e}", file=self.outfile)
1147+
except ValueError as ve:
1148+
if "operation on closed file" not in str(ve):
1149+
raise
11461150
valid_server_limit = server_limit if server_limit is not None and server_limit > 0 else None
11471151
can_use_overflow = valid_server_limit is not None
11481152
max_request_size = DEFAULT_MAX_REQUEST_SIZE
@@ -1266,17 +1270,26 @@ def _unwrap_lazy_values(
12661270
if not is_retrying and self.sync_flush:
12671271
raise Exception(errmsg) from e
12681272
else:
1269-
print(errmsg, file=self.outfile)
1270-
traceback.print_exc(file=self.outfile)
1273+
try:
1274+
print(errmsg, file=self.outfile)
1275+
traceback.print_exc(file=self.outfile)
1276+
if is_retrying:
1277+
sleep_time_s = BACKGROUND_LOGGER_BASE_SLEEP_TIME_S * (2**i)
1278+
print(f"Sleeping for {sleep_time_s}s", file=self.outfile)
1279+
except ValueError as ve:
1280+
if "operation on closed file" not in str(ve):
1281+
raise
12711282
if is_retrying:
1272-
sleep_time_s = BACKGROUND_LOGGER_BASE_SLEEP_TIME_S * (2**i)
1273-
print(f"Sleeping for {sleep_time_s}s", file=self.outfile)
12741283
time.sleep(sleep_time_s)
12751284

1276-
print(
1277-
f"Failed to construct log records to flush after {self.num_tries} attempts. Dropping batch",
1278-
file=self.outfile,
1279-
)
1285+
try:
1286+
print(
1287+
f"Failed to construct log records to flush after {self.num_tries} attempts. Dropping batch",
1288+
file=self.outfile,
1289+
)
1290+
except ValueError as ve:
1291+
if "operation on closed file" not in str(ve):
1292+
raise
12801293
return [], []
12811294

12821295
def _request_logs3_overflow_upload(
@@ -1403,13 +1416,22 @@ def _submit_logs_request(self, items: Sequence[LogItemWithMeta], max_request_siz
14031416
if not is_retrying and self.sync_flush:
14041417
raise Exception(errmsg)
14051418
else:
1406-
print(errmsg, file=self.outfile)
1419+
try:
1420+
print(errmsg, file=self.outfile)
1421+
if is_retrying:
1422+
sleep_time_s = BACKGROUND_LOGGER_BASE_SLEEP_TIME_S * (2**i)
1423+
print(f"Sleeping for {sleep_time_s}s", file=self.outfile)
1424+
except ValueError as ve:
1425+
if "operation on closed file" not in str(ve):
1426+
raise
14071427
if is_retrying:
1408-
sleep_time_s = BACKGROUND_LOGGER_BASE_SLEEP_TIME_S * (2**i)
1409-
print(f"Sleeping for {sleep_time_s}s", file=self.outfile)
14101428
time.sleep(sleep_time_s)
14111429

1412-
print(f"log request failed after {self.num_tries} retries. Dropping batch", file=self.outfile)
1430+
try:
1431+
print(f"log request failed after {self.num_tries} retries. Dropping batch", file=self.outfile)
1432+
except ValueError as ve:
1433+
if "operation on closed file" not in str(ve):
1434+
raise
14131435

14141436
def _dump_dropped_events(self, wrapped_items):
14151437
publish_payloads_dir = [x for x in [self.all_publish_payloads_dir, self.failed_publish_payloads_dir] if x]
@@ -1455,7 +1477,11 @@ def _write_payload_to_dir(payload_dir, payload, debug_logging_adjective=None):
14551477
eprint(f"Failed to write failed payload to output file {payload_file}:\n", e)
14561478

14571479
def _log_failed_payloads_dir(self):
1458-
print(f"Logging failed payloads to {self.failed_publish_payloads_dir}", file=self.outfile)
1480+
try:
1481+
print(f"Logging failed payloads to {self.failed_publish_payloads_dir}", file=self.outfile)
1482+
except ValueError as ve:
1483+
if "operation on closed file" not in str(ve):
1484+
raise
14591485

14601486
# Should only be called by BraintrustState.
14611487
def internal_replace_api_conn(self, api_conn: HTTPConnection):

0 commit comments

Comments
 (0)