fix: Avoid stack overflow when deserializing large flat JSON objects#5361
Open
fix: Avoid stack overflow when deserializing large flat JSON objects#5361
Conversation
Replace JsonObjectDeserializer's recursive token parsing with an iterative loop. The parser already tracks state explicitly, so recursion was only used to advance to the next JSON token and could overflow on large flat maps. Relates to getsentry/sentry-dart#3668
📲 Install BuildsAndroid
|
vm might not respect stack size, so increase the frame count (with smaller payload) to make test more robust
buenaflor
reviewed
May 4, 2026
Comment on lines
+180
to
+219
| fun `deserializes event with large flat modules map on a small stack`() { | ||
| val moduleCount = 50000 | ||
| val json = buildString { | ||
| append("{\"event_id\":\"00000000000000000000000000000000\",\"modules\":{") | ||
| repeat(moduleCount) { | ||
| if (it > 0) { | ||
| append(',') | ||
| } | ||
| append("\"m") | ||
| append(it) | ||
| append("\":\"v\"") | ||
| } | ||
| append("}}") | ||
| } | ||
|
|
||
| val error = AtomicReference<Throwable?>() | ||
| val event = AtomicReference<SentryEvent?>() | ||
| val thread = | ||
| Thread( | ||
| null, | ||
| Runnable { | ||
| try { | ||
| event.set( | ||
| JsonSerializer(SentryOptions()) | ||
| .deserialize(StringReader(json), SentryEvent::class.java) | ||
| ) | ||
| } catch (throwable: Throwable) { | ||
| error.set(throwable) | ||
| } | ||
| }, | ||
| "large-flat-modules-repro", | ||
| 1024L * 1024L, | ||
| ) | ||
|
|
||
| thread.start() | ||
| thread.join() | ||
|
|
||
| assertNull(error.get()) | ||
| assertEquals(moduleCount, event.get()?.modules?.size) | ||
| } |
Contributor
There was a problem hiding this comment.
does this test fail with the previous (recursive) variant?
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 8687935 | 294.00 ms | 304.02 ms | 10.02 ms |
| 6edfca2 | 305.52 ms | 432.78 ms | 127.26 ms |
| b6702b0 | 395.86 ms | 409.98 ms | 14.12 ms |
| a1eadfa | 345.67 ms | 411.26 ms | 65.59 ms |
| 889ecea | 367.58 ms | 437.52 ms | 69.94 ms |
| e2dce0b | 315.85 ms | 369.20 ms | 53.35 ms |
| d15471f | 322.58 ms | 396.08 ms | 73.50 ms |
| 62b579c | 349.26 ms | 426.26 ms | 77.00 ms |
| cf708bd | 434.73 ms | 502.96 ms | 68.22 ms |
| 6edfca2 | 316.43 ms | 398.90 ms | 82.46 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 8687935 | 1.58 MiB | 2.19 MiB | 619.17 KiB |
| 6edfca2 | 1.58 MiB | 2.13 MiB | 559.07 KiB |
| b6702b0 | 1.58 MiB | 2.12 MiB | 551.79 KiB |
| a1eadfa | 0 B | 0 B | 0 B |
| 889ecea | 1.58 MiB | 2.11 MiB | 539.75 KiB |
| e2dce0b | 0 B | 0 B | 0 B |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| 62b579c | 0 B | 0 B | 0 B |
| cf708bd | 1.58 MiB | 2.11 MiB | 539.71 KiB |
| 6edfca2 | 1.58 MiB | 2.13 MiB | 559.07 KiB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📜 Description
Replace JsonObjectDeserializer's recursive token parsing with an iterative loop. The parser already tracks state explicitly, so recursion was only used to advance to the next JSON token and could overflow on large flat maps.
💡 Motivation and Context
Relates to getsentry/sentry-dart#3668
💚 How did you test it?
Added a regression test. It fails with StackOverflow before the test.
📝 Checklist
sendDefaultPIIis enabled.