Skip to content

fix(serializer): Don't call __iter__ on arbitrary sequences#6304

Merged
sentrivana merged 8 commits into
masterfrom
ivana/dont-iter-random-objects
May 21, 2026
Merged

fix(serializer): Don't call __iter__ on arbitrary sequences#6304
sentrivana merged 8 commits into
masterfrom
ivana/dont-iter-random-objects

Conversation

@sentrivana
Copy link
Copy Markdown
Contributor

@sentrivana sentrivana commented May 20, 2026

Description

The serializer will attempt to serialize whatever objects are thrown into it. This includes arbitrary local variables in folks' programs that might have all sorts of side-effects.

Notably, if an object is a Sequence (it has __iter__, __len__, and __getitem__), we will try to __iter__ over it. Which is potentially an unwise thing to do, as we know nothing about the object's custom __iter__ implementation. It can, for example, acquire a non-reentrant lock. In some scenarios, this can lead to deadlocks caused by the SDK.

In this PR, the serializer is changed so that we don't try to __iter__ over all Sequences, just built-in stdlib ones. Of course, nothing is preventing folks from defining side-effecty lists, tuples and sets, but it's a bit less likely to happen than on a custom class. Custom sequences will now be simply __repr__'d.

Issues

Reminders

@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 20, 2026

PY-2124

Comment thread sentry_sdk/serializer.py Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 20, 2026

Codecov Results 📊

282 passed | Total: 282 | Pass Rate: 100% | Execution Time: 43.06s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 14891 uncovered lines.
✅ Project coverage is 33.42%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
serializer.py 86.10% ⚠️ 26 Missing and 11 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    33.42%    33.42%        —%
==========================================
  Files          190       190         —
  Lines        22366     22367        +1
  Branches      7594      7594         —
==========================================
+ Hits          7475      7476        +1
- Misses       14891     14891         —
- Partials       744       744         —

Generated by Codecov Action

Comment thread sentry_sdk/serializer.py Outdated
@sentrivana sentrivana changed the title fix(serializer): Don't __iter__ arbitrary sequences fix(serializer): Don't call __iter__ on arbitrary sequences May 20, 2026
@sentrivana sentrivana marked this pull request as ready for review May 20, 2026 10:43
@sentrivana sentrivana requested a review from a team as a code owner May 20, 2026 10:43
Copy link
Copy Markdown
Contributor

@alexander-alderman-webb alexander-alderman-webb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly leads to some data loss but it's worth it to not interfere with users apps IMO.

@sentrivana sentrivana merged commit 0af4a8b into master May 21, 2026
157 checks passed
@sentrivana sentrivana deleted the ivana/dont-iter-random-objects branch May 21, 2026 06:04
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.

Deadlock during GC with Sentry + Opentelemetry + Tornado + aiohttp

3 participants