Skip to content

Commit

Permalink
fix: improve end_traces so it doesn't block the build loop (#3482)
Browse files Browse the repository at this point in the history
* refactor: Simplify log configuration logic in Graph class

* feat(tracing): Refactor _end_traces method to be async and use asyncio.to_thread.

This is an attempt to avoid blocking the build loop
  • Loading branch information
ogabrielluiz authored Aug 22, 2024
1 parent dde0fa4 commit 40237d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/backend/base/langflow/graph/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ def __init__(
edges (List[Dict[str, str]]): A list of dictionaries representing the edges of the graph.
flow_id (Optional[str], optional): The ID of the flow. Defaults to None.
"""
if not log_config:
log_config = {"disable": False}
configure(**log_config)
if log_config:
configure(**log_config)
self._start = start
self._state_model = None
self._end = end
Expand Down
12 changes: 8 additions & 4 deletions src/backend/base/langflow/services/tracing/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from collections import defaultdict
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING, Any, Dict, Optional, List
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from uuid import UUID

from loguru import logger
Expand All @@ -12,11 +12,12 @@
from langflow.services.tracing.schema import Log

if TYPE_CHECKING:
from langchain.callbacks.base import BaseCallbackHandler

from langflow.custom.custom_component.component import Component
from langflow.graph.vertex.base import Vertex
from langflow.services.monitor.service import MonitorService
from langflow.services.settings.service import SettingsService
from langchain.callbacks.base import BaseCallbackHandler


def _get_langsmith_tracer():
Expand Down Expand Up @@ -210,8 +211,11 @@ async def trace_context(
self._end_traces(trace_id, trace_name, e)
raise e
finally:
self._end_traces(trace_id, trace_name, None)
self._reset_io()
asyncio.create_task(asyncio.to_thread(self._end_and_reset, trace_id, trace_name, None))

async def _end_and_reset(self, trace_id: str, trace_name: str, error: Exception | None = None):
self._end_traces(trace_id, trace_name, error)
self._reset_io()

def set_outputs(
self,
Expand Down

0 comments on commit 40237d0

Please sign in to comment.