antlr / intellij-plugin-v4

An IntelliJ plugin for ANTLR v4
https://plugins.jetbrains.com/plugin/7358-antlr-v4
BSD 3-Clause "New" or "Revised" License
460 stars 103 forks source link

ANTLR plugin high CPU utilization during idle #702

Open gravityfox opened 2 months ago

gravityfox commented 2 months ago

Windows 10 x64 IntelliJ IDEA 2024.1.1 IDEA plugin version 1.23.1

When starting the IDE with the plugin enabled, CPU utilization is normal.

After opening a .g4 file, CPU usage spikes and stays high.

CPU utilization continues staying high if the file is closed. It also stays high if parsing is stopped by the "stop" button in the preview pane. The preview pane does not ever need to have opened for the high utilization to start.

When setting idea64.exe affinity to just cores 0 and 2 (to avoid hyper-threading), both cores are at 100% utilization. Giving IDEA additional cores distributes the load, which suggests 2 no-sleep looping threads.

Closing the project does not cause this utilization to come back down. Only terminating the IDE and restarting causes idle utilization to be normal again.

This anomalous utilization does not occur if the plugin is disabled.

After some testing, I discovered that this does not happen to all grammars. I have created an offending grammar that causes this issue:

grammar Bad;

start: list EOF;

list: x list;

x: 'test';
bjansen commented 2 months ago

Hi, and thanks for the detailed description.

It seems your problem is related to a bad grammar. To fix https://github.com/antlr/intellij-plugin-v4/issues/399 I implemented a "watchdog" that prevents the grammar interpreter from being stuck in an infinite loop when it parses such grammars. It's working correctly, but in your case the error recovery strategy is somehow trying to recover again and again from the exception thrown by the watchdog.

You can 'fix' your grammar by removing the infinite recursion:

list: x list?;

And on my side I'll fix the plugin to avoid the infinite recovery and try to provide a useful error message.

BTW antlr itself (not the IJ plugin) also crashes on your grammar:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.antlr.v4.gui.TestRig.process(TestRig.java:207)
    at org.antlr.v4.gui.TestRig.process(TestRig.java:158)
    at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
Caused by: java.lang.StackOverflowError
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)
    at org.antlr.v4.runtime.atn.PredictionContext.fromRuleContext(PredictionContext.java:72)