Skip to content

NullPointerException when using a nested await() with an AnyVal class #86

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

Closed
liff opened this issue Jul 25, 2014 · 3 comments
Closed

NullPointerException when using a nested await() with an AnyVal class #86

liff opened this issue Jul 25, 2014 · 3 comments
Assignees
Labels
Milestone

Comments

@liff
Copy link

liff commented Jul 25, 2014

Running this code:

import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration.Inf
import scala.concurrent.ExecutionContext.Implicits.global
import scala.async.Async.{async, await}


object Main {
  def main(args: Array[String]): Unit = {
    val f = async {
      val a = Future.successful(new A(BigInt(42)))
      await(await(a).g)
    }
    Await.result(f, Inf)
  }
}

class A(val b: BigInt) extends AnyVal {
  def g = Future.successful(b + 1)
}

results in an NPE:

java.lang.NullPointerException
    at Main$stateMachine$macro$1$1.resume(Main.scala:9)
    at Main$stateMachine$macro$1$1.apply(Main.scala:9)
    at Main$stateMachine$macro$1$1.apply(Main.scala:9)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.pollAndExecAll(ForkJoinPool.java:1253)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1346)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

If extends AnyVal is removed from A, it runs successfully. It also works if the enclosed type is an Int instead of a BigInt.

Scala version: 2.11.2
scala-async version: 0.9.2

@ewiner
Copy link

ewiner commented Sep 25, 2014

+1. Does anyone know of a workaround that doesn't involve removing AnyVal?

@ewiner
Copy link

ewiner commented Sep 25, 2014

Sorry, I might be having a slightly different problem, as I'm not using a nested await(). But I get an identical stack trace from this code, on Scala 2.11.2 and async 0.9.2.

import scala.async.Async._
import scala.concurrent.{ExecutionContext, Future, Await}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global

object Break {

  def main(args: Array[String]) {

    val fut = async {
      Option(ValueClass("value!")) match {
        case Some(valueHolder) =>
          await(doAThing(valueHolder))
        case None =>
          None
      }
    }

    println(Await.result(fut, Duration.Inf))
  }

  case class ValueClass(two: String) extends AnyVal

  private def doAThing(id: ValueClass)(implicit context: ExecutionContext) = Future(None)
}

ewiner added a commit to ewiner/async that referenced this issue Sep 25, 2014
@retronym retronym added the bug label Sep 26, 2014
@retronym retronym added this to the 0.9.3 milestone Sep 26, 2014
@retronym retronym self-assigned this Sep 26, 2014
ewiner added a commit to ewiner/async that referenced this issue Sep 26, 2014
@retronym
Copy link
Member

See #91

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants