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

Circular dependency if exception during resource init #4361

Closed
cyrilchampier opened this issue Oct 10, 2013 · 4 comments
Closed

Circular dependency if exception during resource init #4361

cyrilchampier opened this issue Oct 10, 2013 · 4 comments

Comments

@cyrilchampier
Copy link

class MyResource extends $resource("/test", { arg: throw "holy shit!" })

the first time the resource is needed, the exception is throw.
the second time "Error: [$injector:cdep] Circular dependency found: "

It seems that the resource is in cache as an empty object and is considered as INSTANTIATING:
(cache[serviceName] === INSTANTIATING) returns true

@petebacondarwin
Copy link
Member

What happens to the first thrown error? Does it swallowed somewhere?

@cyrilchampier
Copy link
Author

No, it interrupts the script and log to the console.

@metamatt
Copy link
Contributor

I think that all broken dependencies leave the injector cache in this state, such that all subsequent requests for the same service (circular or not, even completely independent) will receive the "circular dependency found" error.

Here's a jsfiddle that demonstrates the problem

@metamatt
Copy link
Contributor

And here's a patch I propose to fix this (this patch happens to be against 1.2.5)

diff --git a/browser_modules/angular-1.2.5/angular.js b/browser_modules/angular-1.2.5/angular.js
index e2c062b..ddfb27e 100644
--- a/browser_modules/angular-1.2.5/angular.js
+++ b/browser_modules/angular-1.2.5/angular.js
@@ -3644,6 +3644,11 @@ function createInjector(modulesToLoad) {
           path.unshift(serviceName);
           cache[serviceName] = INSTANTIATING;
           return cache[serviceName] = factory(serviceName);
+        } catch (err) {
+          if (cache[serviceName] === INSTANTIATING) {
+            delete cache[serviceName];
+          }
+          throw err;
         } finally {
           path.shift();
         }

Will test more and send a PR.

metamatt added a commit to metamatt/angular.js that referenced this issue Dec 31, 2013
getService flags services as INSTANTIATING while it calls their
provider factory, in order to detect circular dependencies. If
the service is instantiated correctly, the INSTANTIATING flag is
overwritten with the actual service. However, if the service is
not instantiated correctly, the INSTANTIATING flag should still
be removed, or all further requests for this service will be
mis-detected as a circular dependency.

Closes angular#4361.
@ghost ghost assigned IgorMinar Dec 31, 2013
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
getService flags services as INSTANTIATING while it calls their
provider factory, in order to detect circular dependencies. If
the service is instantiated correctly, the INSTANTIATING flag is
overwritten with the actual service. However, if the service is
not instantiated correctly, the INSTANTIATING flag should still
be removed, or all further requests for this service will be
mis-detected as a circular dependency.

Closes angular#4361
Closes angular#5577
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
getService flags services as INSTANTIATING while it calls their
provider factory, in order to detect circular dependencies. If
the service is instantiated correctly, the INSTANTIATING flag is
overwritten with the actual service. However, if the service is
not instantiated correctly, the INSTANTIATING flag should still
be removed, or all further requests for this service will be
mis-detected as a circular dependency.

Closes angular#4361
Closes angular#5577
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants