pjfanning commented on PR #493:
URL: 
https://github.com/apache/pekko-persistence-cassandra/pull/493#issuecomment-5790804519

   Thanks @nvollmar — I prefer that shape to the boolean too, but as written it 
gives up the thread safety that `lazy val` provides.
   
   `lazy val` carries a compiler-generated once-only guarantee; `getOrElse` 
over a plain `var` does not. Two threads calling `deleteAllTaggedEvents` 
concurrently can each construct a `Reconciliation`, each starting a tag writers 
actor, and only the last reference written to the field is reachable from 
`close()` — so the leak this PR is fixing would come back under exactly the 
usage the class documents:
   
   > It's possible to parallelize the deletes by running several cleanup 
operations at the same time operating on different sets of `persistenceIds`.
   
   The field is also read from inside the `flatMap` callback in 
`deleteAllTaggedEvents`, so it is touched from execution-context threads, and 
an unsynchronised non-`@volatile` `var` has a visibility hole on top of the 
construction race.
   
   How about keeping `lazy val` but storing the reference rather than a 
boolean? That removes the flag you objected to, gives `close()` the `Option` 
shape you suggested, and makes it idempotent by construction:
   
   ```scala
   private val startedReconciliation = new 
AtomicReference[Option[Reconciliation]](None)
   
   private lazy val tagViewsReconciliation = {
     val reconciliation = new Reconciliation(
       system,
       new 
ReconciliationSettings(system.settings.config.getConfig(pluginLocation + 
".reconciler")))
     startedReconciliation.set(Some(reconciliation))
     reconciliation
   }
   
   def close(): Unit = startedReconciliation.getAndSet(None).foreach(_.close())
   ```
   
   Happy to push that if it looks right to you.
   


-- 
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