Skip to content

fix: prevent StackOverflowError in ObjectView JSON mode for circular references#3160

Open
daguimu wants to merge 1 commit intoalibaba:masterfrom
daguimu:fix/objectview-stackoverflow-3095
Open

fix: prevent StackOverflowError in ObjectView JSON mode for circular references#3160
daguimu wants to merge 1 commit intoalibaba:masterfrom
daguimu:fix/objectview-stackoverflow-3095

Conversation

@daguimu
Copy link
Copy Markdown
Contributor

@daguimu daguimu commented Mar 24, 2026

Problem

When using watch command with JSON output mode (options json true), fastjson2 serialization causes StackOverflowError on objects with circular references or deeply nested object graphs.

Stack trace example from issue #3095:

java.lang.StackOverflowError: null
    at com.alibaba.arthas.deps.com.alibaba.fastjson2.writer.ObjectWriterAdapter.hasFilter(ObjectWriterAdapter.java:132)
    at com.alibaba.arthas.deps.com.alibaba.fastjson2.writer.ObjectWriter3.write(ObjectWriter3.java:62)
    ...

Fix

  • Add JSONWriter.Feature.ReferenceDetection to detect circular references and serialize them using JSON $ref syntax instead of infinite recursion
  • Catch StackOverflowError as a safety net and return a user-friendly error message with a hint to disable json mode (options json false)

Test

Added unit test testJsonModeCircularReference that creates a circular reference (A→B→A) and verifies no exception is thrown.

Fixes #3095

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 24, 2026

CLA assistant check
All committers have signed the CLA.

@daguimu daguimu force-pushed the fix/objectview-stackoverflow-3095 branch from 4bf05c8 to b19886f Compare March 24, 2026 20:07
…references

When watch command uses JSON format (options json true), fastjson2
serialization can enter infinite recursion on objects with circular
references or deeply nested object graphs, causing StackOverflowError.

Fix by adding JSONWriter.Feature.ReferenceDetection to detect and break
circular references using JSON $ref syntax, and catch StackOverflowError
as a safety net with a user-friendly error message.

Fixes alibaba#3095
@daguimu daguimu force-pushed the fix/objectview-stackoverflow-3095 branch from b19886f to 12d3b50 Compare March 24, 2026 20:11
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.

watch 命令使用 json 格式时,容易出现 java.lang.StackOverflowError: null

2 participants