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

plugin-transform-regenerator can produce infinite loop using async/await and nested try/catch/finally #9019

Open
mgroenhoff opened this Issue Nov 13, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@mgroenhoff
Contributor

mgroenhoff commented Nov 13, 2018

Bug Report

Current Behavior
The state machine produced by @babel/plugin-transform-regenerator is incorrect and can produce an infinite loop using nested try/catch/finally statements inside a loop together with async/await as shown in the example below.

If I run the code from the repl below with node it loops two times and then exits the loop. If I run the code produced by babel it loops 10 times.

Input Code

let call = 0;
async function callback() {
    ++call;
    if (call < 2) {
        throw new Error("err");
    }
    call = 0;
}

async function main() {
    let retry;
    do {
      	retry = false;
        let result;
        try {
            try {
                result = await callback();
                console.log("success");
            } catch (error) {
                console.log("error 1");
                throw error;
            }/* finally {
                // If I enable this finally block and `await callback()` does not throw ("success"), it jumps to the outer catch block ("error 2") below
                console.log("finally 1");
            }*/
        } catch (error) {
            console.log("error 2");
            retry = true;
        } finally {
            console.log("finally 2");
        }
    } while (retry);
}

main();

See repl

Expected behavior/code
If I uncomment the inner finally everything works as expected.
See repl

@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Nov 13, 2018

Hey @mgroenhoff! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@ulrichb

This comment has been minimized.

ulrichb commented Nov 23, 2018

@mgroenhoff Is the this._invoke() call in defineIteratorMethods() involved in this endless loop?

I'm also seeing an endless loop here (but only in the MS Edge (v18) browser, not in Chrome/Fx)...

@mgroenhoff

This comment has been minimized.

Contributor

mgroenhoff commented Nov 27, 2018

@ulrichb I'm sorry but I don't see any references to this._invoke() or defineIteratorMethods() in my code or the generated code. Can you try to explain why you brought those up?

@ulrichb

This comment has been minimized.

ulrichb commented Nov 27, 2018

@mgroenhoff Sry. Forget my comment. I had an endless iterator in my user code (just affecting MS Edge).

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