Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print abstract modifier once and with proper spaces #4318

Closed
wants to merge 1 commit into from

Conversation

algomaster99
Copy link
Contributor

@algomaster99 algomaster99 commented Nov 30, 2021

Reference: ASSERT-KTH/sorald#601

This is a bizarre issue because of the randomness involved (found a Heisenbug).

If you directly run this test, it will fail because it modifies the file as such:

package sniperPrinter;

public class PrintAbstractOnceWithProperSpacing {
-     static abstract class AbstractFoo
+     abstractstatic abstract class AbstractFoo
    {

    }
}

However, when you want to debug why this happens, the test gets a bit flaky. In other words, sometimes it passes, and sometimes it fails.

If you directly press debug, without setting any breakpoints in code, the tests sometimes fail, and it passes the other times. I haven't been able to figure out what is going wrong simply because of the random behaviour.

Now if you set a breakpoint at here, the program will stop there twice - 1) for printing modifiers of PrintAbstractOnceWithProperSpacing 2) for printing modifiers of AbstractFoo. Just press "Resume Program (F9)" twice and this test will pass 🤯 .

@khaes-kth is suggesting that it probably has to do with how the program is split into threads. Could you please elaborate?

@algomaster99
Copy link
Contributor Author

@MartinWitt @slarse have a look at this when you get time.

@algomaster99
Copy link
Contributor Author

Screencast of the flaky behaviour:
abstract-static

@khaes-kth
Copy link

Cool and worrying screenshot :)

@I-Al-Istannen
Copy link
Collaborator

I-Al-Istannen commented Dec 1, 2021

I have absolutely no idea how anything works but this issue seems to stem from the undefined order of modifiers in CtModifierHandle. In ElementPrinterHelper#writeModifiers the secondPosition list is either [static, abstract] in which case it can match that onto the source (static abstract class) or [abstract, static] in which case it can not and creates that garbled output.

The order seems to depend on the order of an unspecified set in the CtModifierHandle which probably ends up depending on the hashcode order of the underlying enum, which varies from JVM start to JVM start and hence creates some flakey output. In my unlucky periods I hit that issue after retrying 10 or more times, sometimes instantly on the first try but it was always reproducible.

Hashcodes, you must love em!

Maybe this can serve as a starting point for Slarse or Martin :)

@slarse
Copy link
Collaborator

slarse commented Dec 2, 2021

Probably related to #4033

Regardless of how the modifiers are "ordered", the sniper printer still shouldn't print that output. The non-determinism of it all is probably due #4033, but there's still a bug in the sniper printer.

@monperrus
Copy link
Collaborator

conflict here

@algomaster99
Copy link
Contributor Author

algomaster99 commented Jan 19, 2022

@monperrus I have reproduced the issue in this PR. I have not worked on a fix for this.

@algomaster99
Copy link
Contributor Author

I will restart work on this when I feel it's blocking something because it is a tough one to deal with.

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.

5 participants