From 0a226eb3ea0123ac55dd7fc20b708c73e24619c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Andr=C3=A9n?= Date: Fri, 9 Apr 2021 14:01:49 +0200 Subject: [PATCH] Extra catch-ignore for source attributes in case not possible #30138 --- .../stream/SourceLocationAttributeSpec.scala | 21 +++++++++++++++++++ .../main/scala/akka/stream/Attributes.scala | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 akka-stream-tests/src/test/scala/akka/stream/SourceLocationAttributeSpec.scala diff --git a/akka-stream-tests/src/test/scala/akka/stream/SourceLocationAttributeSpec.scala b/akka-stream-tests/src/test/scala/akka/stream/SourceLocationAttributeSpec.scala new file mode 100644 index 0000000000..88d2b16d04 --- /dev/null +++ b/akka-stream-tests/src/test/scala/akka/stream/SourceLocationAttributeSpec.scala @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2009-2021 Lightbend Inc. + */ + +package akka.stream + +import akka.stream.scaladsl.Flow +import akka.stream.testkit.StreamSpec + +class SourceLocationAttributeSpec extends StreamSpec { + + "The SourceLocation attribute" must { + "not throw NPE" in { + // #30138 + val f1 = Flow[Int].fold(0)(_ + _) + val f2 = Flow[Int].fold(0)(_ + _) + f1.join(f2).toString + } + } + +} diff --git a/akka-stream/src/main/scala/akka/stream/Attributes.scala b/akka-stream/src/main/scala/akka/stream/Attributes.scala index 176b01275a..9f09cf3f10 100644 --- a/akka-stream/src/main/scala/akka/stream/Attributes.scala +++ b/akka-stream/src/main/scala/akka/stream/Attributes.scala @@ -21,6 +21,8 @@ import akka.util.{ ByteString, OptionVal } import akka.util.JavaDurationConverters._ import akka.util.LineNumbers +import scala.util.control.NonFatal + /** * Holds attributes which can be used to alter [[akka.stream.scaladsl.Flow]] / [[akka.stream.javadsl.Flow]] * or [[akka.stream.scaladsl.GraphDSL]] / [[akka.stream.javadsl.GraphDSL]] materialization. @@ -308,7 +310,7 @@ object Attributes { * for debugging. Included in the default toString of GraphStageLogic if present */ final class SourceLocation(lambda: AnyRef) extends Attribute { - lazy val locationName: String = { + lazy val locationName: String = try { val locationName = LineNumbers(lambda) match { case LineNumbers.NoSourceInfo => "unknown" case LineNumbers.UnknownSourceFormat(_) => "unknown" @@ -317,6 +319,8 @@ object Attributes { s"$filename:$from" } s"${lambda.getClass.getPackage.getName}-$locationName" + } catch { + case NonFatal(_) => "unknown" // location is not critical so give up without failing } override def toString: String = locationName