-
Notifications
You must be signed in to change notification settings - Fork 332
fix(resource-manager): prevent stale clients from queueing after shutdown #1826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import os | ||
| import threading | ||
| import time | ||
| from queue import Empty, Full, Queue | ||
| from typing import Any, Callable, Optional, TypeVar, cast | ||
|
|
@@ -45,6 +46,8 @@ def __init__( | |
| self._enabled = os.environ.get( | ||
| LANGFUSE_MEDIA_UPLOAD_ENABLED, "True" | ||
| ).lower() not in ("false", "0") | ||
| self._shutdown = False | ||
| self._state_lock = threading.Lock() | ||
|
|
||
| def reinitialize( | ||
| self, | ||
|
|
@@ -53,9 +56,15 @@ def reinitialize( | |
| httpx_client: httpx.Client, | ||
| media_upload_queue: Queue, | ||
| ) -> None: | ||
| self._api_client = api_client | ||
| self._httpx_client = httpx_client | ||
| self._queue = media_upload_queue | ||
| with self._state_lock: | ||
| self._api_client = api_client | ||
| self._httpx_client = httpx_client | ||
| self._queue = media_upload_queue | ||
| self._shutdown = False | ||
|
|
||
| def begin_shutdown(self) -> None: | ||
| with self._state_lock: | ||
| self._shutdown = True | ||
|
|
||
| def process_next_media_upload(self) -> None: | ||
| try: | ||
|
|
@@ -98,6 +107,12 @@ def _find_and_process_media( | |
| ) -> Any: | ||
| if not self._enabled: | ||
| return data | ||
| with self._state_lock: | ||
| if self._shutdown: | ||
| logger.warning( | ||
| "Media: Skipping upload because the Langfuse client has already been shut down." | ||
| ) | ||
| return data | ||
|
|
||
| seen = set() | ||
| max_levels = 10 | ||
|
|
@@ -279,10 +294,16 @@ def _process_media( | |
| field=field, | ||
| ) | ||
|
|
||
| self._queue.put( | ||
| item=upload_media_job, | ||
| block=False, | ||
| ) | ||
| with self._state_lock: | ||
| if self._shutdown: | ||
| logger.warning( | ||
| f"Media: Skipping upload for media_id={media._media_id} because the Langfuse client has already been shut down." | ||
| ) | ||
| return | ||
|
Comment on lines
+297
to
+302
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an application thread is traversing a multi-media payload as another thread reaches Knowledge Base Used: Prompt To Fix With AIThis is a comment left during a code review.
Path: langfuse/_task_manager/media_manager.py
Line: 297-302
Comment:
**Shutdown creates dangling media references**
When an application thread is traversing a multi-media payload as another thread reaches `begin_shutdown()`, later `_process_media` calls return without queueing uploads while their callers still replace the payload values with media references, causing those referenced media objects never to be uploaded.
**Knowledge Base Used:**
- [Client Core](https://app.greptile.com/personal-org-4986/-/custom-context/knowledge-base/langfuse/langfuse-python/-/docs/client-core.md)
- [Task Manager: Media Upload and Score Ingestion](https://app.greptile.com/personal-org-4986/-/custom-context/knowledge-base/langfuse/langfuse-python/-/docs/task-manager.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| self._queue.put( | ||
| item=upload_media_job, | ||
| block=False, | ||
| ) | ||
| logger.debug( | ||
| f"Queue: Enqueued media ID {media._media_id} for upload processing | trace_id={trace_id} | field={field}" | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user-supplied span-export callback synchronously calls
shutdown()during the initial shutdown'sforce_flush(), this branch waits on_shutdown_eventfrom the same thread responsible for eventually setting it, causing shutdown to block indefinitely.Knowledge Base Used: Client Core
Prompt To Fix With AI