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

Returning MediaInfo library as a Promise Hangs the Browser #17

Closed
dgp1130 opened this issue Nov 16, 2018 · 1 comment
Closed

Returning MediaInfo library as a Promise Hangs the Browser #17

dgp1130 opened this issue Nov 16, 2018 · 1 comment

Comments

@dgp1130
Copy link

dgp1130 commented Nov 16, 2018

Consider the following JavaScript code:

function init() {
  return new Promise((resolve) => {
    const lib = MediaInfo(() => {
      resolve(lib);
    });
  });
}

init().then((lib) => console.log(lib));

While perfectly reasonable on the surface, running it actually hangs the browser. The log statement is never printed and the entire UI freezes in Chrome. Add a button to the HTML, try to click it and it won't animate. It also locks down the tab and refreshes fail. You have to completely kill the tab and re-open a new one to try again. The same thing happens if you try to use async/await patterns.

I believe the problem here is the Module.then() function. My lib variable is effectively a Module (looking at the obfuscated source here) and the Module has a then() function (reproduced below and pretty-printed).

function then(func) {
    if (Module["calledRun"]) {
        func(Module)
    } else {
        var old = Module["onRuntimeInitialized"];
        Module["onRuntimeInitialized"] = (function() {
            if (old) old();
            func(Module)
        })
    }
    return Module
}

When resolved in a Promise, it sees that the result has a .then() and considers it Thenable. It invokes .then() and returns it's result, expecting a Promise or another Thenable which will chain to an eventual result.

However, .then() actually returns Module which is Thenable with the same function. This endlessly recurses as the Promise continually wraps the Module and invokes .then(). This adds microtasks to the queue forever and so the browser never moves on to paint the UI.

I'm guessing this library was written in a time before the native Promise object, but it should either be changed to be compatible with Thenable semantics or have this function renamed to something else.

@buzz
Copy link
Owner

buzz commented Mar 19, 2020

Please have a look at the current release 0.0.3. Thanks

@buzz buzz closed this as completed Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants