-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[jsscripting] Fix multi-threading issues in UI-based scripts #17510
Conversation
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
… Core can lock and unlock when executing the pre-compiled script Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…-threading issues with UI-based scripts Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
@@ -230,7 +229,10 @@ public Path toRealPath(Path path, LinkOption... linkOptions) throws IOException | |||
protected void beforeInvocation() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is this method called (when executing a script)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is called everytime before the engine executes code, there is only one exception: When a compiled script is executed, this won't be called (it has been called when the script was compiled, so the setup already happened).
See https://github.com/openhab/openhab-addons/blob/main/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable.java, there is beforeInvocation
called.
Any feedback on this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As nobody has a better idea - let's merge this. Thanks @florian-h05!
Just confirmed this fix works on my
|
Not to be overly critical but that doesn't sound like a fix for a specific issue as mush as a mitigation against an unknown issue. There is no guarantee that 1/100 seconds is enough given that we do not know the root cause; it may simply happen less often with this mitigation. Having said that, this is an improvement and I cannot judge if it's worth investigating deeper right now or focusing on other things. I don't know what the policy is for using the bug tracker to track things, but personally, I'd leave an issue open as a reminder to come back to remove the |
…implements Lock This moves the locking mechanism added in openhab#4402 to the inheritors of AbstractScriptModuleHandler to synchronize execution context access as well. This fixes the problem thathttps://github.com/openhab/openhab-addons/pull/17510 worked around by using Thread.sleep. Signed-off-by: Florian Hotze <dev@florianhotze.com>
I was not satisfied with this solution so I started some futher investigation once I had the time to do so, and after checking the core code again, I found the root cause and created a fix: openhab/openhab-core#4426 |
…implements Lock (#4426) This moves the locking mechanism added in #4402 to the inheritors of AbstractScriptModuleHandler to synchronize execution context access as well. This fixes the problem thathttps://github.com/openhab/openhab-addons/pull/17510 worked around by using Thread.sleep. Signed-off-by: Florian Hotze <dev@florianhotze.com>
…#17510) * [jsscripting] Make logger non-static * [jsscripting] Log lock acquisition and release * [jsscripting] Implement Lock in DebuggingGraalScriptEngine so openHAB Core can lock and unlock when executing the pre-compiled script * [jsscripting] Improve comments in ThreadsafeTimers * [jsscripting] Add a short sleep after unlocking the lock to fix multi-threading issues with UI-based scripts Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
…#17510) * [jsscripting] Make logger non-static * [jsscripting] Log lock acquisition and release * [jsscripting] Implement Lock in DebuggingGraalScriptEngine so openHAB Core can lock and unlock when executing the pre-compiled script * [jsscripting] Improve comments in ThreadsafeTimers * [jsscripting] Add a short sleep after unlocking the lock to fix multi-threading issues with UI-based scripts Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This fixes illegal multi-thread access requests to the GraalJS context for UI-based rules.
Those have two causes:
This is fixed through the following:
Lock
interface in the ScriptEngine that gets passed to openHAB Core.The issue can be reproduced by pasting the following script into a script inside the UI and running it: