scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

actor call fails in android #1390

Closed scabug closed 13 years ago

scabug commented 15 years ago

i am doing some android work using scala and stumbled into the following exception while using actor:

09-27 12:19:51.046: ERROR/AndroidRuntime(354): Uncaught handler: thread Thread-13 exiting due to uncaught exception 09-27 12:19:51.056: ERROR/AndroidRuntime(354): java.lang.NullPointerException 09-27 12:19:51.056: ERROR/AndroidRuntime(354): at scala.actors.TimerThread$$$$anon$$1.run(TimerThread.scala:44) 09-27 12:19:51.056: ERROR/AndroidRuntime(354): at java.lang.Thread.run(Thread.java:935)

after looking at the source (TimerThread.scala), i discovered a problem: timerThread might not be initialized before it is used in timerTask, thus the NPE. ("timerThread.synchronized {" is where the NPE is thrown) the fix should be simple enough, replace 2 occurences where "timerThread" appears in "timerTask" by "java.lang.Thread.currentThread" should give a semantically equivalent code without the risk of NPE.

in TimerThread.scala:

private val timerTask = new Runnable { override def run = { try { while(true) { timerThread.synchronized { try { val sleepTime = dequeueLateAndGetSleepTime if (lateList.isEmpty) timerThread.wait(sleepTime) } catch { case t: Throwable => { throw t } } }

      // process guys waiting for signal and empty list
      for (wa <- lateList) {
        if (wa.valid) {
          wa.actor ! TIMEOUT
        }
      }
      lateList = Nil
    }
  } catch {
    case consumed: InterruptedException =>
      // allow thread to quit
  }
}

}

private var timerThread: Thread = { val t = new Thread(timerTask) t.start() t }

regards,

walter

scabug commented 15 years ago

Imported From: https://issues.scala-lang.org/browse/SI-1390?orig=1 Reporter: weihsiu

scabug commented 15 years ago

@phaller said: Fixed in r16171.