Skip to content

Instantly share code, notes, and snippets.

@rkuhn
Created July 23, 2013 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkuhn/6061663 to your computer and use it in GitHub Desktop.
Save rkuhn/6061663 to your computer and use it in GitHub Desktop.
“unbecoming” blog post races
var isDownloading = false
// set up a scheduler to sync the registry
system.scheduler.schedule(0 millis, 3 hours) {
if(!isDownloading) {
system.actorOf(Props[Downloader]) ! Download
}
}
/*
* Since isDownloading is not volatile any write performed by another thread
* might not ever be visible to the code above, in principle it would be
* a correct implementation to always execute the conditional branch.
*/
class Downloader extends Actor {
import context._
def receive = {
case _ =>
download onComplete unbecome
/*
* The line above is really problematic, since you are calling
* context.unbecome from a different thread; ActorContext must
* not ever leave the actor, and you are throwing it out there.
* The resulting race condition is that either the become or the
* unbecome can be lost when they happen “at the same time”.
*/
become {
case _ => // do nothing
}
}
def download = future {
// perform download
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment