Skip to content

fix(gateway): fix dictionary iteration race condition during stop#1195

Closed
usvimal wants to merge 1 commit intoNousResearch:mainfrom
usvimal:fix/gateway-race-condition
Closed

fix(gateway): fix dictionary iteration race condition during stop#1195
usvimal wants to merge 1 commit intoNousResearch:mainfrom
usvimal:fix/gateway-race-condition

Conversation

@usvimal
Copy link
Contributor

@usvimal usvimal commented Mar 13, 2026

Problem

The method iterates over while another async task could be modifying the dict, causing:

Solution

Copy the dict before iterating using .

Changes

  • Line 721: Added wrapper around
  • Added comment explaining the fix

This is a minimal, safe fix that prevents the race condition during gateway shutdown.

The stop() method iterates over self.adapters.items() while another async
task could be modifying the dict. Copy the dict before iterating to avoid
RuntimeError: dictionary changed size during iteration.

Fixes race condition that occurred during gateway shutdown.
@usvimal usvimal force-pushed the fix/gateway-race-condition branch from ffdb9bf to 3350714 Compare March 13, 2026 18:56
@teknium1
Copy link
Contributor

Thanks for the PR. I'm closing this as redundant because the shutdown fix is already present on current main.

Gateway.stop() already iterates over a copy of the adapters dict:

  • for platform, adapter in list(self.adapters.items()):

So the dictionary changed size during iteration case this PR addresses has already been fixed upstream. In review I also confirmed the current line is already on main via blame/history, so there's nothing left to salvage here.

Appreciate the fix.

@teknium1 teknium1 closed this Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants