He-Pin opened a new issue, #3106:
URL: https://github.com/apache/pekko/issues/3106

   ### Motivation
   
   `SourceRefImpl.source` and `SinkRefImpl.sink()` create a new 
`SourceRefStageImpl` / `SinkRefStageImpl` instance on every access. If a user 
accidentally materializes the returned Source/Sink more than once, multiple 
stage instances compete for the same partner handshake, causing the second 
materialization to fail with a confusing error.
   
   ### Current behavior
   
   `SourceRefImpl.scala:32-33`:
   ```scala
   def source: Source[T, NotUsed] =
     Source.fromGraph(new SourceRefStageImpl(OptionVal.Some(initialPartnerRef)))
   ```
   
   `SinkRefImpl.scala:33-34`:
   ```scala
   override def sink(): Sink[In, NotUsed] =
     Sink.fromGraph(new SinkRefStageImpl[In](OptionVal.Some(initialPartnerRef)))
   ```
   
   ### Proposed fix
   
   Option A: Cache with `lazy val` (requires changing from `case class` to 
`class`):
   ```scala
   private[stream] final class SourceRefImpl[T](initialPartnerRef: ActorRef) 
extends SourceRef[T] {
     lazy val source: Source[T, NotUsed] =
       Source.fromGraph(new 
SourceRefStageImpl(OptionVal.Some(initialPartnerRef))).mapMaterializedValue(_ 
=> NotUsed)
     // ... equals/hashCode/toString to preserve case class semantics
   }
   ```
   
   Option B: Add a clear error message when double-materialization is 
attempted, rather than a confusing handshake failure.
   
   ### References
   
   - Inspired by Akka.NET 
[#7905](https://github.com/akkadotnet/akka.net/pull/7905)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to