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 the issue:
- The bug is in
deepagents/middleware/filesystem.pyin thesync_globfunction - The problem is that when a timeout occurs,
ThreadPoolExecutor.__exit__callsshutdown(wait=True)which waits for the worker thread to finish - This means the timeout doesn't actually bound the wall-clock time - it just changes the error message
- The fix should prevent
shutdown(wait=True)from blocking
Let me first explore the repository structure and find the relevant file.
- The bug is in
Assistant
Let me find the specific file mentioned in the issue:
deepagents/middleware/filesystem.pyAssistant
I need to use the correct argument format for the open command.
Assistant
Let me try a different syntax for the open command.
Assistant
Let me search for the
sync_globfunction specifically:Assistant
Now let me see the full
sync_globfunction and surrounding code:Assistant
Now I understand the issue clearly. The problem is in the
sync_globfunction (lines 1272-1284):with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path)) try: glob_result = future.result(timeout=GLOB_TIMEOUT) except concurrent.futures.TimeoutError: return ToolMessage(...) # THIS RETURN IS INSIDE THE `with` BLOCK # CODE AFTER `with` BLOCKWhen a
TimeoutErroris caught and the function returns, Python first needs to exit thewithblock, which callsThreadPoolExecutor.__exit__→shutdown(wait=True), which joins the worker thread. So the return doesn't actually happen until the glob finishes.The fix is to either:
- Use
shutdown(wait=False)when exiting after a timeout - Or move the executor creation outside the context manager pattern
The cleanest fix is to call
executor.shutdown(wait=False)before returning the timeout error. But since we're inside awithblock, we need a different approach.Let me create a test script first to reproduce the issue:
- Use
Assistant
I need to install the dependencies first. Let me check how to set up the environment:
Assistant
I've confirmed the bug. The test shows it returns after 3.01s instead of ~0.5s. Now let me fix the issue.
The fix is to avoid the
withblock's implicitshutdown(wait=True). I need to:- Create the executor without the
withblock - On timeout, call
executor.shutdown(wait=False)before returning the error message - Clean up the executor after successful execution
Let me look at the exact lines to edit:
- Create the executor without the
Assistant
Now I'll implement the fix. The solution is to:
- Not use the
withstatement for the executor - Manually handle
shutdown(wait=False)on timeout - Manually handle
shutdown(wait=True)after successful completion
- Not use the
Assistant
Now let me apply the fix. I'll replace the
withblock pattern with explicit executor handling:Assistant
The edit command is failing because I don't have a file open. Let me open the file first, then use the edit command.
Assistant
The open command doesn't seem to accept a
fileparameter. Let me try using it without arguments or with just the path as a positional argument.Assistant
Let me use sed to make the replacement instead:
Assistant
Now let me verify the change was applied correctly:
Assistant
The fix looks correct. Now let me run the test to verify it works:
Assistant
Hmm, it's still taking 3 seconds. Let me check if the module is being cached. The issue is that Python caches imported modules. The test imports the module before I made the fix, so I need to make sure I'm using the updated code. Let me check by re-running in a fresh Python process:
Assistant
Still failing. Let me check if maybe the issue is that the
glob_tool.invoke()is somehow using the cached version of the function. Let me inspect the actual function:Assistant
The code is updated correctly. Let me think about why it's still not working...
Wait! I see the issue now. The
finallyblock always callsexecutor.shutdown(wait=True). So even when we callexecutor.shutdown(wait=False)in the timeout case, thefinallyblock will still executeexecutor.shutdown(wait=True)afterwards, which will block!The
shutdown(wait=False)call doesn't prevent thefinallyblock from running. I need to ensure thatshutdown(wait=True)in thefinallyblock is only called when we didn't already callshutdown(wait=False).Let me revise the fix:
Assistant
I see - when the timeout happens,
executor.shutdown(wait=False)is called first, but then thefinallyblock runs and callsexecutor.shutdown(wait=True). Looking at Python'sThreadPoolExecutorsource code, callingshutdown(wait=True)aftershutdown(wait=False)should be a no-op since shutdown is idempotent. Let me verify:Assistant
Interesting! The
shutdown(wait=False)sets_shutdown = True, and then callingshutdown(wait=True)afterwards doesn't actually wait because the executor is already shutdown. So callingshutdown(wait=True)twice should be fine.But wait - the test is still failing! Let me investigate more:
Assistant
Now I see the issue! Calling
shutdown(wait=True)aftershutdown(wait=False)still waits. The_shutdownflag doesn't preventwait=Truefrom blocking - it only prevents accepting new tasks.Let me check the Python source for
ThreadPoolExecutor.shutdown:Assistant
Now I understand. When
wait=True, it iterates over all threads and callsjoin()on each one, regardless of whether shutdown was already called. So callingshutdown(wait=True)aftershutdown(wait=False)still waits for threads to finish.The fix needs to not call
shutdown(wait=True)when we've already calledshutdown(wait=False). I'll use a flag to track this:Assistant
Let me verify the change:
Assistant
Now let me test the fix:
Assistant
The fix works. Now the glob timeout correctly bounds wall-clock time (0.50s instead of 3.00s).
Let me also verify the normal (non-timeout) case still works:
Assistant
This error is expected - the StateBackend needs to be used within a graph context. The important thing is that the timeout test works. Let me also write a simpler test for the normal path to make sure the fix doesn't break non-timeout scenarios:
Assistant
The normal case still works correctly. Now let me run the existing tests for this module to make sure I haven't broken anything:
Assistant
Let me look for specific tests related to the glob tool or filesystem middleware:
Assistant
There are existing tests for glob timeout. Let me run them:
Assistant
I'll run the existing tests to make sure my fix doesn't break anything. </tool_call>
Assistant
Let me install pytest first or find another way to run tests.
Assistant
Let me check if there's a way to run tests using the venv's pytest or install it in the venv.
Assistant
The tests need to be run with the venv's pytest. Let me try running them now.
Assistant
The venv doesn't have pytest installed. Let me install it properly in the venv.
Assistant
Let me try using the system pytest but pointing it to the venv's Python modules.
Assistant
The issue is that the venv's Python doesn't have pytest. Let me try using the system Python but with the venv's site-packages in the path.
Assistant
Let me try running pytest directly from the system Python with the correct PYTHONPATH.
Assistant
The problem is that python3 is aliasing to the venv python. Let me check which Python I'm using:
Assistant
Ah, my shell has the venv activated. Let me use the system python3 directly.
Assistant
The sync glob timeout test passes. Let me also run the async version to make sure I didn't break anything:
Assistant
Both glob timeout tests pass. Let me run all the middleware tests to ensure nothing else is broken:
Assistant
All 139 middleware tests pass. Let me also run any other tests that might be related to glob:
Assistant
Let me run glob-related tests while ignoring the benchmarks directory:
Assistant
All glob-related tests pass (88 passed). Let me verify my fix once more with the reproduction script from the issue:
Assistant
Let me now verify the fix one more time and review my changes before submitting:
Assistant
Let me verify my fix handles all the checklist items properly:
Checklist verification:
-
If an error was reported in the issue description, I have successfully reproduced it. ✅
- Ran the reproduction script from the issue and confirmed the bug:
returned after 3.01sinstead of~0.5s
- Ran the reproduction script from the issue and confirmed the bug:
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error. ✅
- After applying the fix:
returned after 0.50sas expected
- After applying the fix:
-
I have thought about edge cases and made sure my fix handles them as well. ✅
- Normal (non-timeout) case: Tested with a fast task - works correctly with
shutdown(wait=True)waiting for completion - : Returns immediately after timeout with
- Normal (non-timeout) case: Tested with a fast task - works correctly with
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.