Skip to content
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

Method console.warn invoked with wrong context #484

Closed
vla74 opened this issue Dec 18, 2019 · 9 comments · Fixed by #499
Closed

Method console.warn invoked with wrong context #484

vla74 opened this issue Dec 18, 2019 · 9 comments · Fixed by #499
Assignees

Comments

@vla74
Copy link

vla74 commented Dec 18, 2019

Hi,

We're running Autobahn JS on various terminal types, including Samsung and LG Smart TVs (Chromium-based browsers).
With these Smart TVs, when the established connection is closed (for example after stopping the Crossbar server), we get JS errors like:

Uncaught TypeError: Illegal invocation

TypeError: Type error

In this case the AB connection's onclose handler is not called, and thereby our code is not aware that reconnection must be attempted.

This issue appeared from ABJS v19.10.1.
Connection handler self._transport.onclose calls:

log.warn("connection closed", reason, details);

https://github.com/crossbario/autobahn-js/blob/v19.10.1/packages/autobahn/lib/connection.js#L343

The log module defines its warn method as a direct reference to DOM console.warn:

var warn = console.warn;
exports.warn = warn;

https://github.com/crossbario/autobahn-js/blob/master/packages/autobahn/lib/log.js#L23

Execution of AB's log.warn does not cause issue on desktop browsers (Chrome, Firefox, Edge, IE11). However it fails on the affected Smart TVs because the warn Function is executed with a this context which is not the console object.
The Smart TV browsers may have a specific behaviour requiring this context (like redirecting the browser logs to an internal debugging system...).

To work on such platforms, Autobahn log module code should be something like:

var warn = console.warn.bind(console);

Note: AB's log.debug method should not be affected by this issue, as its code uses the console context.

Beyond the specific case of these Smart TV platforms, from a more general OOP point of view, the method of an object's instance should not be called outside of the context of this instance.

Thanks

@oberstet
Copy link
Contributor

ok, thanks for reporting! so just to be sure:

  1. while autobahn.log.debug works in your environment (samsung smart tvs), autobahn.log.warn does not work.
  2. the fix is a one liner (as linked above): var warn = console.warn; => var warn = console.warn.bind(console);

?

@vla74
Copy link
Author

vla74 commented Dec 19, 2019

You're welcome.

  1. After enabling the AB debug mode, I confirm that log.debug does not cause any exception on the affected Smart TVs, while log.warn does.
  2. Yes. In more details, I tested the 2 following fixes with the affected terminals, and both work:
  • Compact version with bind:
var warn = console.warn.bind(console);
  • Closure similar to the declaration of the debug function:
var warn = function () {
    console.warn.apply(console, arguments);
};

Function.prototype.bind normally requires an ECMAScript 5 compatible browser (http://kangax.github.io/compat-table/es5/#test-Function.prototype.bind), however I think that there's a polyfill embedded in ABJS (https://github.com/crossbario/autobahn-js/blob/master/packages/autobahn/lib/polyfill/function.js).

BTW, for the sake of consistency, let me suggest that debug and warn functions are declared the same way (so, probably using the bind method for both).

@hmistry-lz
Copy link

This is causing issue in IE 11. It breaks the add in that we are working on. It breaks on the same line as mentioned by @vla74
I am seeing below error on IE11

SCRIPT5022: InvalidAccessError
0.chunk.js (57211,9)
[bugsnag] Report not sent due to releaseStage/notifyReleaseStages configuration
{"code":1005,"wasClean":true}
SCRIPT65535: Invalid calling object
0.chunk.js (53095,7)

I have also enable debug mode in AB

@hmistry-lz
Copy link

@oberstet @om26er
If possible, Please share the timeline for this fix.

@vla74 have you done anything special to get around the issue you are facing while this bug gets fixed in AB?

@vla74
Copy link
Author

vla74 commented Apr 14, 2020

@om26er @oberstet Thank you for the fix.

@hmistry-lz As we're also facing issue #485, we decided to stick to AB version <19.10.1, waiting for the availability of fixes. Otherwise a workaround might consist in redefining console.warn before import of AB asset (console.warn = console.warn.bind(console);), but this is not really something we wanted to operate in production.

@om26er
Copy link
Contributor

om26er commented Apr 14, 2020

@vla74 @hmistry-lz I have released autobahn-js 20.4.1, with contains the fix for this bug as well. Kindly try that.

@vla74
Copy link
Author

vla74 commented Apr 15, 2020

@om26er We're not integrating Autobahn-js project directly but rather
autobahn-js-browser, and this one is not available in v20.4.1 yet. I can try as soon as you release an update.

@om26er
Copy link
Contributor

om26er commented Apr 15, 2020

I can try as soon as you release an update.

That is done now https://github.com/crossbario/autobahn-js-browser

@vla74
Copy link
Author

vla74 commented Apr 15, 2020

Many thanks for this new release. I confirm that it solves the issue for our Smart TVs.

Note: It seems that this 20.4 branch introduced the use of async / await syntax. I've had to polyfill this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants