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

fix: AsyncGeneratorComplete cannot call user code #3522

Conversation

aapoalas
Copy link

@aapoalas aapoalas commented Jan 11, 2025

It shouldn't be possible for a Call to both be asserted to never throw an error and call user code (except if the user function is known to be an async function I guess, but I don't think that's the case here?). I believe in this case the user-code effect is the wrong one.

@bakkot
Copy link
Contributor

bakkot commented Jan 11, 2025

Unfortunately, it can! Consider

Object.defineProperty(Object.prototype, 'then', { get(){ console.log('user code'); } });

let gen = (async function* gen() {})();
gen.return();

Tracing through the execution, this will call AsyncGeneratorCompleteStep, and step 7.d will call promiseCapability.[[Resolve]] passing { value: undefined, done: true }, where promiseCapability is the promise capability created in %AsyncGeneratorPrototype%.return step 2. That means its [[Resolve]] is the function defined in Promise resolve function, and in step 9 of that algorithm you can see it does does a Get(resolution, "then"), where resolution is the { value: undefined, done: true } object created earlier. And since that object inherits from Object.prototype, doing a Get on its then will (in this case) trigger user code. However, it will wrap any errors from that user code in a rejected promise, so the Call itself will still complete normally.

The assumption that a Call which never throws must also not run user code usually holds, but not always, which is why we have an explicit marker for cases like this.

@bakkot bakkot closed this Jan 11, 2025
@aapoalas
Copy link
Author

Ah, my bad then. Thank you for the explanation; trying to wrap one's mind around the async stuff is really hard :D

@aapoalas aapoalas deleted the fix/async-generator-complete-step-no-user-code branch January 11, 2025 16:18
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

Successfully merging this pull request may close these issues.

2 participants