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(core): error if detectChanges is called at the wrong time under specific circumstances #40206

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Dec 19, 2020

Internally we store lifecycle hooks in the format [index, hook, index, hook] and when iterating over them, we check one place ahead to figure out whether we've hit found a hook or an index. The problem is that the loop is set up to iterate up to hooks.length which means that we may go out of bounds on the last iteration, depending on where we started. This appears to happen under a specific set of circumstances where a directive calls detectChanges from an input setter while it has ngOnChanges and ngAfterViewInit hooks.

These changes resolve the issue by only iterating up to length - 1 which guarantees that we can always look one place ahead.

This appears to have regressed some time in version 10.

Fixes #38611.

@google-cla google-cla bot added the cla: yes label Dec 19, 2020
…pecific circumstances

Internally we store lifecycle hooks in the format `[index, hook, index, hook]` and when
iterating over them, we check one place ahead to figure out whether we've hit found
a hook or an index. The problem is that the loop is set up to iterate up to `hooks.length`
which means that we may go out of bounds on the last iteration, depending on where
we started. This appears to happen under a specific set of circumstances where a
directive calls `detectChanges` from an input setter while it has `ngOnChanges` and
`ngAfterViewInit` hooks.

These changes resolve the issue by only iterating up to `length - 1` which guarantees that
we can always look one place ahead.

This appears to have regressed some time in version 10.

Fixes angular#38611.
@crisbeto crisbeto force-pushed the 38611-hook-detect-changes-error-again branch from d5b60d3 to 2529e96 Compare December 19, 2020 13:02
@crisbeto crisbeto marked this pull request as ready for review December 19, 2020 13:20
@pullapprove pullapprove bot requested a review from atscott December 19, 2020 13:20
@pullapprove pullapprove bot added the area: core Issues related to the framework runtime label Dec 19, 2020
@ngbot ngbot bot added this to the Backlog milestone Dec 19, 2020
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release type: bug/fix labels Dec 19, 2020
@crisbeto
Copy link
Member Author

crisbeto commented Dec 19, 2020

Note for reviewer: the ci/circleci: test appears to be a known failure that is happening in other PRs as well.

@atscott
Copy link
Contributor

atscott commented Dec 21, 2020

presubmit

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Dec 21, 2020
@ngbot
Copy link

ngbot bot commented Dec 21, 2020

I see that you just added the action: merge label, but the following checks are still failing:
    failure status "ci/circleci: test" is failing
    pending status "google3" is pending
    pending missing required status "ci/circleci: publish_snapshot"

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

josephperrott pushed a commit that referenced this pull request Dec 22, 2020
…pecific circumstances (#40206)

Internally we store lifecycle hooks in the format `[index, hook, index, hook]` and when
iterating over them, we check one place ahead to figure out whether we've hit found
a hook or an index. The problem is that the loop is set up to iterate up to `hooks.length`
which means that we may go out of bounds on the last iteration, depending on where
we started. This appears to happen under a specific set of circumstances where a
directive calls `detectChanges` from an input setter while it has `ngOnChanges` and
`ngAfterViewInit` hooks.

These changes resolve the issue by only iterating up to `length - 1` which guarantees that
we can always look one place ahead.

This appears to have regressed some time in version 10.

Fixes #38611.

PR Close #40206
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jan 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cla: yes target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A runtime error is thrown when calling detectChanges inside the transform method of a pipe.
2 participants