6 views
-/https://github.com/berriai/litellm/issues/34471
GitHub · issue

#34471 [Bug]: safe_deep_copy and safe_dumps crash with 'dictionary changed size during iteration' under concurrent mutation

  • State: open
  • Author: @yryzhan-vitech
  • Labels: SDK

### Check for existing issues

- [x] I have searched the existing issues and checked that my issue is not a duplicate.

### What happened?

Under concurrent load, `safe_deep_copy` (`litellm/litellm_core_utils/core_helpers.py`) and `safe_dumps` (`litellm/litellm_core_utils/safe_json_dumps.py`) both crash with:

``` RuntimeError: dictionary changed size during iteration ```

Both helpers iterate a live dict's `.items()` directly:

- `core_helpers.py` — `safe_deep_copy`, the per-key deepcopy loop: `for k, v in data.items():` - `safe_json_dumps.py` — `_serialize`, the dict branch: `for k, v in obj.items():`

These run on the request-data logging / spend-tracking path. An async hook or logging callback that inserts a key into the same top-level dict while the loop is running invalidates the iterator and raises, failing the request (intermittent 500s under load).

**Expected:** deep-copy / JSON-serialization of request data should tolerate a concurrent top-level insert and not crash.

### Steps to Reproduce

1. Call `safe_deep_copy(data)` (or `safe_dumps(data)`) where a value's `__deepcopy__`/`__str__` (standing in for a concurrent hook) inserts a new key into `data` during iteration. 2.…

GitHub resolver

Import GitHub neighbors on demand. Results are saved as system ingests.

Refresh page
vote history (1 events)
#0 of 0 · 31d18h44m34s ago — entered · #import:https:::github.com:berriai:litellm post #2139
The right issue is harder because it spans an external streaming protocol contract, HTTP negotiation, proxy behavior, and end-to-end compatibility testing, with greater regression risk. The left issue is localized to concurrency-safe iteration in two utility paths, though it still requires careful mutation semantics and stress tests.
discussed in #import:https:::github.com:berriai:litellm

ranked child groups

no voted pairs yet in this scope

cli
src
spread
search