Skip to content

Commit

Permalink
Avoid race condition in Reporter errorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Aneta Porebska committed May 21, 2024
1 parent 27b7f16 commit c6380f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ trait ScexCompiler extends LoggingUtils {

def displayPrompt(): Unit = {}

override def reset(): Unit = {
super.reset()
def clearErrors(): Unit = {
errorsBuilder.clear()
}
}
Expand Down Expand Up @@ -221,9 +220,11 @@ trait ScexCompiler extends LoggingUtils {

protected final def withGlobal[T](code: ScexGlobal => T): T = underLock {
reporter.reset()
reporter.clearErrors()
val global = this.global
val result = try code(global) finally {
reporter.reset()
reporter.clearErrors()
}
result
}
Expand All @@ -241,6 +242,7 @@ trait ScexCompiler extends LoggingUtils {
val classfileDirectory = classLoader.classfileDirectory

reporter.reset()
reporter.clearErrors()

logger.debug(s"Compiling source file ${sourceFile.path} to $classfileDirectory:\n${new String(sourceFile.content)}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ trait ScexPresentationCompiler extends ScexCompiler { compiler =>

protected final def withIGlobal[T](code: IGlobal => T) = underLock {
reporter.reset()
reporter.clearErrors()
val global = compiler.global
val result = try code(global) finally {
reporter.reset()
reporter.clearErrors()
}
result
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.avsystem.scex.compiler

import com.avsystem.scex.compiler.presentation.TypeCompletionPrefixTest.Root
import com.avsystem.scex.japi.{DefaultJavaScexCompiler, JavaScexCompiler}
import com.avsystem.scex.util.SimpleContext
import org.scalatest.funsuite.AnyFunSuite

final class MultipleEvaluationsTest extends AnyFunSuite with CompilationTest {
override protected def createCompiler: JavaScexCompiler = {
val settings = new ScexSettings
settings.classfileDirectory.value = "testClassfileCache"

// for some reason enabling this setting fixes validation
settings.noGetterAdapters.value = false
new DefaultJavaScexCompiler(settings)
}

private val completer = compiler.getCompleter[SimpleContext[Root], Any](
createProfile(Nil),
template = false,
header = "",
)

test("Multiple evaluations of invalid expressions should always fail") {
(0 until 1000)
.map(i => s"invalidExpression $i") //added to avoid hitting the cache
.foreach(expr => if (completer.getErrors(expr).isEmpty) fail(s"Expression $expr should not compile"))
}
}

0 comments on commit c6380f3

Please sign in to comment.