Skip to content

fix(router): prevent error handling when injector is destroyed#59457

Closed
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/router-prevent-injector-on-destroyed-transitions
Closed

fix(router): prevent error handling when injector is destroyed#59457
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/router-prevent-injector-on-destroyed-transitions

Conversation

@arturovt
Copy link
Copy Markdown
Contributor

@arturovt arturovt commented Jan 9, 2025

In this commit, we prevent error handling when the root injector is already destroyed. This may happen when the observable completes before emitting a value, which would trigger a catchError block that attempts to call runInInjectionContext on a destroyed injector.

@ngbot ngbot bot added this to the Backlog milestone Jan 9, 2025
@arturovt arturovt force-pushed the fix/router-prevent-injector-on-destroyed-transitions branch from 444f4bc to e3d918e Compare January 10, 2025 16:06
@arturovt arturovt marked this pull request as ready for review January 10, 2025 16:07
@pullapprove pullapprove bot requested a review from AndrewKushnir January 10, 2025 16:07
@AndrewKushnir AndrewKushnir requested review from atscott and removed request for AndrewKushnir January 10, 2025 17:23
@pullapprove pullapprove bot requested review from AndrewKushnir and removed request for atscott January 10, 2025 17:24
@AndrewKushnir AndrewKushnir requested review from atscott and removed request for AndrewKushnir January 11, 2025 00:28
@atscott
Copy link
Copy Markdown
Contributor

atscott commented Jan 16, 2025

If the injector is already destroyed, that error is still caught by the catch block so I'm not sure why this would really be necessary. Presumably everything's shutting down anyways, so what issues does this cause?

Edit: This change also isn't quite equivalent to the previous state because you're missing the NavigationError event as well as resolving or rejecting the navigation promise at the very least.

@arturovt
Copy link
Copy Markdown
Contributor Author

The issue is that runInInjectionContext throws an error that injector has already been destroyed and that's getting logged by our services (which practically shouldn't).

@arturovt
Copy link
Copy Markdown
Contributor Author

If the application is already destroyed, the catch block should not execute anything in practice (because other stuff has already been released and destroyed).

@atscott
Copy link
Copy Markdown
Contributor

atscott commented Jan 16, 2025

If the application is already destroyed, the catch block should not execute anything in practice (because other stuff has already been released and destroyed).

Okay, yea that's fair. I would reframe the justification for this. It's not exactly that we don't want to do runInInjectionContext but really that we don't want to do anything at all. If we're destroyed, we shouldn't do more work, regardless of what that is (and the error really isn't an error, it's just comes that way because of the way rxjs is working here).

That said, I'm still not sure whether we should omit the event and the promise completion and only return EMPTY.

@arturovt
Copy link
Copy Markdown
Contributor Author

arturovt commented Jan 16, 2025

Alright, maybe we should wrap the code with if !destroyed, but still resolve/reject the promise? (It would still return empty at the end).

Btw, the _events is unsubscribe()d on dispose (see this._events.unsubscribe()), so there's no reason to emit events as there will be no observers.

@atscott
Copy link
Copy Markdown
Contributor

atscott commented Jan 16, 2025

Alright, maybe we should wrap the code with if !destroyed, but still resolve/reject the promise? (It would still return empty at the end).

Yea, let's do overallTransitionState.resolve(false); return EMPTY;.

Btw, the _events is unsubscribe()d on dispose (see this._events.unsubscribe()), so there's no reason to emit events as there will be no observers.

👍

@arturovt
Copy link
Copy Markdown
Contributor Author

👍 will update the code tomorrow.

In this commit, we prevent error handling when the root injector is already destroyed. This may happen when the observable completes before emitting a value, which would trigger a `catchError` block that attempts to call `runInInjectionContext` on a destroyed injector.
@arturovt arturovt force-pushed the fix/router-prevent-injector-on-destroyed-transitions branch from e3d918e to ddfa390 Compare January 17, 2025 07:41
@arturovt
Copy link
Copy Markdown
Contributor Author

@atscott updated the code to include resolve(false).

@atscott atscott added target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker labels Jan 21, 2025
@AndrewKushnir AndrewKushnir added action: global presubmit The PR is in need of a google3 global presubmit requires: TGP This PR requires a passing TGP before merging is allowed labels Jan 21, 2025
@AndrewKushnir AndrewKushnir removed the action: merge The PR is ready for merge by the caretaker label Jan 22, 2025
@AndrewKushnir
Copy link
Copy Markdown
Contributor

FYI, removing the "merge" label for now, since we'd need to run a global presubmit for this change. We'll re-add the "merge" label back if the presubmit is successful.

@atscott
Copy link
Copy Markdown
Contributor

atscott commented Jan 23, 2025

TGP is green.

@atscott atscott added action: merge The PR is ready for merge by the caretaker and removed action: global presubmit The PR is in need of a google3 global presubmit labels Jan 23, 2025
@alxhub
Copy link
Copy Markdown
Member

alxhub commented Jan 23, 2025

This PR was merged into the repository by commit c7b6e11.

The changes were merged into the following branches: main, 19.1.x

alxhub pushed a commit that referenced this pull request Jan 23, 2025
In this commit, we prevent error handling when the root injector is already destroyed. This may happen when the observable completes before emitting a value, which would trigger a `catchError` block that attempts to call `runInInjectionContext` on a destroyed injector.

PR Close #59457
@alxhub alxhub closed this in c7b6e11 Jan 23, 2025
@arturovt arturovt deleted the fix/router-prevent-injector-on-destroyed-transitions branch January 24, 2025 09:23
PrajaktaB27 pushed a commit to PrajaktaB27/angular that referenced this pull request Feb 7, 2025
…ar#59457)

In this commit, we prevent error handling when the root injector is already destroyed. This may happen when the observable completes before emitting a value, which would trigger a `catchError` block that attempts to call `runInInjectionContext` on a destroyed injector.

PR Close angular#59457
@angular-automatic-lock-bot
Copy link
Copy Markdown

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 Feb 24, 2025
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: router PullApprove: disable requires: TGP This PR requires a passing TGP before merging is allowed target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants