Skip to content

Fix/runtime context thread id bug#934

Open
SARTHAK2511 wants to merge 2 commits intobytedance:mainfrom
SARTHAK2511:fix/runtime-context-thread-id-bug
Open

Fix/runtime context thread id bug#934
SARTHAK2511 wants to merge 2 commits intobytedance:mainfrom
SARTHAK2511:fix/runtime-context-thread-id-bug

Conversation

@SARTHAK2511
Copy link

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 assumed runtime.context would contain a dictionary with thread_id, but in LangGraph, runtime.context is None by default.


Root Cause

The original implementation incorrectly accessed thread_id using:

thread_id = runtime.context["thread_id"] # runtime.context is None

In LangGraph's architecture:

  • Runtime.context defaults to None unless explicitly passed during graph invocation.
  • The LangGraph API server does not pass any context when invoking graphs.
  • The thread_id is actually stored in the RunnableConfig under config["configurable"]["thread_id"].

Solution

Replace all instances of runtime.context["thread_id"] access with the proper LangGraph pattern:

from langgraph.config import get_config

config = get_config()
thread_id = config.get("configurable", {}).get("thread_id")

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.

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.
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@zhiyunyao
Copy link
Contributor

Hello, I am currently exploring the detailed implementation of deer-flow. To my knowledge, the frontend has explicitly set the thread_id field within runtime.context. Refer to hooks.ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants