Skip to content

Commit

Permalink
Use LazyThreadSafteyMode.PUBLICATION instead of NONE (#433) (#440)
Browse files Browse the repository at this point in the history
LazyThreadSafetyMode.NONE literally means that accessing
the lazy value from multiple threads can cause an NPE.

Unfortunately, we assumed NONE would be implemented such
that a lazy value could be initialized by multiple
threads without throwing an NPE, with race conditions
only possibly occurring inside the initializer block.
However, that is actually the behavior is for
LazyThreadSafeteyMode.PUBLICATION.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

(cherry picked from commit eef183c)
  • Loading branch information
alancai98 authored Sep 13, 2021
1 parent d0c50ea commit 86ccd3e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lang/src/org/partiql/lang/eval/Bindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun CaseSensitivity.toBindingCase(): BindingCase = when(this) {
* Encapsulates the data necessary to perform a binding lookup.
*/
data class BindingName(val name: String, val bindingCase: BindingCase) {
val loweredName: String by lazy(LazyThreadSafetyMode.NONE) { name.toLowerCase() }
val loweredName: String by lazy(LazyThreadSafetyMode.PUBLICATION) { name.toLowerCase() }
/**
* Compares [name] to [otherName] using the rules specified by [bindingCase].
*/
Expand Down
4 changes: 2 additions & 2 deletions lang/src/org/partiql/lang/eval/ExprValueFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ private abstract class ScalarExprValue : BaseExprValue(), Scalar {

abstract fun ionValueFun(): IonValue

// LazyThreadSafetyMode.NONE is ok here because the worst that can happen is that [ionValueFun] is invoked
// LazyThreadSafetyMode.PUBLICATION is ok here because the worst that can happen is that [ionValueFun] is invoked
// from multiple threads. This should be ok because [IonSystem] is thread-safe.
override val ionValue by lazy(LazyThreadSafetyMode.NONE) { ionValueFun().seal() }
override val ionValue by lazy(LazyThreadSafetyMode.PUBLICATION) { ionValueFun().seal() }
}

/** A base class for the `true` boolean value, intended to be memoized. */
Expand Down
2 changes: 1 addition & 1 deletion testscript/src/org/partiql/testscript/compiler/Compiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Compiler(val ion: IonSystem) {
val dirPath = File(node.scriptLocation.inputName).parent
val file = File("$dirPath/${node.environmentRelativeFilePath}")

val lazyDatagram = lazy(LazyThreadSafetyMode.NONE) { ion.loader.load(file) }
val lazyDatagram = lazy(LazyThreadSafetyMode.PUBLICATION) { ion.loader.load(file) }

when {
!file.exists() -> {
Expand Down

0 comments on commit 86ccd3e

Please sign in to comment.