6

The following code works fine in Scala 2.8 but not in 2.9.0.1 (copy and paste in REPL). This will throw an exception in 2.9.0.1.

import scala.actors.Actor
import scala.actors.TIMEOUT

object A2 extends Actor {
  def act = {
    loop {
      react {
        case "hello" => 
          val s = sender 
          reactWithin(2000){
            case i:Int => s ! "hello"
            case TIMEOUT => s ! "TIMEOUT"
          }
        case _ => 
      }
    }
  }
}

object A1 extends Actor {
  def act = {
    loop {
      react {
        case m:String => println (A2 !? (1000, m))
        case _ =>
      }
    }
  }
}

A1.start
A2.start

A1 ! "hi"
A1 ! "hello"

If the exception is not thrown immediately, keep doing A1 ! "hi" or A1 ! "hello" a few times.

Is this a bug in Scala 2.9.0.1, or something wrong with the code?

[EDIT] Forgot to add the actual exception thrown.

scala> $line4.$read$$iw$$iw$A1$@6be03fce: caught java.lang.RuntimeException: unhandled timeout
java.lang.RuntimeException: unhandled timeout
    at scala.sys.package$.error(package.scala:27)
    at scala.actors.Actor$class.receiveWithin(Actor.scala:606)
    at $line4.$read$$iw$$iw$A1$.receiveWithin(<console>:10)
    at scala.actors.Channel.receiveWithin(Channel.scala:71)
    at scala.actors.ActorCanReply$class.$bang$qmark(ActorCanReply.scala:32)
    at $line3.$read$$iw$$iw$A2$.$bang$qmark(<console>:9)
    at $line4.$read$$iw$$iw$A1$$anonfun$act$1$$anonfun$apply$1.apply(<console>:15)
    at $line4.$read$$iw$$iw$A1$$anonfun$act$1$$anonfun$apply$1.apply(<console>:13)
    at scala.actors.ReactorTask.run(ReactorTask.scala:31)
    at scala.actors.Reactor$class.resumeReceiver(Reactor.scala:129)
    at $line4.$read$$iw$$iw$A1$.scala$actors$ReplyReactor$$super$resumeReceiver(<console>:10)
    at scala.actors.ReplyReactor$class.resumeReceiver(ReplyReactor.scala:68)
    at $line4.$read$$iw$$iw$A1$.resumeReceiver(<console>:10)
    at scala.actors.Actor$class.searchMailbox(Actor.scala:500)
    at $line4.$read$$iw$$iw$A1$.searchMailbox(<console>:10)
    at scala.actors.Reactor$$anonfun$startSearch$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(Reactor.scala:117)
    at scala.actors.Reactor$$anonfun$startSearch$1$$anonfun$apply$mcV$sp$1.apply(Reactor.scala:114)
    at scala.actors.Reactor$$anonfun$startSearch$1$$anonfun$apply$mcV$sp$1.apply(Reactor.scala:114)
    at scala.actors.ReactorTask.run(ReactorTask.scala:33)
    at scala.concurrent.forkjoin.ForkJoinPool$AdaptedRunnable.exec(ForkJoinPool.java:611)
    at scala.concurrent.forkjoin.ForkJoinTask.quietlyExec(ForkJoinTask.java:422)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.mainLoop(ForkJoinWorkerThread.java:340)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:325)

EDIT2: Standalone program without REPL

package scalaapplication5
import scala.actors.Actor._
import scala.actors.Actor
import scala.actors.TIMEOUT


object Main {
  A1.start
  A2.start
  def main(args: Array[String]): Unit = {
    while (true){
      A1 ! "hi"
      A1 ! "hello"
      Thread.sleep(1000)
    }
  }
}

object A2 extends Actor {
  def act = loop { 
    react {
      case "hello" =>
        val s = sender
        reactWithin(2000){
          case i:Int => s ! "hello"
          case TIMEOUT => s ! "TIMEOUT"
        }
      case _ =>
    }
  }
}

object A1 extends Actor {
  def act = {
    loop {
      react {
        case m:String => println (A2 !? (1000, m))
        case _ =>
      }
    }
  }
}
Jus12
  • 17,824
  • 28
  • 99
  • 157
  • 3
    "Is this a bug in Scala 2.9.0.1, or something wrong with the code?" - I can't tel the difference between Scala and Esperanto, but I know the answer to that question is almost invariably "it's your code" :-) – paxdiablo Jun 09 '11 at 04:01
  • Did you try with Scala 2.8, and see that no exception is thrown, but is thrown in 2.9.x – Jus12 Jun 09 '11 at 04:03
  • Has no one else been able to reproduce this behavior? Please try with Scala 2.9.0.1 (the latest builds). – Jus12 Jun 09 '11 at 11:27
  • 1
    I tried the code (Windows 7 64-bit, 64-bit JVM, Scala 2.9.0.1), and get the exception – Luigi Plinge Jun 09 '11 at 17:46
  • I cannot duplicate this on Windows 7 32-bit 32-bit JVM, scala 2.9.0.1. – Ian McLaird Jun 14 '11 at 01:58
  • @Ian I tried the following combinations and all fail: Scala 2.9.0.1 with WinXP, WinVista (both 32 bit), Windows 7 64 bit with 64 bit jvm. I have posted a file that can be run outside REPL. can you please try this. – Jus12 Jun 14 '11 at 14:20
  • 1
    I can confirm the issue using the updated code. I guess I just needed to let it run a few more times. I don't have a good solution for you, though. – Ian McLaird Jun 16 '11 at 19:13
  • @Ian The solution right now is to use an older version of Scala. 2.8.x works fine. – Jus12 Jun 24 '11 at 18:23
  • @Jus12 -- "Doctor, Doctor, it hurts when it do *this*!" "Well, stop doing that!" – Michael Lorton Jul 22 '11 at 16:28

1 Answers1

3

This does appear to have been a bug in the Actor framework. Looks like you're never supposed to get a RuntimeException. See the Scala Bug Tracker for details. It was fixed July 7th 2011, and is probably awaiting release.

Ross Judson
  • 1,132
  • 5
  • 11