Skip to content
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

Better error message when JavaSerializer.currentSystem is not set #27418

Open
schernichkin opened this issue Jul 25, 2019 · 3 comments
Open

Better error message when JavaSerializer.currentSystem is not set #27418

schernichkin opened this issue Jul 25, 2019 · 3 comments
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Issues that the core team will likely not have time to work on

Comments

@schernichkin
Copy link

Version:

val akkaVersion = "2.6.0-M4"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-actor-typed" % akkaVersion)

Code to reproduce:

import java.io._

import akka.actor.typed._
import akka.actor.typed.scaladsl._
import akka.actor.typed.scaladsl.AskPattern._
import akka.util.Timeout

import scala.concurrent.Await
import scala.concurrent.duration._

object Main {
  def main(args: Array[String]): Unit = {
    val system = ActorSystem(SpawnProtocol.behavior, "def")
    implicit val timeout: Timeout = Timeout(3.seconds)
    implicit val scheduler: Scheduler = system .scheduler
    val ref = system ? SpawnProtocol.Spawn(behavior = beh, name = "")
    val a = Await.result(ref, 3.seconds)
    val bs = new ByteArrayOutputStream()
    val oos = new ObjectOutputStream(bs)
    oos.writeObject(a)
  }

  def beh: Behavior[Int] = Behaviors.receiveMessage { msg =>
    println(msg)
    Behaviors.same
  }
}

Expected result:
Actor ref should be serializable.

Actual result:

Exception in thread "main" java.lang.NullPointerException: system must not be null!
	at java.util.Objects.requireNonNull(Objects.java:228)
	at akka.actor.ExtensionId.apply(Extension.scala:77)
	at akka.actor.ExtensionId.apply$(Extension.scala:76)
	at akka.actor.typed.scaladsl.adapter.AdapterExtension$.apply(AdapterExtension.scala:23)
	at akka.actor.typed.scaladsl.adapter.package$UntypedActorSystemOps$.toTyped$extension(package.scala:70)
	at akka.actor.typed.SerializedActorRef$.toAddress(ActorRef.scala:83)
	at akka.actor.typed.SerializedActorRef.<init>(ActorRef.scala:98)
	at akka.actor.typed.SerializedActorRef$.apply(ActorRef.scala:77)
	at akka.actor.typed.internal.adapter.ActorRefAdapter.writeReplace(ActorRefAdapter.scala:42)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at java.io.ObjectStreamClass.invokeWriteReplace(ObjectStreamClass.java:1218)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1136)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at Main$.main(Main.scala:20)
	at Main.main(Main.scala)
@chbatey
Copy link
Member

chbatey commented Jul 26, 2019

You can use ActorRefResolver(system).toSerializationFormat to serialize an ActorRef

To use JavaSerialization outside of an SerializationExtension then you need to wrap in

   JavaSerializer.currentSystem.withValue(system) {
      ...code...
   }

We should improve the error message here if currentSystem is not set.

@schernichkin
Copy link
Author

Thanks. Should I close this issue?

@chbatey
Copy link
Member

chbatey commented Jul 29, 2019

We can use this issue to improve the error message, i'll re-name it

@chbatey chbatey changed the title Typed actor-ref is not serializable Better error message when JavaSerializer.currentSystem is not set Jul 29, 2019
@chbatey chbatey added 1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Issues that the core team will likely not have time to work on labels Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Issues that the core team will likely not have time to work on
Projects
None yet
Development

No branches or pull requests

2 participants