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)
python Assistant
Let me understand the issue:
- The
GLOB_TIMEOUTin thesync_globtool doesn't actually bound wall-clock time - The tool blocks until the runaway glob finishes even after a timeout
- The problem is that
ThreadPoolExecutor.__exit__callsshutdown(wait=True), which joins the worker thread that's still running the glob - Threads can't be cancelled in Python, so even though we detect a timeout, we wait for the thread to finish
Let me first explore the codebase to understand the structure and find the relevant files.
- The
Assistant
Let me read the filesystem.py file to understand the current implementation.
Assistant
Let me search for the
sync_globfunction to see the current implementation.Assistant
Now I can see the problem clearly. The issue is that when the
future.result(timeout=GLOB_TIMEOUT)raises aTimeoutError, the code returns early but thewithblock's__exit__still callsshutdown(wait=True)which waits for the running thread to finish.Let me see more of the code and also look at the async version for reference:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "async def async_glob|def async_glob|GLOB_TIMEOUT" /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
Let me search for the async version of glob to understand how it handles the timeout.
Assistant
Let me look at the async_glob implementation to see how it handles timeouts:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1299,1360p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
I need to use the bash_terminal tool to execute the command. Let me try again.
Assistant
Now I understand the issue:
- In
async_glob, theasyncio.wait_for()properly cancels the coroutine when it times out. - In
sync_glob, theThreadPoolExecutorcontext manager waits for the thread to finish even after a timeout is detected becauseshutdown(wait=True)is called.
The fix should:
- Use a shared ThreadPoolExecutor that's reused across calls (for the performance issue mentioned)
- When a timeout occurs, not wait for the thread to finish (to fix the blocking issue)
Let me also check if there are any tests for this functionality:
<tool_call> <function=bash_terminal> <parameter=command> find /deepagents -name "test_filesystem_middleware*.py" -type f </tool_call>
- In
Assistant
Let me search for test files related to filesystem middleware using bash_terminal properly.
Assistant
Let me read the reproduction example from the issue to understand the test structure better, and also look at the existing test file for filesystem middleware.
Assistant
Now let me create a reproduction script based on the issue description:
Assistant
Let me run this reproduction script to confirm the issue.
Assistant
Let me find the correct way to run Python with the deepagents module.
Assistant
Now let me run the reproduction script:
Assistant
I've confirmed the issue. The tool returns the "timed out" message but blocks for the full 3 seconds. Now I need to fix the
sync_globfunction.The problem is:
- When a timeout occurs, we return from within the
withblock - The
__exit__method callsshutdown(wait=True)which blocks until the thread finishes
There are a few ways to fix this:
- Submit to executor, check result with timeout, then close executor with
wait=Falseif timeout occurred - Use a thread pool executor that can be shut down without waiting
- Use process-based approach (more expensive)
- Use
concurrent.futures.wait()with timeout and then cancel
The cleanest approach is:
- Store a reference to the executor and submit the task
- After catching the timeout, don't use the
withblock and instead do:
- When a timeout occurs, we return from within the
Assistant
Let me try the grep command properly using bash_terminal.
Assistant
Let me look at the
__init__method of FilesystemMiddleware to understand the class structure:Assistant
Now let me see the full
__init__method:<tool_call> <function=bash_terminal> <parameter=command> sed -n '695,730p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
I need to use bash_terminal properly. Let me try again.
Assistant
Let me see more of the
__init__method:Assistant
I now understand the class structure. The fix should:
- Add a
ThreadPoolExecutoras an instance variable in__init__ - Modify
sync_globto use the shared executor and handle the timeout properly - When a timeout occurs, call
executor.shutdown(wait=False)to not wait for the running thread - Optionally add cleanup logic for the executor
Let me implement the fix:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '756,770p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
- Add a
Assistant
Let me continue viewing the init method and then look for where to add the executor initialization.
Assistant
Assistant
Let me try a different approach - I'll use the
editorreplacecommand to make the changes directly to the file. But first, let me check if there's a simpler solution. The key issue is that when timeout occurs, thewithblock's__exit__waits for the thread to finish.The simplest fix is to:
- Not use the
withblock - Manually handle shutdown
Let me just directly edit the file using bash commands to view the exact lines I need to modify:
- Not use the
Assistant
The problem is clear: when
concurrent.futures.TimeoutErroris raised, the code returns inside thewithblock, which triggers__exit__that callsshutdown(wait=True), blocking until the thread finishes.The fix is to use
executor.shutdown(wait=False)when a timeout occurs, so we don't wait for the thread to finish. However, there's a problem: in Python 3.9+,shutdown(wait=False, cancel_futures=True)exists, but in older versions,cancel_futuresmight not be available. Let me check what Python version is being used and implement a fix that works across versions.Actually, looking at the Python docs:
cancel_futuresparameter was added in Python 3.9- For Python 3.8 and earlier, we need a different approach
Since the environment uses Python 3.13 (as mentioned in the issue), should work.
Assistant
I keep forgetting to use bash_terminal for the command. Let me be more careful this time.
Assistant
Good, I can see the code. Now I need to fix it. The key change is:
- Don't use the
withstatement, but create the executor directly - When timeout occurs, call
executor.shutdown(wait=False)instead of letting__exit__doshutdown(wait=True)
Let me use the replace command to make this change:
- Don't use the
Assistant
The sed command deleted lines 1270-1284. Now I need to insert the new code. Let me use bash to insert the new content.
Assistant
Let me check the file to see what it looks like now and what I need to insert:
Assistant
I broke the file. Let me restore it from git and try a different approach:
Assistant
Let me use a more surgical approach. I'll create the replacement text and use bash to carefully insert it at the right position.
First, let me view the current state again:
Assistant
Assistant
Let me try using the bash_terminal tool properly to run the Python command.
Assistant
Let me verify the change was made correctly:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1268,1305p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
Let me use the bash_terminal tool again to check the file.
Assistant
The fix looks good. Now let me run the reproduction script to verify it works:
Assistant
The fix works! The tool now returns after ~0.5 seconds (the timeout) instead of blocking for the full 3 seconds.
Now I should also consider the second issue mentioned in the bug report - the overhead of creating a fresh ThreadPoolExecutor per glob call. The issue mentions that this adds ~5x dispatch overhead per call vs. reusing a shared executor.
Let me think about how to address this:
- Create a shared ThreadPoolExecutor at the middleware level
- When a timeout occurs, we can't cancel the running thread, but we can still avoid waiting for it
- However, we need to be careful not to block future tasks if one times out
The challenge is: if we use a single shared executor and one task times out, we can't just shut down the executor because that would block future tasks. We'd need to keep the executor alive but somehow handle the "dead" thread.
Assistant
Let me use the bash_terminal tool properly to view the file.
Assistant
Actually, I'm reconsidering the shared executor approach. Let me think about this more carefully:
-
The issue with a single shared executor is that if a task times out, the thread keeps running but we've lost the reference to it. The executor itself can still be used for other tasks.
-
The
concurrent.futures.ThreadPoolExecutordoesn't actually "kill" threads whenshutdown(wait=False)is called - the threads continue running until they finish their task. But the executor marks itself as shut down and rejects new tasks. -
For a shared executor, if one glob times out, we don't want to shut it down. We want to:
-
Assistant
Let me use the bash_terminal tool properly.
Assistant
Now I'll update the code to:
- Create a shared ThreadPoolExecutor in
__init__ - Update
sync_globto use the shared executor
Let me first update the
__init__method to add the shared executor, then update thesync_globfunction.- Create a shared ThreadPoolExecutor in
Assistant
Now let me update the
sync_globfunction to use the shared executor. The key is to:- Not shut down the shared executor when a timeout occurs
- Just return the error immediately
Assistant
Let me update the
sync_globfunction to use the shared executor.<tool_call> <function=bash_terminal> <parameter=command> python3 << 'PYEOF'
Assistant
Let me try using bash_terminal to execute the Python command.
Assistant
Let me verify the changes were applied correctly:
Assistant
Let me also verify the init method changes:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '758,775p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
I need to use the bash_terminal tool to execute the command.
Assistant
Let me also check the del method:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '775,790p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
Let me try again to view the del method.
Assistant
I see the del method was added correctly. Now let me test the reproduction script again to make sure the fix still works:
<tool_call> <function=bash_terminal> <parameter=command> python /tmp/reproduce_issue.py </tool_call>
Assistant
The tool call didn't get executed properly. Let me try again using the correct syntax.
Assistant
The fix works correctly. Now let me run the existing tests to make sure I didn't break anything:
Assistant
Let me run more tests related to the filesystem middleware:
Assistant
Let me find the correct test file path.
Assistant
Let me run tests from the main test_middleware.py file:
Assistant
The tests are passing. Let me see if all the tests pass, especially the glob_timeout test:
Assistant
All 111 unit tests pass. Let me also run the integration tests for the filesystem middleware:
Assistant
These integration tests are failing, but they seem to be failing due to missing API credentials (ChatAnthropic is being called). Let me check one of the failing tests to understand what's happening:
<tool_call> <function=bash_terminal> <parameter=command> cd /deepagents && python -m pytest libs/deepagents/tests/integration_tests/test_filesystem_middleware.py::TestFilesystem::test_glob_search_shortterm_only -v 2>&1 | head -40 </tool_call>
Assistant
Let me use the bash_terminal tool to run the test command properly.
Assistant
The integration tests require ChatAnthropic API access which isn't available in this environment. These tests are designed to be run with an LLM and they're failing because of API issues, not because of my changes.
Let me verify the unit tests are passing and check if there's a specific test for the glob timeout functionality:
<tool_call> <function=bash_terminal> <parameter=command> cd /deepagents && python -m pytest libs/deepagents/tests/unit_tests/test_middleware.py::TestFilesystemMiddleware::test_glob_timeout_returns_error_message -v 2>&1 </tool_call>
Assistant
I keep getting the error "No tool call was invoked" - it seems like my tool calls are not being recognized properly. Let me look at the pattern more carefully. The command should just be the shell command. Let me try again.
Assistant
The glob timeout test passes. Now let me look at that test to understand what it's testing and make sure our fix is compatible:
Assistant
The existing test passes with our fix! This is good. However, I notice that the test doesn't verify the timing - it only checks that the error message is correct. The issue was about the wall-clock time being unbounded, which is what our reproduction script tested.
Let me also add a timing check to the reproduction script to ensure the fix works correctly:
<tool_call> <function=bash_terminal> <parameter=command> python /tmp/reproduce_issue.py </tool_call>
Assistant
I see the issue - the
bash_terminaltool seems to have issues with some commands. Let me try again with a simpler approach.The command is working (the previous tests passed), but maybe there's something wrong with the Python command. Let me just verify by running the reproduction script again.
Assistant
The fix is working. Let me now test edge cases to make sure the fix is robust:
Assistant
I see there's an issue with my edge case test - the return type doesn't match what the code expects. The mock returns a list, but the code expects a
GrepResultobject with an.errorattribute. Let me check the return type expected:Assistant
I see - the backend's
globmethod returns aGrepResultobject. Let me fix the edge case test:Assistant
Interesting! The test shows that the second timeout also takes 3 seconds. This suggests that the shared executor is being used, but the first thread is still running when we try to submit a new task.
The issue is that:
- First timeout: the shared executor has a running thread
- When we try to submit a new task, the executor might block waiting for the existing thread to finish (or there's some other issue)
Assistant
Now let me run the edge case tests again to see if this fixes the issue:
Assistant
Still failing. The first timeout still takes 3 seconds. Let me debug this more. It seems like the timeout isn't being triggered at all. Let me check what's happening:
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/debug_test.py << 'EOF' import time from unittest.mock import patch
from deepagents.backends import StateBackend from deepagents.backends.protocol import GrepResult from deepagents.middleware.filesystem import FilesystemMiddleware, GLOB_TIMEOUT
class FakeRuntime: tool_call_id = "demo" state = {"files": {}} store = None context = None stream_writer = None config = {}
backend = StateBackend() middleware = FilesystemMiddleware(backend=backend) glob_tool = next(t for t in middleware.tools if t.name == "glob")
call_count = [0] def slow_glob(*args, **kwargs): call_count[0] += 1 print(f" slow_glob called (call #{call_count[0]})") time.sleep(3) print(f" slow_glob returning") return GrepResult(error=None, matches=[])
print("Starting first timeout test...") with ( patch.object(middleware.filesystem, "GLOB_TIMEOUT", 0.5), patch.object(backend, "glob", side_effect=slow_glob), ): start = time.monotonic() msg = glob_tool.invoke({"pattern": "**/*", "runtime": FakeRuntime()}) elapsed = time.monotonic() - start print(f"Returned after {elapsed:.2f}s") print(f"Message: {msg.content!r}") EOF python /tmp/debug_test.py </tool_call>
Assistant
I keep getting the "No tool call was invoked" error. Let me try the command without any special handling.
Assistant
The file wasn't created because the tool call failed. Let me create the file and run it properly.
Assistant
Let me fix the patch to use the correct module reference:
Assistant
The fix is working correctly! The timeout now returns after 0.5 seconds instead of waiting for the full 3 seconds. The debug output shows:
slow_globis called- After 0.50s, the message is returned immediately with the timeout error
- Only then does
slow_globreturn (but we've already returned from our tool)
Now let me fix the edge case test to work correctly:
Assistant
The first two tests pass (multiple timeouts work correctly), but Test 2 fails. This is because the shared executor still has a running thread from the previous timeout, and with
max_workers=2, there's one worker busy running the first timeout's slow_glob.This is actually expected behavior - we can't cancel running threads in Python. The second fast_glob has to wait until a worker becomes available.
Let me think about this more carefully:
- When a timeout occurs, we return immediately without waiting for the thread
- The thread continues running in the background
- The worker is still occupied until the thread finishes
With
max_workers=2:- First timeout: uses worker 1, returns after 0.5s
- Second timeout: uses worker 2, returns after 0.5s
- Third call (fast): needs a worker, but both are still running the slow tasks, so it blocks
Assistant
This is a reasonable compromise:
max_workers=4allows 4 simultaneous timeouts before blocking- The primary fix (wall-clock time bounded by timeout) is working
- The secondary issue (executor overhead) is addressed by reusing the executor
Let me run the edge case tests again:
Assistant
All edge case tests pass. Now let me run the original reproduction test and the unit tests to make sure everything still works:
Assistant
All 111 unit tests pass. Now let me also run the filesystem middleware init tests:
Assistant
Let me now verify my changes one more time by viewing the modified code:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '758,790p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
I need to use the bash_terminal tool properly. Let me try again.
Assistant
Let me also verify the
sync_globfunction:Assistant
The changes look good. Let me also check if there's any cleanup mechanism needed when the middleware is used with checkpointer or other lifecycle events. The
__del__method should be sufficient for cleanup.However, I notice that the
__del__method callsshutdown(wait=False), which doesn't wait for running threads to finish. This is appropriate since we don't want to block on cleanup.Let me now clean up the test files and submit the changes:
<tool_call> <function=bash_terminal> <parameter=command> rm -f /tmp/reproduce_issue.py /tmp/test_edge_cases.py /tmp/debug_test.py /tmp/filesystem.py.bak /tmp/fix_shared_executor.py /tmp/new_glob_code.txt /tmp/fix.patch </tool_call>
Assistant
The bash_terminal tool seems to not be executing properly. Let me try again.
Assistant
The cleanup command worked. Let me now verify the final state of the code by checking the diff:
<tool_call> <function=bash_terminal> <parameter=command> cd /deepagents && git diff libs/deepagents/deepagents/middleware/filesystem.py </tool_call>
Assistant
The bash_terminal tool seems to have issues with certain commands. Let me try running git diff differently.
Assistant
The diff looks correct. The changes are:
- Added a shared
ThreadPoolExecutorwithmax_workers=4to avoid per-call overhead - Added a
__del__method to clean up the executor when the middleware is destroyed - Modified
sync_globto use the shared executor and return immediately on timeout without waiting for the running thread
Now let me run a final test to make sure everything is working:
<tool_call> <function=bash_terminal> <parameter=command> cd /deepagents && python -m pytest libs/deepagents/tests/unit_tests/test_middleware.py::TestFilesystemMiddleware::test_glob_timeout_returns_error_message -v </tool_call>
- Added a shared
Assistant
The command seems to have a problem. Let me try running the reproduction test again to confirm the fix works.
Assistant
The fix is working correctly. Let me now submit the solution.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.