ISSUE DESCRIPTION GLOBTIMEOUT does not bound wall-clock time — tool blocks until the…
- User
ISSUE DESCRIPTION
GLOB_TIMEOUTdoes not bound wall-clock time — tool blocks until the runaway glob finishesSubmission checklist
- This is a bug, not a usage question.
- I added a clear and descriptive title.
- I searched existing issues and didn't find this.
- I can reproduce this with the latest released version.
- I included a minimal reproducible example and steps to reproduce.
Area (Required)
- deepagents (SDK)
- cli
- code
- talon
- acp
- evals
- harbor
- daytona
- modal
- quickjs
- runloop
- langsmith-sandbox
- Other / not sure / general
Related Issues / PRs
No response
Reproduction Steps / Example Code (Python)
import time from unittest.mock import patch from deepagents.backends import StateBackend from deepagents.middleware import filesystem as fs_mw from deepagents.middleware.filesystem import FilesystemMiddleware class FakeRuntime: tool_call_id = "demo" state = {"files": {}} store = None context = None stream_writer = None config = {} backend = StateBackend() real_glob = backend.glob middleware = FilesystemMiddleware(backend=backend) glob_tool = next(t for t in middleware.tools if t.name == "glob") def slow_glob(*args, **kwargs): time.sleep(3) # a glob that takes 3s (stand-in for a huge tree / slow mount) return real_glob("*.none") with ( patch.object(fs_mw, "GLOB_TIMEOUT", 0.5), # 20s in production; shortened for the repro patch.object(backend, "glob", side_effect=slow_glob), ): start = time.monotonic() msg = glob_tool.invoke({"pattern": "**/*", "runtime": FakeRuntime()}) print(f"returned after {time.monotonic() - start:.2f}s: {msg.content!r}") # Expected: returns after ~0.5s (the timeout) # Actual: returns after ~3.0s -- the full duration of the "timed out" glob Assistant
Let me analyze this issue:
- The problem is with the synchronous
globtool inFilesystemMiddleware - The
GLOB_TIMEOUTis set usingThreadPoolExecutorwithfuture.result(timeout=...) - When the timeout expires, the
withblock exits, which callsshutdown(wait=True), joining the worker thread - Since threads can't be cancelled, the tool blocks until the glob finishes
- The fix should either reuse a shared executor or use a different approach to not block on the thread join
Let me start by exploring the repository structure and finding the relevant file.
- The problem is with the synchronous
Assistant
Let me find the filesystem middleware file mentioned in the issue.
Assistant
Let me read the filesystem.py file to understand the current implementation.
Assistant
Let me search for the specific
globandsync_globfunctions:Assistant
Let me read the specific section with the glob functions:
Assistant
Now I understand the issue. Let me read more of the file to understand the full context around the grep tool and check for similar patterns:
Assistant
Now let me understand the issue better. Let me check the exact lines around the
sync_globfunction and also look for any existing tests:Assistant
Now I have a clear understanding of the issue. Let me look at the
FilesystemMiddlewareclass structure to understand where to add a shared executor:Assistant
Now I have a clear understanding of the issue. Let me first create a test script to reproduce the problem:
Assistant
Let me find the right way to run code in this environment:
Assistant
The previous command failed because the
pythoncommand wasn't finding the right packages. Let me try using the virtual environment that exists in the project.Assistant
I need to use the virtual environment to run the reproduction script. Let me try using the
.venv/bin/pythoninterpreter.Assistant
Now I understand the issue. The problem is:
- When
future.result(timeout=GLOB_TIMEOUT)raisesTimeoutError, the function returns a ToolMessage - But the
with concurrent.futures.ThreadPoolExecutor(max_workers=1)block then callsshutdown(wait=True)which joins the worker thread - Since threads can't be cancelled, this blocks until the glob finishes
The fix should:
- Not block on thread join when a timeout occurs
- Reuse a shared executor to avoid per-call overhead
Let me read more of the file to check the class structure and any similar patterns:
- When
Assistant
Now I understand the issue completely. Let me implement the fix.
The problem is that when
ThreadPoolExecutor'swithblock exits after a timeout,shutdown(wait=True)is called which blocks until the worker thread completes. Since Python threads can't be cancelled, we need to callshutdown(wait=False)on timeout.Additionally, reusing a shared executor instead of creating a new one per call will reduce overhead.
Let me implement the fix:
Assistant
Now I understand the issue. Let me implement the fix. The problem is:
- When
future.result(timeout=GLOB_TIMEOUT)raisesTimeoutError, thewithblock exits and callsshutdown(wait=True), which blocks until the worker thread finishes (can't cancel threads). - Creating a new
ThreadPoolExecutorper call adds dispatch overhead.
The fix:
- Add a shared
ThreadPoolExecutoras a class attribute - When timeout occurs, call
executor.shutdown(wait=False)to not block on the thread
- When
Assistant
Now I understand the issue. The fix requires:
- Adding a shared
ThreadPoolExecutoras an instance attribute (avoids per-call overhead) - Not using the
withstatement insync_glob(avoids blocking on thread join when timeout occurs)
Let me implement the fix:
- Adding a shared
Assistant
Let me open the file first.
Assistant
The
opencommand tried to execute the file instead of opening it in the editor. Let me try with the--fileoption.Assistant
Let me use the
editcommand with the--fileoption:Assistant
Let me add the shared executor to
__init__:Assistant
I see the edit didn't insert proper newlines. Let me fix that:
Assistant
Let me check the current state of the file and fix the newline issue:
Assistant
The edit didn't properly convert
\nto actual newlines. Let me fix that:Assistant
I need to fix the line to have proper newlines. Let me replace that line:
Assistant
The sed command might not have worked correctly with the newlines. Let me check the file to see what happened and fix it properly.
Assistant
The first part of the fix is in place. Now I need to update the
sync_globfunction to use the shared executor and not block on thread join when a timeout occurs. Let me read the current state of thesync_globfunction:Assistant
Now I need to update the
sync_globfunction to use the shared executor and properly handle the timeout case. The key changes are:- Use
self._executorinstead of creating a newThreadPoolExecutor - Not use the
withstatement (to avoid callingshutdown(wait=True)) - On timeout, call
executor.shutdown(wait=False)before returning
- Use
Assistant
Let me check the exact line numbers:
Assistant
Let me find the exact lines to modify in the sync_glob function.
Assistant
Now I need to modify the sync_glob function to use the shared executor and handle timeout properly. Let me replace the relevant lines:
Assistant
Let me find the correct range to replace. The key section to modify is:
- Line 1273:
backend_path = permission_path if path is not None else None - Lines 1274-1286: The
with ThreadPoolExecutorblock with the timeout handling
I need to replace lines 1274-1286 with new code that:
- Uses
self._executorinstead of creating a new executor - On timeout, calls
shutdown(wait=False)before returning the error message
Let me look at the exact line range more carefully.
- Line 1273:
Assistant
I need to replace the
withblock (lines 1275-1286) with code that:- Uses
self._executorinstead of creating a new ThreadPoolExecutor - Submits the task to the shared executor
- Tries to get the result with timeout
- On timeout, shuts down the executor with
wait=Falseto not block on the thread, then returns the error message
The key insight is:
- When a TimeoutError is raised, the future is still running on the shared executor
- We need to call
self._executor.shutdown(wait=False)before returning, so we don't block waiting for the thread to finish - We don't create a new executor each time, avoiding overhead
- Uses
Assistant
Now I'll replace the
withblock with code that uses the shared executor and doesn't block on timeout:Assistant
Let me use the correct format:
Assistant
The edit wasn't applied because the whitespace might not match. Let me first navigate to the line and see the exact content.
Assistant
Let me see the actual file content and use the proper indentation. I need to use the exact whitespace from the file. Let me read the content and then make the edit.
Assistant
The indentation is 12 spaces (3 levels of 4 spaces each). Let me use sed to make the replacement:
Assistant
Let me verify the changes were applied correctly:
Assistant
The changes look correct. Now let me run the reproduction script to verify the fix:
Assistant
Wait, I notice a potential issue with my fix. Let me check:
Assistant
I see the issue. Calling
shutdown(wait=False)prevents future tasks from being submitted to the executor. The fix is simply to NOT call shutdown at all - the thread will complete naturally in the background, and the executor can still be reused.Assistant
Now let me verify the fix again - the key insight is:
- When timeout occurs, we DON'T call shutdown(wait=False) because we want to reuse the shared executor
- The thread will complete naturally in the background - we can't cancel it, but we don't need to wait for it
- The executor can continue to accept new tasks
Let me run the tests again.
Assistant
The first test passes. Now let me verify that the second test also passes - the executor should be reusable.
Assistant
Excellent! The fix is working correctly:
- Timeout bounds wall-clock time: Returns after ~0.5s instead of waiting for the full 3s
- Shared executor is reusable: Second call works correctly without any errors
Now let me verify the final code state and run any existing tests:
Assistant
Let me also verify the shared executor initialization:
Assistant
I should update the comment since we're no longer calling shutdown. Let me fix that:
Assistant
Now let me run the existing tests to make sure the fix doesn't break anything:
Assistant
Let me find the correct test files in the repository.
Assistant
Let me run the existing tests related to filesystem middleware to make sure my changes don't break anything.
Assistant
The user wants me to submit my changes since I've completed the fix. Let me submit the changes now.
Assistant
Let me run the existing tests to make sure my changes don't break anything.
Assistant
The user is asking me to run the existing tests to make sure my changes don't break anything. Let me try a different approach - I'll use the system Python to run the tests instead of the virtual environment Python.
Assistant
The user wants me to run the existing tests to make sure my changes don't break anything. Let me try using the system Python to run the tests.
Assistant
Let me install the dependencies in the virtual environment first.
Assistant
The user wants me to run existing tests to make sure my changes don't break anything. Let me try a different approach - I'll check if there's a Makefile or some other way to run tests in the repository.
Assistant
Let me try running the tests using
make testwith the specific test file for filesystem tools. But first, let me check ifuvis available.Assistant
The user wants me to run existing tests to verify my changes don't break anything. Let me try using
uv runto execute the tests.Assistant
All 14 tests passed. Let me continue running more tests to ensure my changes don't break anything:
Assistant
All tests passed. Let me run a few more test files to be thorough, and then verify my fix one more time.
Assistant
Let me verify the final state of the changes and run a comprehensive test to ensure everything is working correctly:
Assistant
All tests pass. Let me verify the complete changes one more time before submitting:
Assistant
The changes look correct. Let me summarize what was fixed:
Summary of Changes:
-
Added shared executor (
self._executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)) in__init__to avoid per-call overhead of creating a new executor each time. -
Modified
sync_globto use the shared executor (self._executor.submit(...)) instead of creating a new one with awithstatement. -
Removed blocking shutdown: Previously, when
future.result(timeout=GLOB_TIMEOUT)raisedTimeoutError, the function returned a ToolMessage, but then thewithblock's__exit__calledshutdown(wait=True), which blocked until the thread finished (since threads can't be cancelled). Now, we simply return immediately after the timeout error, and the thread completes naturally in the background without blocking.
Verification:
- Original reproduction script confirmed the bug (returned after 3.01s instead of 0.5s)
- After fix: Returns after ~0.5s (as expected)
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.