# Fix rate limiter holding permits during tool execution \(\#47494\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/29cf14ed2fc72c36d4502120daad09ded049ea6e)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 29cf14ed2fc72c36d4502120daad09ded049ea6e

Requested commit: 29cf14ed2fc72c36d4502120daad09ded049ea6e

Commit: 29cf14ed2fc72c36d4502120daad09ded049ea6e

Tree: c3e96a9ef11aa6c8bae92bff963f6c6b78a5efde

Author: Richard Feldman

Committer: GitHub

## Message

```
Fix rate limiter holding permits during tool execution (#47494)

The rate limiter's semaphore guard was being held for the entire
duration of a turn, including during tool execution. This caused
deadlocks when subagents tried to acquire permits while parent requests
were waiting for them to complete.

## The Problem

In `run_turn_internal`, the stream (which contains the `RateLimitGuard`
holding the semaphore permit) was kept alive throughout the entire loop
iteration - including during **tool execution**:

1. Parent request acquires permit
2. Parent starts streaming, consumes response
3. Parent starts executing tools (subagents)
4. **Stream/guard still held** while tools execute
5. Subagents try to acquire permits → blocked because parent still holds
permit
6. Deadlock if all permits are held by parents waiting for subagent
children

## The Fix

Two changes were made:

1. **Drop the stream early**: Added an explicit `drop(events)` after the
stream is fully consumed but before tool execution begins. This releases
the rate limit permit so subagents can acquire it.

2. **Removed the `bypass_rate_limit` workaround**: Since the root cause
is now fixed, the bypass mechanism is no longer needed.

Note: no release notes because subagents are still feature-flagged, and
this rate limiting change isn't actually observable without them.

Release Notes:

- N/A
```

## Parents

- [9ef825d753e43f02964616e76384bd9de066c76b](https://git.cafe/gitcafe/zed/commit/9ef825d753e43f02964616e76384bd9de066c76b?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/29cf14ed2fc72c36d4502120daad09ded049ea6e?format=markdown)
