Now I need to implement the fix. Let me create the replacement text for the sync_glob function. I need to:
- Add a module-level shared executor for glob operations
- Fix the sync_glob function to not block after timeout
Let me think about the best approach:
Option 1: Use a shared module-level executor with shutdown(wait=False) on timeout.
- Pro: Simple, reuses threads
- Con: Need to handle executor lifecycle
Option 2: Use a try/finally pattern without the with statement.
- Pro: Simple fix
- Con: Still creates new executor per call
I think the best approach is:
- Add a shared module-level ThreadPoolExecutor for glob operations
- Use it in sync_glob without the
with statement
- On timeout, call
executor.shutdown(wait=False) to immediately release threads
Actually, looking more carefully, I should be careful about reusing an executor that has threads with pending work. Let me think...
Actually, the simplest and safest fix is:
- Remove the
with statement
- On timeout, call
executor.shutdown(wait=False) before returning
This ensures the thread is not joined when we return the timeout error.
For the shared executor benefit, I can add a module-level executor and reuse it, but I need to be careful about thread safety and pending work.
Let me go with the simpler fix first - just removing the blocking shutdown on timeout. Then I can add a shared executor if needed.
Actually, re-reading the issue more carefully:
- "The synchronous
glob tool created by FilesystemMiddleware enforces GLOB_TIMEOUT (20s) by running the backend glob on a worker thread: future.result(timeout=GLOB_TIMEOUT) inside a with concurrent.futures.ThreadPoolExecutor(max_workers=1) block"
- "The
return inside the with block triggers ThreadPoolExecutor.__exit__ -> shutdown(wait=True), which joins the worker thread still running the glob"
So the fix is simple: don't use with and call shutdown(wait=False) on timeout.
Let me also consider adding a shared executor for the secondary issue. But that's more complex and requires careful lifecycle management. Let me focus on the primary issue first.
Actually, looking at the code more carefully, I think the best approach is:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
try:
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:
executor.shutdown(wait=False)
return ToolMessage(...)
finally:
executor.shutdown(wait=False)
finally:
executor.shutdown(wait=True)
Wait, that's getting complex. Let me think about this more carefully.
Actually, the cleanest approach is:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
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:
executor.shutdown(wait=False) # Don't wait for the thread to finish
return ToolMessage(...)
finally:
executor.shutdown(wait=False) # Always release resources immediately
Wait, but we need to handle both success and error paths. Let me think again...
Actually, executor.shutdown(wait=False) is safe to call even if the future is already done. So we can do:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
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:
executor.shutdown(wait=False)
return ToolMessage(...)
# Success path - executor is still running but future is done
executor.shutdown(wait=False)
Actually, executor.shutdown(wait=False) is fine - it just doesn't wait for pending futures to complete. If the future is already done, it's fine. If it's still running, it will continue running in the background but we don't block.
But wait, after we call shutdown(wait=False), the executor can't accept new tasks. So we need to create a new executor for each call OR use a shared executor and just not call shutdown at all during the tool call.
For the shared executor approach:
# Module-level shared executor
_glob_executor = concurrent.futures.ThreadPoolExecutor(max_workers=4)
def sync_glob(...):
future = _glob_executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
# The thread is still running, but we don't wait for it
# Don't call shutdown - the shared executor continues
return ToolMessage(...)
# Success path
# The executor continues for next calls
return ToolMessage(...)
But the issue with this approach is that if a thread is stuck running a glob, it holds up a slot in the executor. With max_workers=4, we can have 3 other concurrent globs.
Actually, let me re-read the issue description more carefully:
"Secondary issue with the same code: constructing a fresh ThreadPoolExecutor (and spawning a thread) per glob call adds ~5x dispatch overhead per call vs. reusing a shared executor."
So the secondary issue is just about performance - creating a new executor per call is slow. The primary issue is about the blocking shutdown.
For now, let me fix the primary issue first (blocking shutdown) and then consider the secondary issue.
The simplest fix for the primary issue is:
backend_path = permission_path if path is not None else None
ctx = contextvars.copy_context()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
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:
executor.shutdown(wait=False) # Don't wait for the thread
return ToolMessage(...)
executor.shutdown(wait=False) # Release resources without blocking
This is clean and fixes the primary issue. For the secondary issue (shared executor), I can add a module-level executor.
Actually, thinking about this more, I realize that with a shared executor, if a glob times out, the thread is still running in the background. But that's OK - the thread will eventually complete and the executor will be idle. The key is that we don't wait for it.
Let me implement both fixes:
- Add a module-level shared executor
- Use it in sync_glob without blocking shutdown
Actually, wait. If I use a shared executor with max_workers=N, and N globs are running concurrently (some of them timing out), the executor might run out of threads. That's actually fine - the other globs will just wait for a thread to become available.
Let me implement this. I'll use a shared executor with enough workers to handle concurrent globs, say max_workers=4 or even just keep it at 1 but create it once.
Actually, looking at the original code, it uses max_workers=1. With a shared executor, this would mean globs are sequential, which is fine for correctness. But for concurrency, we might want more workers.
Let me keep it simple and just use max_workers=1 for the shared executor. This matches the original behavior and fixes both issues.
Actually, thinking about this more, I think the shared executor approach might cause issues. If one glob is running and another comes in, it will wait for the first one to finish (since max_workers=1). That might not be the desired behavior.
Let me just fix the primary issue first (blocking shutdown) and leave the shared executor for a separate improvement.
Actually, let me re-read the issue description once more:
"The synchronous glob tool created by FilesystemMiddleware enforces GLOB_TIMEOUT (20s) by running the backend glob on a worker thread: future.result(timeout=GLOB_TIMEOUT) inside a with concurrent.futures.ThreadPoolExecutor(max_workers=1) block (deepagents/middleware/filesystem.py, sync_glob).
I expect a glob that exceeds the timeout to make the tool return the "timed out" error after ~GLOB_TIMEOUT seconds.
Instead, the return inside the with block triggers ThreadPoolExecutor.__exit__ -> shutdown(wait=True), which joins the worker thread still running the glob (threads can't be cancelled). The tool builds the "timed out after 20s" message but then blocks until the glob actually finishes — however long that takes. The timeout changes the message the model sees, not the latency, so a glob over a huge tree or slow network mount hangs the agent loop unboundedly.
Secondary issue with the same code: constructing a fresh ThreadPoolExecutor (and spawning a thread) per glob call adds ~5x dispatch overhead per call vs. reusing a shared executor."
So the primary issue is about unbounded blocking. The secondary issue is about dispatch overhead. Let me fix both.
Here's my plan:
- Add a module-level shared
ThreadPoolExecutor for glob operations
- Fix the
sync_glob function to not block after timeout by calling shutdown(wait=False)
Wait, but with a shared executor, we can't call shutdown because other callers might be using it. So the shared executor approach is:
_glob_executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
def sync_glob(...):
future = _glob_executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
# Don't call shutdown - the shared executor continues
return ToolMessage(...)
# Success path - also don't call shutdown
return ToolMessage(...)
But this means the executor never shuts down, which is fine for the lifetime of the process.
Actually, I think a better approach for the shared executor is to not call shutdown at all, since the executor is shared and we can't shut it down per-call.
But then we lose the thread cleanup on process exit... Actually, Python will clean up threads on process exit, so this is fine.
Wait, there's another issue. If we use a shared executor with max_workers=1, and a timeout happens, the thread is still running in the background. When the next glob call comes in, it will wait for the shared executor's thread to become available. But the thread is still running the old glob! So the next call will wait up to GLOB_TIMEOUT for the old call to finish, then run the new call.
Hmm, that's not great. Let me think about this differently.
Actually, looking at the executor behavior:
executor.submit() submits a task to the executor
- The executor has N worker threads
- If all threads are busy, submit() returns immediately but the task waits in a queue
- When a thread becomes free, it picks up the next task from the queue
So with max_workers=1:
- Call 1 submits, runs on the single thread
- Call 2 submits, waits in the queue until Call 1's thread is free
- If Call 1 times out, its thread is still running (in the background)
- Call 2's task will never run because the thread is busy with Call 1
That's a problem! With a shared executor and max_workers=1, if one glob times out, all subsequent globs will be blocked.
Let me reconsider. I think the best approach is:
- Use
max_workers=1 per call (to avoid race conditions in the backend glob)
- Create a new executor per call (as in the original code)
- Fix the blocking shutdown issue by calling
executor.shutdown(wait=False) on timeout
This fixes the primary issue without introducing the secondary issue of blocking subsequent calls.
Actually, wait. Let me re-read the original code:
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(...)
The max_workers=1 is per-call, so each call creates its own executor with one thread. This is fine.
The issue is the shutdown(wait=True) on exit. If we don't use the with statement and call shutdown(wait=False) instead, we avoid the blocking.
So the fix is simple:
backend_path = permission_path if path is not None else None
ctx = contextvars.copy_context()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
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:
executor.shutdown(wait=False)
return ToolMessage(...)
executor.shutdown(wait=False)
Wait, but executor.shutdown(wait=False) in the success path is also not ideal because we're not waiting for the thread to finish. But since the future is already done (we got the result), the thread has already finished. So calling shutdown(wait=False) is fine - it won't block.
Actually, looking at the Python docs for concurrent.futures.Executor.shutdown:
If wait is True, this method will not return until all the pending futures are done executing.
Since we got the result in the success path, the future is done. So shutdown(wait=False) won't block.
But wait, I also need to handle the case where future.result() raises an exception other than TimeoutError. Let me add a try/finally:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
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:
executor.shutdown(wait=False)
return ToolMessage(...)
except Exception:
executor.shutdown(wait=False)
raise
executor.shutdown(wait=False)
Actually, let me simplify. Since the executor has only one thread and we're waiting for the result (or timeout), after the try/except block, the thread is either done or we've already returned. So:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
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:
executor.shutdown(wait=False)
return ToolMessage(...)
executor.shutdown(wait=False)
Wait, I'm not handling the case where future.result() raises an exception (not TimeoutError). Let me add that:
Actually, looking at the original code more carefully:
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(...)
if glob_result.error:
...
So if future.result() raises an exception other than TimeoutError, it propagates out. The with block catches that and shuts down. Let me keep that behavior:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
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:
executor.shutdown(wait=False)
return ToolMessage(...)
executor.shutdown(wait=False)
Actually, if future.result() raises an exception, it will propagate out and executor.shutdown(wait=False) won't be called. The executor's thread will just be cleaned up when the garbage collector runs. That's not ideal but it's acceptable.
Actually, let me add a try/finally to ensure cleanup:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
executor.shutdown(wait=False)
return ToolMessage(...)
executor.shutdown(wait=False)
except Exception:
executor.shutdown(wait=False)
raise
Hmm, that's getting complex. Let me simplify:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
executor.shutdown(wait=False)
return ToolMessage(...)
finally:
executor.shutdown(wait=False)
Wait, but if we're in the timeout path, we return and the finally block runs executor.shutdown(wait=False) after we've already called it. That's fine - calling shutdown twice is a no-op.
Actually, looking at the Python docs:
This method should be called before the garbage collects a shutdown Executor.
And from the source code, shutdown(wait=False) is idempotent - calling it multiple times is fine.
OK, let me go with this approach. Here's the fix:
backend_path = permission_path if path is not None else None
ctx = contextvars.copy_context()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
return ToolMessage(
content=f"Error: glob timed out after {GLOB_TIMEOUT}s. Try a more specific pattern or a narrower path.",
name="glob",
tool_call_id=runtime.tool_call_id,
status="error",
)
finally:
executor.shutdown(wait=False)
Wait, but if we call return inside the try block, the finally block still runs. So this is correct - the return happens, but first the finally block executes executor.shutdown(wait=False), which doesn't block.
Let me verify: in the timeout path:
TimeoutError is raised
return ToolMessage(...) is called
- The
finally block runs: executor.shutdown(wait=False) - doesn't block
- Function returns
In the success path:
glob_result is obtained
- The
finally block runs: executor.shutdown(wait=False) - doesn't block (future is done)
- Code continues to process the result
This looks correct!
Now, for the shared executor secondary issue, let me think about whether to implement it.
Looking at the issue description: "Secondary issue with the same code: constructing a fresh ThreadPoolExecutor (and spawning a thread) per glob call adds ~5x dispatch overhead per call vs. reusing a shared executor."
This is a performance issue, not a correctness issue. Let me focus on fixing the primary issue first and then consider the secondary issue.
For the shared executor, I'll add a module-level executor and use it in sync_glob. Since we're not calling shutdown on the shared executor, we need to be careful about the executor's lifecycle.
Actually, for a shared executor, we can't call shutdown during the tool call because other callers might be using it. So the shared executor approach is simpler:
# Module-level shared executor for glob operations
_glob_executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
def sync_glob(...):
...
future = _glob_executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
# Don't call shutdown - the shared executor continues
return ToolMessage(...)
# Success path - also don't call shutdown
...
But wait, this has the problem I mentioned earlier: with max_workers=1, if one glob times out, the thread is still running in the background, and the next call will wait for it.
Let me increase max_workers to handle this. If a glob times out, we lose that thread slot, but with enough slots, we can handle concurrent timeouts.
Actually, let me think about this differently. The max_workers=1 was originally intended to avoid race conditions in the backend. But with a shared executor, we need more workers to handle concurrent globs.
Let me just fix the primary issue first (blocking shutdown) without adding a shared executor. The shared executor can be added in a separate PR if needed.
Actually, wait. Let me re-read the issue one more time...
The issue says:
"Secondary issue with the same code: constructing a fresh ThreadPoolExecutor (and spawning a thread) per glob call adds ~5x dispatch overhead per call vs. reusing a shared executor."
This is mentioned as a secondary issue. The primary issue is the blocking shutdown. Let me fix the primary issue first.
Actually, looking at the issue again, I think the fix should address both:
- Primary: Remove the blocking shutdown on timeout
- Secondary: Use a shared executor to reduce dispatch overhead
For the shared executor, I think the best approach is to use a larger max_workers to handle concurrent globs and timeouts. Something like max_workers=4 or even max_workers=10 would work.
But actually, I'm concerned about thread safety. If the backend's glob method is not thread-safe, having multiple workers might cause issues. The original code used max_workers=1 to avoid this.
Let me just fix the primary issue for now and leave the shared executor as a separate improvement.
Wait, actually, I think I can do both safely. Here's the approach:
- Add a module-level shared executor with
max_workers=4 (or similar)
- In sync_glob, use the shared executor and don't call shutdown on it
The max_workers=4 allows up to 4 concurrent globs. If one times out, the other 3 can still run. And since we don't call shutdown, the shared executor continues to serve new requests.
But wait, if a glob times out, its thread is still running in the background. So we've "wasted" a thread slot. If 4 globs all timeout, all 4 threads are still running, and new globs will have to wait.
That's not ideal. Let me think about this differently.
Actually, I think the best approach is to use a context manager that doesn't block on exit:
class NonBlockingExecutor:
def __init__(self, max_workers=1):
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=max_workers)
def __enter__(self):
return self.executor
def __exit__(self, *args):
self.executor.shutdown(wait=False)
def sync_glob(...):
...
with NonBlockingExecutor(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 way, the __exit__ doesn't block, but we still get the convenience of the with statement.
But wait, if the timeout path returns, the __exit__ still runs (since Python runs finally blocks for with statements even when returning). And __exit__ calls shutdown(wait=False), which doesn't block. So this is correct.
Hmm, actually, I realize the issue. When a return happens inside a with block, Python does execute the __exit__ method before actually returning. So the __exit__ method in the original code (ThreadPoolExecutor.__exit__) is called even when we return after a timeout.
So my fix using a context manager that calls shutdown(wait=False) in __exit__ is correct.
Let me implement this.
Actually, you know what, let me just do the simplest fix possible. Remove the with statement and call executor.shutdown(wait=False) after the timeout and in the success path.
Here's the fix:
backend_path = permission_path if path is not None else None
ctx = contextvars.copy_context()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
executor.shutdown(wait=False)
return ToolMessage(
content=f"Error: glob timed out after {GLOB_TIMEOUT}s. Try a more specific pattern or a narrower path.",
name="glob",
tool_call_id=runtime.tool_call_id,
status="error",
)
executor.shutdown(wait=False)
except Exception:
executor.shutdown(wait=False)
raise
Wait, but if future.result() raises an exception (not TimeoutError), it will propagate out. The executor.shutdown(wait=False) in the inner try/except won't run because we're catching only TimeoutError. So I need a try/finally:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
executor.shutdown(wait=False)
return ToolMessage(...)
except Exception:
executor.shutdown(wait=False)
raise
executor.shutdown(wait=False)
Wait, this is getting complex. Let me simplify:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
return ToolMessage(...)
finally:
executor.shutdown(wait=False)
In the timeout path:
TimeoutError is caught
return ToolMessage(...) is called
finally block runs: executor.shutdown(wait=False) - doesn't block
- Function returns
In the success path:
glob_result is obtained
finally block runs: executor.shutdown(wait=False) - doesn't block (future is done)
- Code continues to process the result
In the exception path (other exceptions):
- Exception is raised
finally block runs: executor.shutdown(wait=False) - doesn't block
- Exception propagates
This is clean and simple. Let me go with this approach.
OK, I've decided on the approach. Now let me implement it:
The key change is replacing:
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(...)
with:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
future = executor.submit(lambda: ctx.run(resolved_backend.glob, pattern, path=backend_path))
try:
try:
glob_result = future.result(timeout=GLOB_TIMEOUT)
except concurrent.futures.TimeoutError:
return ToolMessage(...)
finally:
executor.shutdown(wait=False)
And for the shared executor, I'll add a module-level executor.
Actually wait, I want to re-think the shared executor approach. The issue says creating a new executor per call adds ~5x dispatch overhead. That's a significant performance issue.
Let me think about how to implement a shared executor safely:
- Use a module-level
ThreadPoolExecutor with enough workers
- Don't call shutdown on it during the tool call
- The executor is shut down when the process exits (Python's GC)
The concern is that with a shared executor, if one glob times out, we "waste" a thread. But with enough workers (e.g., max_workers=4), we can handle concurrent timeouts.
Actually, I just realized something. With the original code using max_workers=1, each glob call creates its own executor with one thread. If a glob times out, the thread is joined (blocking), so the caller is blocked until the glob finishes. But the executor is then destroyed.
With a shared executor and max_workers=1, if a glob times out, the thread is still running in the background. The next glob call will wait for this thread to finish before running. That means the next glob call will be delayed by the duration of the timed-out glob.
This is worse than the original behavior! With the original code, each glob call is independent. With a shared executor and max_workers=1, timed-out globs block subsequent globs.
So for the shared executor to work well, we need max_workers > 1. But then we might have thread safety issues if the backend's glob method is not thread-safe.
Let me check if there are any comments in the code about thread safety:
Looking at the code, there's contextvars.copy_context() which is used to copy the context for the worker thread. This suggests the code is aware of context propagation and is trying to be safe.
But I don't see any explicit thread-safety guarantees for the backend's glob method. Let me just assume it's not thread-safe and use max_workers=1 per call.
OK, I've decided: just fix the primary issue (blocking shutdown) without adding a shared executor. The shared executor can be added in a separate PR.
Actually wait, let me reconsider once more. The issue description mentions the shared executor as a "secondary issue", but it's still an issue that should be fixed.
Let me look at the actual code pattern used. The code uses:
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
...
This creates a new executor per call. The max_workers=1 is probably to avoid race conditions in the backend.
For the shared executor, I think the safest approach is:
- Use a module-level executor with
max_workers=4 (or similar)
- This allows concurrent globs
- If one times out, the other 3 can still run
- The shared executor is never shut down (it's a module-level singleton)
But wait, I need to be careful. If I use a shared executor and don't shut it down, the threads will be cleaned up by Python's GC when the process exits. That's fine for a long-running process.
Actually, looking at the code more carefully, I see that this middleware is typically instantiated once and used for the lifetime of the process. So a shared executor that's never shut down is fine.
But I'm still concerned about thread safety. If the backend's glob method is not thread-safe, using a shared executor with max_workers > 1 could cause issues.
Let me just fix the primary issue first. If the shared executor is needed, it can be added in a separate PR.
OK, final decision: just fix the primary issue (blocking shutdown). Here's the plan:
- Remove the
with statement around ThreadPoolExecutor
- Call
executor.shutdown(wait=False) in a finally block to release resources without blocking
- On timeout,
return happens, finally block runs shutdown(wait=False), no blocking
Let me implement this now.
Now I understand the problem completely. Let me implement the fix. The issue is that ThreadPoolExecutor.__exit__ calls shutdown(wait=True), which blocks until the running thread completes. Even after the TimeoutError is caught and we return, the with block's exit handler blocks.
The fix is to:
- Not use the
with statement (so shutdown(wait=True) is not called automatically)
- Call
executor.shutdown(wait=False) explicitly to release resources without blocking
<tool_call>
<function=bash_terminal>
<parameter=command>
sed -n '1270,1300p' /deepagents/libs/deepagents/deepagents/middleware/filesystem.py
</tool_call>