Skip to content

Commit 6081a6c

Browse files
committed
[core] Properly detect and report dependency cycle, prevent StackOverflowError
1 parent ee4fe16 commit 6081a6c

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2017-Present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.koin.core.error
17+
18+
class DependencyCycleException(msg: String) : Exception(msg)

projects/core/koin-core/src/commonMain/kotlin/org/koin/core/instance/ScopedInstanceFactory.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.koin.core.instance
1717

1818
import org.koin.core.definition.BeanDefinition
19+
import org.koin.core.error.DependencyCycleException
1920
import org.koin.core.scope.Scope
2021
import org.koin.core.scope.ScopeID
2122
import org.koin.mp.KoinPlatformTools
@@ -27,7 +28,8 @@ import org.koin.mp.KoinPlatformTools
2728
class ScopedInstanceFactory<T>(beanDefinition: BeanDefinition<T>) :
2829
InstanceFactory<T>(beanDefinition) {
2930

30-
private var values = hashMapOf<ScopeID, T>()
31+
private var instanceCreationInProgressMap = KoinPlatformTools.safeHashMap<ScopeID, Unit>()
32+
private var values = KoinPlatformTools.safeHashMap<ScopeID, T>()
3133

3234
override fun isCreated(context: InstanceContext?): Boolean = (values[context?.scope?.id] != null)
3335

@@ -38,12 +40,13 @@ class ScopedInstanceFactory<T>(beanDefinition: BeanDefinition<T>) :
3840
}
3941
}
4042

41-
override fun create(context: InstanceContext): T {
42-
return if (values[context.scope.id] == null) {
43-
super.create(context)
44-
} else {
45-
values[context.scope.id] ?: error("Scoped instance not found for ${context.scope.id} in $beanDefinition")
43+
override fun create(context: InstanceContext): T = values.getOrPut(context.scope.id) {
44+
if (instanceCreationInProgressMap.put(context.scope.id, Unit) != null) {
45+
throw DependencyCycleException("Instance creation invoked twice, most likely it's a dependency cycle")
4646
}
47+
val instance = super.create(context)
48+
instanceCreationInProgressMap.remove(context.scope.id)
49+
instance
4750
}
4851

4952
override fun get(context: InstanceContext): T {

projects/core/koin-core/src/commonMain/kotlin/org/koin/core/instance/SingleInstanceFactory.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.koin.core.instance
1717

18+
import co.touchlab.stately.concurrency.AtomicBoolean
1819
import org.koin.core.definition.BeanDefinition
20+
import org.koin.core.error.DependencyCycleException
1921
import org.koin.core.scope.Scope
2022
import org.koin.mp.KoinPlatformTools
2123

@@ -26,6 +28,8 @@ import org.koin.mp.KoinPlatformTools
2628
class SingleInstanceFactory<T>(beanDefinition: BeanDefinition<T>) :
2729
InstanceFactory<T>(beanDefinition) {
2830

31+
private val instanceCreationInProgress = AtomicBoolean(false)
32+
2933
private var value: T? = null
3034

3135
private fun getValue(): T = value ?: error("Single instance created couldn't return value")
@@ -41,12 +45,13 @@ class SingleInstanceFactory<T>(beanDefinition: BeanDefinition<T>) :
4145
drop()
4246
}
4347

44-
override fun create(context: InstanceContext): T {
45-
return if (value == null) {
46-
super.create(context)
47-
} else {
48-
getValue()
48+
override fun create(context: InstanceContext): T = value ?: run {
49+
if (!instanceCreationInProgress.compareAndSet(false, true)) {
50+
throw DependencyCycleException("Instance creation invoked twice, most likely it's a dependency cycle")
4951
}
52+
val instance = super.create(context)
53+
instanceCreationInProgress.value = false
54+
instance
5055
}
5156

5257
override fun get(context: InstanceContext): T {

0 commit comments

Comments
 (0)