- 
                Notifications
    You must be signed in to change notification settings 
- Fork 560
          Fix memory leak in Supervisor internal state
          #4500
        
          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
          
     Open
      
      
            durban
  wants to merge
  8
  commits into
  series/3.6.x
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
issue4490
  
      
      
   
  
    
  
  
  
 
  
      
    base: series/3.6.x
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 6 commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      e7988ac
              
                Reproduce issue #4490
              
              
                durban e0aac4c
              
                More tests
              
              
                durban e07bc85
              
                Fix it
              
              
                durban 2ec6927
              
                scalafmt
              
              
                durban accf623
              
                Cleanup
              
              
                durban 0cb2169
              
                scalafmt
              
              
                durban 52cb203
              
                mima
              
              
                durban 5e9b617
              
                Try to fix test timeout on JS
              
              
                durban File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -141,7 +141,7 @@ object Supervisor { | |
| def apply[F[_]: Concurrent]: Resource[F, Supervisor[F]] = | ||
| apply[F](false) | ||
|  | ||
| private sealed abstract class State[F[_]] { | ||
| private[std] sealed abstract class State[F[_]] { | ||
|  | ||
| def remove(token: Unique.Token): F[Unit] | ||
|  | ||
|  | @@ -150,6 +150,8 @@ object Supervisor { | |
| */ | ||
| def add(token: Unique.Token, fiber: Fiber[F, Throwable, ?]): F[Boolean] | ||
|  | ||
| private[std] def numberOfFibers: F[Int] // for testing | ||
|  | ||
| // these are allowed to destroy the state, since they're only called during closing: | ||
| val joinAll: F[Unit] | ||
| val cancelAll: F[Unit] | ||
|  | @@ -169,152 +171,161 @@ object Supervisor { | |
| case (st, _) => | ||
| doneR.set(true) *> st.cancelAll | ||
| } | ||
| } yield new Supervisor[F] { | ||
|  | ||
| def supervise[A](fa: F[A]): F[Fiber[F, Throwable, A]] = | ||
| F.uncancelable { _ => | ||
| val monitor: (F[A], F[Unit]) => F[Fiber[F, Throwable, A]] = checkRestart match { | ||
| case Some(restart) => { (fa, fin) => | ||
| F.deferred[Outcome[F, Throwable, A]] flatMap { resultR => | ||
| F.ref(false) flatMap { canceledR => | ||
| F.deferred[Fiber[F, Throwable, A]].flatMap { firstCurrent => | ||
| // `currentR` holds (a `Deferred` to) the current | ||
| // incarnation of the fiber executing `fa`: | ||
| F.ref(firstCurrent).flatMap { currentR => | ||
| def action(current: Deferred[F, Fiber[F, Throwable, A]]): F[Unit] = { | ||
| F uncancelable { _ => | ||
| val started = F start { | ||
| fa guaranteeCase { oc => | ||
| F.deferred[Fiber[F, Throwable, A]].flatMap { newCurrent => | ||
| // we're replacing the `Deferred` holding | ||
| // the current fiber with a new one before | ||
| // the current fiber finishes, and even | ||
| // before we check for the cancel signal; | ||
| // this guarantees, that the fiber reachable | ||
| // through `currentR` is the last one (or | ||
| // null, see below): | ||
| currentR.set(newCurrent) *> { | ||
| canceledR.get flatMap { canceled => | ||
| doneR.get flatMap { done => | ||
| if (!canceled && !done && restart(oc)) { | ||
| action(newCurrent) | ||
| } else { | ||
| // we must complete `newCurrent`, | ||
| // because `cancel` below may wait | ||
| // for it; we signal that it is not | ||
| // restarted with `null`: | ||
| newCurrent.complete(null) *> fin.guarantee( | ||
| resultR.complete(oc).void) | ||
| } | ||
| } yield new SupervisorImpl[F](checkRestart, doneR, state) | ||
| } | ||
|  | ||
| private[std] final class SupervisorImpl[F[_]]( | ||
| checkRestart: Option[Outcome[F, Throwable, ?] => Boolean], | ||
| doneR: Ref[F, Boolean], | ||
| private[std] val state: State[F] | ||
| )(implicit F: Concurrent[F]) | ||
| extends Supervisor[F] { | ||
|  | ||
| def supervise[A](fa: F[A]): F[Fiber[F, Throwable, A]] = | ||
| F.uncancelable { _ => | ||
| val monitor: (F[A], F[Unit]) => F[Fiber[F, Throwable, A]] = checkRestart match { | ||
| case Some(restart) => { (fa, fin) => | ||
| F.deferred[Outcome[F, Throwable, A]] flatMap { resultR => | ||
| F.ref(false) flatMap { canceledR => | ||
| F.deferred[Fiber[F, Throwable, A]].flatMap { firstCurrent => | ||
| // `currentR` holds (a `Deferred` to) the current | ||
| // incarnation of the fiber executing `fa`: | ||
| F.ref(firstCurrent).flatMap { currentR => | ||
| def action(current: Deferred[F, Fiber[F, Throwable, A]]): F[Unit] = { | ||
| F uncancelable { _ => | ||
| val started = F start { | ||
| fa guaranteeCase { oc => | ||
| F.deferred[Fiber[F, Throwable, A]].flatMap { newCurrent => | ||
| // we're replacing the `Deferred` holding | ||
| // the current fiber with a new one before | ||
| // the current fiber finishes, and even | ||
| // before we check for the cancel signal; | ||
| // this guarantees, that the fiber reachable | ||
| // through `currentR` is the last one (or | ||
| // null, see below): | ||
| currentR.set(newCurrent) *> { | ||
| canceledR.get flatMap { canceled => | ||
| doneR.get flatMap { done => | ||
| if (!canceled && !done && restart(oc)) { | ||
| action(newCurrent) | ||
| } else { | ||
| // we must complete `newCurrent`, | ||
| // because `cancel` below may wait | ||
| // for it; we signal that it is not | ||
| // restarted with `null`: | ||
| newCurrent.complete(null) *> fin.guarantee( | ||
| resultR.complete(oc).void) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|  | ||
| started flatMap { f => current.complete(f).void } | ||
| } | ||
|  | ||
| started flatMap { f => current.complete(f).void } | ||
| } | ||
| } | ||
|  | ||
| action(firstCurrent).as( | ||
| new Fiber[F, Throwable, A] { | ||
|  | ||
| private[this] val delegateF = currentR.get.flatMap(_.get) | ||
|  | ||
| val cancel: F[Unit] = F uncancelable { _ => | ||
| // after setting `canceledR`, at | ||
| // most one restart happens, and | ||
| // the fiber we get through `delegateF` | ||
| // is the final one: | ||
| canceledR.set(true) *> delegateF flatMap { | ||
| case null => | ||
| // ok, task wasn't restarted, but we | ||
| // wait for the result to be completed | ||
| // (and the finalizer to run): | ||
| resultR.get.void | ||
| case fiber => | ||
| fiber.cancel *> fiber.join flatMap { | ||
| case Outcome.Canceled() => | ||
| // cancel successful (or self-canceled), | ||
| // but we don't know if the `guaranteeCase` | ||
| // above ran so we need to double check: | ||
| delegateF.flatMap { | ||
| case null => | ||
| // ok, the `guaranteeCase` | ||
| // certainly executed/ing: | ||
| resultR.get.void | ||
| case fiber2 => | ||
| // we cancelled the fiber before it did | ||
| // anything, so the finalizer didn't run, | ||
| // we need to do it now: | ||
| val cleanup = fin.guarantee( | ||
| resultR.complete(Outcome.Canceled()).void | ||
| ) | ||
| if (fiber2 eq fiber) { | ||
| cleanup | ||
| } else { | ||
| // this should never happen | ||
| cleanup *> F.raiseError(new AssertionError( | ||
| "unexpected fiber (this is a bug in Supervisor)")) | ||
| } | ||
| } | ||
| case _ => | ||
| // finished in error/success, | ||
| // the outcome will certainly | ||
| // be completed: | ||
| resultR.get.void | ||
| } | ||
| } | ||
| action(firstCurrent).as( | ||
| new Fiber[F, Throwable, A] { | ||
|  | ||
| private[this] val delegateF = currentR.get.flatMap(_.get) | ||
|  | ||
| val cancel: F[Unit] = F uncancelable { _ => | ||
| // after setting `canceledR`, at | ||
| // most one restart happens, and | ||
| // the fiber we get through `delegateF` | ||
| // is the final one: | ||
| canceledR.set(true) *> delegateF flatMap { | ||
| case null => | ||
| // ok, task wasn't restarted, but we | ||
| // wait for the result to be completed | ||
| // (and the finalizer to run): | ||
| resultR.get.void | ||
| case fiber => | ||
| fiber.cancel *> fiber.join flatMap { | ||
| case Outcome.Canceled() => | ||
| // cancel successful (or self-canceled), | ||
| // but we don't know if the `guaranteeCase` | ||
| // above ran so we need to double check: | ||
| delegateF.flatMap { | ||
| case null => | ||
| // ok, the `guaranteeCase` | ||
| // certainly executed/ing: | ||
| resultR.get.void | ||
| case fiber2 => | ||
| // we cancelled the fiber before it did | ||
| // anything, so the finalizer didn't run, | ||
| // we need to do it now: | ||
| val cleanup = fin.guarantee( | ||
| resultR.complete(Outcome.Canceled()).void | ||
| ) | ||
| if (fiber2 eq fiber) { | ||
| cleanup | ||
| } else { | ||
| // this should never happen | ||
| cleanup *> F.raiseError(new AssertionError( | ||
| "unexpected fiber (this is a bug in Supervisor)")) | ||
| } | ||
| } | ||
| case _ => | ||
| // finished in error/success, | ||
| // the outcome will certainly | ||
| // be completed: | ||
| resultR.get.void | ||
| } | ||
| } | ||
|  | ||
| def join = resultR.get | ||
| } | ||
| ) | ||
| } | ||
|  | ||
| def join = resultR.get | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|  | ||
| case None => (fa, fin) => F.start(fa.guarantee(fin)) | ||
| case None => { (fa, fin) => | ||
| F.start(fa).flatMap { fib => F.start(fib.join.guarantee(fin)).as(fib) } | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual fix. (I believe the other  | ||
| } | ||
| } | ||
|  | ||
| for { | ||
| done <- F.ref(false) | ||
| insertResult <- F.deferred[Boolean] | ||
| token <- F.unique | ||
| cleanup = state.remove(token) | ||
| fiber <- monitor( | ||
| // if the supervisor have been (or is now) | ||
| // shutting down, inserting into state will | ||
| // fail; so we need to wait for the positive result | ||
| // of inserting, before actually doing the task: | ||
| insertResult | ||
| .get | ||
| .ifM( | ||
| fa, | ||
| F.canceled *> F.raiseError[A](new AssertionError( | ||
| "supervised fiber couldn't cancel (this is a bug in Supervisor)")) | ||
| ), | ||
| done.set(true) *> cleanup | ||
| ) | ||
| insertOk <- state.add(token, fiber) | ||
| _ <- insertResult.complete(insertOk) | ||
| // `cleanup` could run BEFORE the `state.add` | ||
| // (if `fa` is very fast), in which case it doesn't | ||
| // remove the fiber from the state, so we re-check: | ||
| _ <- done.get.ifM(cleanup, F.unit) | ||
| _ <- { | ||
| if (!insertOk) { | ||
| F.raiseError(new IllegalStateException("supervisor already shutdown")) | ||
| } else { | ||
| F.unit | ||
| } | ||
| for { | ||
| done <- F.ref(false) | ||
| insertResult <- F.deferred[Boolean] | ||
| token <- F.unique | ||
| cleanup = state.remove(token) | ||
| fiber <- monitor( | ||
| // if the supervisor have been (or is now) | ||
| // shutting down, inserting into state will | ||
| // fail; so we need to wait for the positive result | ||
| // of inserting, before actually doing the task: | ||
| insertResult | ||
| .get | ||
| .ifM( | ||
| fa, | ||
| F.canceled *> F.raiseError[A](new AssertionError( | ||
| "supervised fiber couldn't cancel (this is a bug in Supervisor)")) | ||
| ), | ||
| done.set(true) *> cleanup | ||
| ) | ||
| insertOk <- state.add(token, fiber) | ||
| _ <- insertResult.complete(insertOk) | ||
| // `cleanup` could run BEFORE the `state.add` | ||
| // (if `fa` is very fast), in which case it doesn't | ||
| // remove the fiber from the state, so we re-check: | ||
| _ <- done.get.ifM(cleanup, F.unit) | ||
| _ <- { | ||
| if (!insertOk) { | ||
| F.raiseError(new IllegalStateException("supervisor already shutdown")) | ||
| } else { | ||
| F.unit | ||
| } | ||
| } yield fiber | ||
| } | ||
| } | ||
| } | ||
| } yield fiber | ||
| } | ||
| } | ||
|  | ||
| private[effect] def applyForConcurrent[F[_]]( | ||
|  | @@ -335,6 +346,9 @@ object Supervisor { | |
| case map => (map.updated(token, fiber), true) | ||
| } | ||
|  | ||
| private[std] final override def numberOfFibers: F[Int] = // for testing | ||
| stateRef.get.map(_.size) | ||
|  | ||
| private[this] val allFibers: F[List[Fiber[F, Throwable, ?]]] = { | ||
| // we're closing, so we won't need the state any more, | ||
| // so we're using `null` as a sentinel to reject later | ||
|  | @@ -371,6 +385,9 @@ object Supervisor { | |
| F.delay(state.put(token, fiber)) *> doneR.get.map(!_) | ||
| } | ||
|  | ||
| private[std] final override def numberOfFibers: F[Int] = // for testing | ||
| F.delay { state.size() } | ||
|  | ||
| private[this] val allFibers: F[List[Fiber[F, Throwable, ?]]] = | ||
| F delay { | ||
| val fibers = ListBuffer.empty[Fiber[F, Throwable, ?]] | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the changes in
Supervisorare just to make it testable (its internal state inspectable).