Skip to content

RuntimeError: dictionary changed size during iteration in anti_detection payload_obfuscator when sending JSON payload #5

@y3yhm

Description

@y3yhm

Hi, thanks for maintaining ai-cloudscraper.

I found a reproducible RuntimeError when using ai-cloudscraper 3.8.4 with POST requests that pass payload via json=....

Observed error:
RuntimeError: dictionary changed size during iteration

Traceback consistently points to:
cloudscraper/anti_detection.py
pre_request_processing -> payload_obfuscator.obfuscate_payload -> _obfuscate_dict -> _encode_values
with the failing line similar to:
for key, value in data.items()

Important observations:

  1. The issue reproduces under multiple modes:
    • hybrid + turbo
    • hybrid (without turbo)
    • compatibility_mode=True
  2. It is not tied to one specific business payload. It appears in both:
    • /web/message
    • /marketplace/explore/items
  3. Request snapshots show that the outgoing payload dict is structurally stable before request dispatch.
  4. The problem seems to happen inside the library while processing kwargs['json'] during payload obfuscation.

Workaround on application side:
Converting all json=payload calls to:

  • data=json.dumps(payload)
  • plus Content-Type: application/json
    appears to avoid the failing obfuscation path.

Suspected root cause:
The library may be iterating over a dict while mutating it internally during payload obfuscation.

Suggested fix:
Please review the payload obfuscation path for kwargs['json'], especially any code that iterates over dict items while mutating the same dict.
A safer approach may be:

  • iterate over list(data.items())
  • or deep-copy the dict before mutation

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions