Closed Garfonso closed 3 years ago
@AlCalzone any idea how to best protect the sandbox for unhandled promise rejections?
@Apollon77 it looks like our error code in js-controller is at least executed:
unhandled promise rejection: undefined
It probably has a bug though, since https://github.com/ioBroker/ioBroker.javascript/blob/4c6f93ab1520e2ffb82b126be5e628e12da42902/main.js#L644-L672 does not seem to do anything.
@AlCalzone Do an unhandled promise rejection has a stack?
Logging wise when checking controller this "Log behaviour" can only happen when:
https://github.com/ioBroker/ioBroker.js-controller/blob/3.1.x/lib/adapter.js#L8320-L8326
And becasue of that the whole handling in javascript adapter only reacts on "err.stack"
Yes, it essentially passes an Error around (or whatever was used as the rejection reason). You see its stack in the above log as a warning. Not sure why that no longer exists in the error handler. @Garfonso do you have a chance to set a breakpoint inside the error method I posted above and run javascript with a debugger attached?
I'm not that sure ...
the "State "id" not found" warning log comes from sandbox.js L368 also with the stack because "context.logWithLineInfo.warn" is used!!
after that the
callback.call(sandbox, 'State "' + id + '" not found');
happens and is promisified by tools.js promisify method to a rejected promise
Okay then we need a custom-made promisified version of setState that does not throw.
Or we find a way to really detect a unhandled promise rejection ... maybe the error object is "just different" data wise?
or the javascript adapter error handler assumes an unhandled rejection when err..stack is undefined and also consider it "handled" automatically?
My bet is on err is a string, since we didn't convert that to pass an error:
callback.call(sandbox, 'State "' + id + '" not found');
hhuuuuu ... so maybe add that to the javascript rpomisify function and test?
Yes, it essentially passes an Error around (or whatever was used as the rejection reason). You see its stack in the above log as a warning. Not sure why that no longer exists in the error handler. @Garfonso do you have a chance to set a breakpoint inside the error method I posted above and run javascript with a debugger attached?
I can do that, maybe today or tomorrow in the afternoon. Is this still relevant or did you figure it out by yourselves? (I did not fully get all of your conversation 😄 )
It would help to verify if err
is indeed a string there.
Alternatively we propose the lib/tools.js change in javascript adapter to make sure it is an error and retest that script again and see if error message is different
Jup, err is just a string not an Error-Object.
I tried to understand your proposed solution and implemented it here: https://github.com/Garfonso/ioBroker.javascript/commit/83ff6407f2bc8a30efdef93a47066767b50dab59
With that change the rejection is handled and an error message printed and the adapter keeps running. If the change is what you meant, I'll open a PR.
awesome!
The only question is if we consider this as "breakign "or not? I would say not because not that many users use async stuff in javasscript scripts. I would do a minor increase with this. Option opinions?
One test (macos, node 14) failed... did not yet understand, why..? Is this known to happen?
The only question is if we consider this as "breakign "or not? I would say not because not that many users use async stuff in javasscript scripts. I would do a minor increase with this. Option opinions?
I don't think it is breaking. Only in the case that somebody catches an error from one of the promisified functions and expects a string. I don't think anybody really does that and the toString() function of Error does a pretty good job, too. I'd do a minor increase, too.
restart tests. there is still a "timing issue" somewhere ... somewhen wee need to find it" :-))
Describe the bug
setStateAsync with unknown id and no try/catch crashes javascript adatper
To Reproduce
Steps to reproduce the behavior:
aync function xy() { await setStateAsync('alias.0.somethingnotexisting', true); }; xy();
(maybe the await is not necessary, but I had it in my code)Expected behavior
Javascript Adapter should try to catch this error and write an error to logfile and not crash with stopping all scripts. :-)
Screenshots & Logfiles
It seems to stop all scripts before exit. But still it exits.
Versions: