openhab-addons: [jsscripting] Loading script files crashes openHAB

This is a follow-up bug report similar to #11952 which I was hoping would have been fixed by openhab/openhab-core#2681 and #12022, but the issue seems to remain in 3.4M5.

Expected Behavior

Loading a JS script from a file should not make the system unstable.

Current Behavior

After reloading a script file multiple times the system becomes slower and slower until it ultimately will become very unstable with timeouts.

Steps to Reproduce

Create this file somewhere as test1.sh:

rules.JSRule({
  name: "Reproduce jsscripting issue",
  description: "Reproduce jsscripting issue",
  triggers: [triggers.ItemStateChangeTrigger('Issue12577', 'OFF', 'ON')],
  execute: data => {
    console.log("Test item triggered: 1");
  }
});

And this one as test2.sh:

rules.JSRule({
  name: "Reproduce jsscripting issue",
  description: "Reproduce jsscripting issue",
  triggers: [triggers.ItemStateChangeTrigger('Issue12577', 'OFF', 'ON')],
  execute: data => {
    console.log("Test item triggered: 2");
  }
});

And finally, create this script as reproduce_issue_12577.sh:

#!/bin/bash
echo "Switch Issue12577" > $OPENHAB_CONF/items/issue_12577.items
i=1
while [ $i -lt 50 ]
do
    if [ $((i % 2)) -eq 0 ]
    then
        cp test1.js "$OPENHAB_CONF/automation/js/test.js"
    else
        cp test2.js "$OPENHAB_CONF/automation/js/test.js"
    fi
    sleep 5
    ((i++))
done

Set execute permission:

chmod +x reproduce_issue_12577.sh

Now run this script and wait a few minutes. For me, after running all 50 iterations, the last log items will look something like this:

2022-04-04 22:27:53.195 [INFO ] [rulesupport.loader.ScriptFileWatcher] - Loading script '/etc/openhab/automation/js/test.js'
2022-04-04 22:28:48.771 [INFO ] [org.openhab.automation.script.rules ] - Adding rule: Reproduce jsscripting issue
2022-04-04 22:28:52.169 [INFO ] [rulesupport.loader.ScriptFileWatcher] - Loading script '/etc/openhab/automation/js/test.js'
2022-04-04 22:29:49.940 [WARN ] [ab.core.internal.events.EventHandler] - Dispatching event to subscriber 'org.openhab.core.internal.items.ItemUpdater@8e17f4' takes more than 5000ms.

After this point the system is no longer usable and a complete openHAB restart is needed. CPU usage of the java process is maxed out. Please note that from loading the script and until the rule was being added, almost a minute passed.

Context

This problem prevents migration from DSL rules to jsscripting, at least for users writing rules in files.

Your Environment

openHABian 1.7, openHAB 3.2-3.3.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (25 by maintainers)

Most upvoted comments

I took a look at this today. I did find a leak, which I have removed in https://github.com/openhab/openhab-addons/pull/12725.

@jlaur I will try this weekend to have at least a closer look at this.

I can confirm that it is still there, 50 iterations of the script above result in the following:

Bildschirmfoto 2022-06-06 um 11 25 26

So still the ScriptEngines are not garbage collected since someone holds a reference to them.

Absolutely - I’ll take a look at that.

I can’t reproduce your failure (probably I have too much memory), but I can see an increasing use of heap memory due to rule-related objects not being unloaded properly:

Bildschirmfoto 2022-04-06 um 18 05 14

This is caused by a bug in core which will be fixed by https://github.com/openhab/openhab-core/pull/2897.

@jpg0 - I’ve tried to provide some information for reproducing a stability issue with jsscripting I’m still having with the 3.3 milestone releases. I hope it will be possible to reproduce, otherwise please let me know of any additional details I can provide.