fix(merged_tree): introduce cycle detection to mitigate a comment bundling bug #611

Open
ada4a wants to merge 1 commit from ada4a/mergiraf:609-stack-overflow into main
Owner

For #609

For #609
fix(merged_tree): introduce cycle detection to mitigate a comment bundling bug
All checks were successful
/ test (pull_request) Successful in 46s
d2e42c508d
ada4a force-pushed 609-stack-overflow from d2e42c508d to aa461e4e57 2025-10-07 17:47:02 +02:00 Compare
wetneb left a comment
Owner

I had a look at the panicking test case, examples/java/failing/successor_loop, which triggers

WARN detected a cycle in `force_line_based_fallback_on_specific_nodes`: CommutativeChildSeparator(\n)

This seems off: it is expected that a merged tree will contain multiple identical copies of CommutativeChildSeparator, which aren't symptoms of any problem.
The loop detection should rather be done at the Leader or AstNode level: there, I think it should indeed be a bug if we encounter the same one multiple times, although probably not if we recurse into Conflict nodes.

I'm curious how you came to the conclusion that the stack overflow comes from this particular method (I don't dispute it, but I struggle to understand how the comment bundling might lead to cycles here…)

I had a look at the panicking test case, `examples/java/failing/successor_loop`, which triggers ``` WARN detected a cycle in `force_line_based_fallback_on_specific_nodes`: CommutativeChildSeparator(\n) ``` This seems off: it is expected that a merged tree will contain multiple identical copies of `CommutativeChildSeparator`, which aren't symptoms of any problem. The loop detection should rather be done at the `Leader` or `AstNode` level: there, I think it should indeed be a bug if we encounter the same one multiple times, although probably not if we recurse into `Conflict` nodes. I'm curious how you came to the conclusion that the stack overflow comes from this particular method (I don't dispute it, but I struggle to understand how the comment bundling might lead to cycles here…)
Author
Owner

I'm curious how you came to the conclusion that the stack overflow comes from this particular method

It's been a bit since I wrote the original patch, so I don't remember all the details, but I think this method was what was repeated in the logs of the original panic, so that's what I went on to patch. But I think the bug in comment bundling just creates cycles in general, and so it might be hard to predict which particular method is going to explode because of that.

This seems off: it is expected that a merged tree will contain multiple identical copies of CommutativeChildSeparator, which aren't symptoms of any problem.
The loop detection should rather be done at the Leader or AstNode level

I think you're right; would you have a suggestion as to which part of the code I could put the loop detection in? What I'm thinking of is doing it right after the construction of the AST honestly, by employing one of the cycle detection algorithms

> I'm curious how you came to the conclusion that the stack overflow comes from this particular method It's been a bit since I wrote the original patch, so I don't remember all the details, but I think this method was what was repeated in the logs of the original panic, so that's what I went on to patch. But I think the bug in comment bundling just creates cycles _in general_, and so it might be hard to predict which particular method is going to explode because of that. > This seems off: it is expected that a merged tree will contain multiple identical copies of `CommutativeChildSeparator`, which aren't symptoms of any problem. The loop detection should rather be done at the `Leader` or `AstNode` level I think you're right; would you have a suggestion as to which part of the code I could put the loop detection in? What I'm thinking of is doing it right after the construction of the AST honestly, by employing one of the [cycle detection algorithms](https://stackoverflow.com/questions/261573/efficient-algorithm-for-detecting-cycles-in-a-directed-graph)
Owner

I think the bug in comment bundling just creates cycles in general

Hmm, I'm curious how that would happen. But if it does, then those would be cycles of AstNodes already? If that's the case, then the stack overflow would happen basically on any further transformation run on the trees, such as their matching.

I think you're right; would you have a suggestion as to which part of the code I could put the loop detection in?

I don't know, I would try to find a concrete example that reaches a stack overflow and take it from there. I can run a large-scale benchmark again and try to find a fitting case.

> I think the bug in comment bundling just creates cycles in general Hmm, I'm curious how that would happen. But if it does, then those would be cycles of `AstNode`s already? If that's the case, then the stack overflow would happen basically on any further transformation run on the trees, such as their matching. > I think you're right; would you have a suggestion as to which part of the code I could put the loop detection in? I don't know, I would try to find a concrete example that reaches a stack overflow and take it from there. I can run a large-scale benchmark again and try to find a fitting case.
Owner

I found an example: #609 (comment)

I found an example: https://codeberg.org/mergiraf/mergiraf/issues/609#issuecomment-7659997
All checks were successful
/ test (pull_request) Successful in 46s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 609-stack-overflow:ada4a-609-stack-overflow
git switch ada4a-609-stack-overflow
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: mergiraf/mergiraf#611
No description provided.