Skip to content

Do not merge throwable to itself #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/src/main/scala/scala/collection/parallel/Tasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ trait Task[R, +Tp] {
mergeThrowables(that)
}

private[parallel] def mergeThrowables(that: Task[_, _]): Unit = {
if (this.throwable != null && that.throwable != null)
this.throwable.addSuppressed(that.throwable)
else if (this.throwable == null && that.throwable != null)
private[parallel] def mergeThrowables(that: Task[_, _]): Unit =
if (this.throwable != null) {
if (that.throwable != null && (this.throwable ne that.throwable))
this.throwable.addSuppressed(that.throwable)
} else if (that.throwable != null)
this.throwable = that.throwable
}

// override in concrete task implementations to signal abort to other tasks
private[parallel] def signalAbort(): Unit = {}
Expand Down
46 changes: 45 additions & 1 deletion junit/src/test/scala/scala/collection/parallel/TaskTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ class TaskTest {
t
}
}
def mkPool(name: String) = new ForkJoinPool(1, mkFactory(name), null, false)
def mkPool(name: String) = {
val parallelism = 1
val handler: Thread.UncaughtExceptionHandler = null
val asyncMode = false
new ForkJoinPool(parallelism, mkFactory(name), handler, asyncMode)
}
Comment on lines +32 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because we can't use named arguments for Java-defined methods? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe scalac still complains, although I didn't try again here. Fool me once, as the saying goes.

I actually did this because my first version of the test was this plus throwing on run, but that fails outright and hangs!


val one = List(1).par
val two = List(2).par
Expand All @@ -48,4 +53,43 @@ class TaskTest {
val r = c.filter(_ != 0).map(_ + 1)
assertSame(myTs, r.tasksupport)
}

// was: Wrong exception: expected scala.collection.parallel.TaskTest$SpecialControl$1 but was java.lang.IllegalArgumentException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the fix, the assertThrows fails with this message.

@Test
def `t10276 exception does not suppress itself when merging`: Unit = {
import TestSupport._
import scala.util.control.ControlThrowable
class SpecialControl extends ControlThrowable("special")
val SpecialExcept = new SpecialControl

class Special {
def add(other: Special): Special = throw SpecialExcept
}

def listed(n: Int) = List.fill(n)(new Special)
val specials = listed(1000).par
assertThrows[SpecialControl](_ eq SpecialExcept)(specials.reduce(_ add _))
}
}
object TestSupport {
import scala.reflect.ClassTag
import scala.util.control.{ControlThrowable, NonFatal}
private val Unthrown = new ControlThrowable {}

def assertThrows[T <: Throwable: ClassTag](checker: T => Boolean)(body: => Any): Unit =
try {
body
throw Unthrown
} catch {
case Unthrown => fail("Expression did not throw!")
case e: T if checker(e) => ()
case failed: T =>
val ae = new AssertionError(s"Exception failed check: $failed")
ae.addSuppressed(failed)
throw ae
case NonFatal(other) =>
val ae = new AssertionError(s"Wrong exception: expected ${implicitly[ClassTag[T]]} but was ${other.getClass.getName}")
ae.addSuppressed(other)
throw ae
}
}