Skip to content

Commit e846b58

Browse files
chore[python]: Add additional debug logging to background threads (#1689)
1 parent c1b9014 commit e846b58

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

python/langsmith/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from langsmith.utils import ContextThreadPoolExecutor
2121

2222
# Avoid calling into importlib on every call to __version__
23-
__version__ = "0.3.33"
23+
__version__ = "0.3.34"
2424
version = __version__ # for backwards compatibility
2525

2626

python/langsmith/_internal/_background_thread.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,25 @@ def keep_thread_active() -> bool:
302302
if not client or (
303303
hasattr(client, "_manual_cleanup") and client._manual_cleanup
304304
):
305+
logger.debug("Client is being cleaned up, stopping tracing thread")
305306
return False
306307
if not threading.main_thread().is_alive():
307308
# main thread is dead. should not be active
309+
logger.debug("Main thread is dead, stopping tracing thread")
308310
return False
309311

310312
if hasattr(sys, "getrefcount"):
311313
# check if client refs count indicates we're the only remaining
312314
# reference to the client
313-
return sys.getrefcount(client) > num_known_refs + len(sub_threads)
315+
should_keep_thread = sys.getrefcount(client) > num_known_refs + len(
316+
sub_threads
317+
)
318+
if not should_keep_thread:
319+
logger.debug(
320+
"Client refs count indicates we're the only remaining reference "
321+
"to the client, stopping tracing thread",
322+
)
323+
return should_keep_thread
314324
else:
315325
# in PyPy, there is no sys.getrefcount attribute
316326
# for now, keep thread alive
@@ -349,7 +359,7 @@ def keep_thread_active() -> bool:
349359
_tracing_thread_handle_batch(
350360
client, tracing_queue, next_batch, use_multipart
351361
)
352-
logger.debug("Tracing control thread is shutting down")
362+
logger.debug("Tracing control thread is shutting down")
353363

354364

355365
def tracing_control_thread_func_compress_parallel(
@@ -381,9 +391,11 @@ def keep_thread_active() -> bool:
381391
if not client or (
382392
hasattr(client, "_manual_cleanup") and client._manual_cleanup
383393
):
394+
logger.debug("Client is being cleaned up, stopping compression thread")
384395
return False
385396
if not threading.main_thread().is_alive():
386397
# main thread is dead. should not be active
398+
logger.debug("Main thread is dead, stopping compression thread")
387399
return False
388400
if hasattr(sys, "getrefcount"):
389401
# check if client refs count indicates we're the only remaining
@@ -394,8 +406,13 @@ def keep_thread_active() -> bool:
394406
active_count = sum(
395407
1 for thread in thread_pool if thread is not None and thread.is_alive()
396408
)
397-
398-
return sys.getrefcount(client) > num_known_refs + active_count
409+
should_keep_thread = sys.getrefcount(client) > num_known_refs + active_count
410+
if not should_keep_thread:
411+
logger.debug(
412+
"Client refs count indicates we're the only remaining reference "
413+
"to the client, stopping compression thread",
414+
)
415+
return should_keep_thread
399416
else:
400417
# in PyPy, there is no sys.getrefcount attribute
401418
# for now, keep thread alive
@@ -538,4 +555,4 @@ def _tracing_sub_thread_func(
538555
_tracing_thread_handle_batch(
539556
client, tracing_queue, next_batch, use_multipart
540557
)
541-
logger.debug("Tracing control sub-thread is shutting down")
558+
logger.debug("Tracing control sub-thread is shutting down")

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "langsmith"
3-
version = "0.3.33"
3+
version = "0.3.34"
44
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
55
authors = ["LangChain <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)