Skip to content

Commit 9645b18

Browse files
committed
fix: auto validation were sometimes executed after being disabled
The race condition was detected in IncrementalMathTest.sideEffects, which caused it to fail sometimes.
1 parent 75e20f1 commit 9645b18

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

incremental/src/commonMain/kotlin/org/modelix/incremental/IncrementalEngine.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class IncrementalEngine(val maxSize: Int = 100_000) : IIncrementalEngine, IState
110110
autoValidator = engineScope.launch {
111111
while (!disposed) {
112112
val key = graph.autoValidationChannel.receive()
113-
update(key)
113+
runAutoValidation(key)
114114
}
115115
}
116116
}
@@ -231,6 +231,20 @@ class IncrementalEngine(val maxSize: Int = 100_000) : IIncrementalEngine, IState
231231
node.setAutoValidate(value)
232232
}
233233

234+
@Synchronized
235+
private fun isAutoValidate(key: IStateVariableReference<*>): Boolean {
236+
val node = (graph.getNode(key) ?: return false) as DependencyGraph.ComputationNode<*>
237+
// TODO there could be multiple instances for the same key
238+
return node.isAutoValidate()
239+
}
240+
241+
@Synchronized
242+
private fun runAutoValidation(key: InternalStateVariableReference<*, *>) {
243+
if (disposed) return
244+
if (!isAutoValidate(key)) return
245+
update(key)
246+
}
247+
234248
private inner class ObservedOutput<E>(val key: IStateVariableReference<E>) : IActiveOutput<E> {
235249
override fun deactivate() {
236250
setAutoValidate(key, false)

0 commit comments

Comments
 (0)