Skip to content

StackOverflowError when using both incoming and outgoing relationships on the same node due to very deep recursive mapping #3039

@duwenice

Description

@duwenice

We encountered a problem in Spring Data Neo4j when a domain entity contains both incoming and outgoing relationships, and the query returns a deeply nested / complex graph structure (with the same nodes being connected through multi-level in/out relationships).

In such cases, the DefaultNeo4jEntityConverter recursively resolves the relationships and keeps creating nested entities.
When the returned subgraph is deep enough, the recursive mapping ends up consuming too many stack frames and eventually causes a java.lang.StackOverflowError.

Image

@query("""
MATCH (c:analyzer)
WHERE c.id IN $0
CALL apoc.path.subgraphAll(c, {
relationshipFilter: 'child>',
maxLevel: $1
})
YIELD nodes, relationships
UNWIND nodes AS node
OPTIONAL MATCH (node)-[r:child]->(a:analyzer)
RETURN node, collect(r),collect(a)
""")
List findAllChildren(Set tableIds, int maxLevel);

public class TableNode extends HiBaseNode {

@Relationship(type = Neo4jConstants.RelationShip.CHILD, direction = Relationship.Direction.INCOMING)
private Set<TableNode> parentNodes;

@Relationship(type = Neo4jConstants.RelationShip.CHILD)
private Set<TableNode> childNodes;

}

We observed that after increasing the JVM thread stack size (-Xss) the conversion succeeds, which indicates that the problem is caused by deep recursion during property filling.

Metadata

Metadata

Assignees

Labels

status: feedback-reminderWe've sent a reminder that we need additional information before we can continuestatus: waiting-for-feedbackWe need additional information before we can continue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions