Fix/runtime context thread id bug#934
Open
SARTHAK2511 wants to merge 2 commits intobytedance:mainfrom
Open
Conversation
Fixed fundamental bug where code incorrectly assumed runtime.context would contain thread_id. In LangGraph, runtime.context is None by default and thread_id is actually in the RunnableConfig. Changes: - Replace runtime.context with get_config() for accessing thread_id - Updated 6 middleware/tool files to use proper LangGraph config access pattern - Added resource cleanup in sandbox provider to prevent memory leaks - Added thread pool cleanup and TTL-based task cleanup in subagent executor This bug existed from the initial implementation and prevented the agent from processing any messages. The fix enables proper agent execution.
|
|
Contributor
|
Hello, I am currently exploring the detailed implementation of deer-flow. To my knowledge, the frontend has explicitly set the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Gemini said
Markdown
Bug Fix Report: AttributeError in DeerFlow Agent
## Problem
The DeerFlow agent was unable to process any messages due to an
AttributeError: 'NoneType' object has no attribute 'get'. This occurred because the code incorrectly assumedruntime.contextwould contain a dictionary withthread_id, but in LangGraph,runtime.contextisNoneby default.Root Cause
The original implementation incorrectly accessed
thread_idusing:In LangGraph's architecture:
Runtime.contextdefaults toNoneunless explicitly passed during graph invocation.thread_idis actually stored in theRunnableConfigunderconfig["configurable"]["thread_id"].Solution
Replace all instances of
runtime.context["thread_id"]access with the proper LangGraph pattern:Changes Made
Core Fixes (6 files)
backend/src/agents/middlewares/thread_data_middleware.py - Fixed thread directory initialization.
backend/src/agents/middlewares/memory_middleware.py - Fixed memory system thread access.
backend/src/agents/middlewares/uploads_middleware.py - Fixed file upload handling.
backend/src/sandbox/middleware.py - Fixed sandbox acquisition.
backend/src/sandbox/tools.py - Fixed sandbox tools thread access.
backend/src/tools/builtins/task_tool.py - Fixed subagent task delegation.
Additional Improvements (2 files)
backend/src/community/aio_sandbox/aio_sandbox_provider.py - Added cleanup for orphaned thread locks to prevent memory leaks.
backend/src/subagents/executor.py - Added thread pool cleanup with atexit handler and TTL-based task cleanup.
Testing & Impact
Testing Results
✅ Successfully built and ran with Docker Compose.
✅ Agent processes messages correctly (tested with simple math queries).
✅ Resolved AttributeError in middleware execution.
✅ Thread creation and sandbox acquisition are now working properly.
Impact
This was a critical bug that prevented the agent from functioning. This fix enables:
Proper message processing.
Thread-specific data directory creation.
Sandbox environment acquisition.
Full Memory system functionality.
Reliable file upload handling and subagent delegation.
Compatibility & Notes
Version: Works with LangGraph >= 1.0.6 (as specified in pyproject.toml).
Backward Compatibility: Maintains support for existing threads; no breaking changes to external APIs.
Note: This bug appeared to be a misunderstanding of the LangGraph Runtime API. This fix aligns the codebase with documented patterns for accessing configuration within middleware and tools.