Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($rootScope): prevent infinite $digest loop by also checking if asyncQueue is empty when decrementing ttl #5578

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ function $RootScopeProvider(){

// `break traverseScopesLoop;` takes us to here

if(dirty && !(ttl--)) {
if((dirty || asyncQueue.length) && !(ttl--)) {
clearPhase();
throw $rootScopeMinErr('infdig',
'{0} $digest() iterations reached. Aborting!\n' +
Expand Down
25 changes: 25 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,31 @@ describe('Scope', function() {
}));


it('should prevent infinite loop when creating and resolving a promise in a watched expression', function() {
module(function($rootScopeProvider) {
$rootScopeProvider.digestTtl(10);
});
inject(function($rootScope, $q) {
var d = $q.defer();

d.resolve('Hello, world.');
$rootScope.$watch(function () {
var $d2 = $q.defer();
$d2.resolve('Goodbye, cruel world.');
$d2.promise.then(function () { });
return d.promise;
}, function () { return 0; });

expect(function() {
$rootScope.$digest();
}).toThrowMinErr('$rootScope', 'infdig', '10 $digest() iterations reached. Aborting!\n'+
'Watchers fired in the last 5 iterations: []');

expect($rootScope.$$phase).toBeNull();
});
});


it('should not fire upon $watch registration on initial $digest', inject(function($rootScope) {
var log = '';
$rootScope.a = 1;
Expand Down