There is a limitation in embedded GraalVM JS upon which JS Scripting runs, and since Blockly compiles to JS Scripting, this impacts Blockly too. In short, only one thread can access a “context” at a time. When a second thread attempts to access the context while it’s already being accessed by another thread that Multi-threaded excepotion is thrown.
I think even before OH 4.0 release changes were made in openhab-js and the JS Scripting add-on to put a lock around the context so if the context is in use by a rule or by a timer, what ever wants access now has to wait for that lock to be released.
However, even since then I’ve noticed some cases where this lock either doesn’t work as expected or there is something else that happens that needs the context while a rule or timer is running because I still rarely see this exception.
Usually this exception will happen when you have a single rule that gets triggered a lot really fast which create Timers, which is exactly the situation here.
First file an issue so this doesn’t get forgotten. This code may be concise enough to reproduce the error more easily than the rule I have that is generating the exception.
In the mean time, some ideas which should work, though they will require some small deviations from this code. All of these entail keeping a list of those Items to be toggled and using a single timer/rule to toggle them on and off instead of giving them all their own timers.
- Use the same timer and simply add/remove the Items to toggle every five seconds to that one timer.
- Instead of a timer, use a separate rule which runse every five seconds to toggle the Items. You can use the shared cache to control the list of Items which should be toggled.
- Use Looping Timer from openHAB Rules Tools [4.1.0.0;5.9.9.9] to toggle the lights
It’s more book keeping but becuase you don’t have lots of Timers trying to run at the same time it should be very unlikely to have two threads trying to access the context at the same time.