Program: 60271
Project: 60271
Buggy source:
/** 
 * Provides singleton scope with the following properties: - creates no more than one instance per Key as a creator is used no more than once, - result is cached and returned quickly on subsequent calls, - exception in a creator is not treated as instance creation and is not cached, - creates singletons in parallel whenever possible, - waits for dependent singletons to be created even across threads and when dependencies are shared as long as no circular dependencies are detected, - returns circular proxy only when circular dependencies are detected, - aside from that, blocking synchronization is only used for proxy creation and initialization,
 * @see CycleDetectingLockFactory
 */
@Override public <T>Provider<T> scope(final Key<T> key,final Provider<T> creator){
  return new Provider<T>(){
    /** 
 * The lazily initialized singleton instance. Once set, this will either have type T or will be equal to NULL. Would never be reset to null.
 */
    volatile Object instance;
    /** 
 * Circular proxies are used when potential deadlocks are detected. Guarded by itself. ConstructionContext is not thread-safe, so each call should be synchronized.
 */
    final ConstructionContext<T> constructionContext=new ConstructionContext<>();
    /** 
 * For each binding there is a separate lock that we hold during object creation. 
 */
    final CycleDetectingLock<Key<?>> creationLock=cycleDetectingLockFactory.create(key);
    /** 
 * The singleton provider needs a reference back to the injector, in order to get ahold of InternalContext during instantiation.
 */
    final InjectorImpl injector;
{
      if (creator instanceof ProviderToInternalFactoryAdapter) {
        injector=((ProviderToInternalFactoryAdapter)creator).getInjector();
      }
 else {
        injector=null;
      }
    }
    @SuppressWarnings("String_Node_Str") @Override public T get(){
      final Object initialInstance=instance;
      if (initialInstance == null) {
        final InternalContext context=injector == null ? null : injector.getLocalContext();
        final ListMultimap<Thread,Key<?>> locksCycle=creationLock.lockOrDetectPotentialLocksCycle();
        if (locksCycle.isEmpty()) {
          try {
            if (instance == null) {
              T provided=creator.get();
              Object providedNotNull=provided == null ? NULL : provided;
              if (instance == null) {
                if (Scopes.isCircularProxy(provided)) {
                  return provided;
                }
synchronized (constructionContext) {
                  instance=providedNotNull;
                  constructionContext.setProxyDelegates(provided);
                }
              }
 else {
                Preconditions.checkState(instance == providedNotNull,"String_Node_Str");
              }
            }
          }
 catch (          RuntimeException e) {
synchronized (constructionContext) {
              constructionContext.finishConstruction();
            }
            throw e;
          }
 finally {
            creationLock.unlock();
          }
        }
 else {
          if (context == null) {
            throw new ProvisionException(ImmutableList.of(createCycleDependenciesMessage(locksCycle,null)));
          }
synchronized (constructionContext) {
            if (instance == null) {
              Dependency<?> dependency=Preconditions.checkNotNull(context.getDependency(),"String_Node_Str");
              Class<?> rawType=dependency.getKey().getTypeLiteral().getRawType();
              try {
                @SuppressWarnings("String_Node_Str") T proxy=(T)constructionContext.createProxy(context.getInjectorOptions(),rawType);
                return proxy;
              }
 catch (              InternalProvisionException e) {
                Message proxyCreationError=Iterables.getOnlyElement(e.getErrors());
                Message cycleDependenciesMessage=createCycleDependenciesMessage(locksCycle,proxyCreationError);
                throw new ProvisionException(ImmutableList.of(cycleDependenciesMessage,proxyCreationError));
              }
            }
          }
        }
        final Object initializedInstance=instance;
        Preconditions.checkState(initializedInstance != null,"String_Node_Str");
        @SuppressWarnings("String_Node_Str") T initializedTypedInstance=(T)initializedInstance;
        return initializedInstance == NULL ? null : initializedTypedInstance;
      }
 else {
        @SuppressWarnings("String_Node_Str") T typedInitialIntance=(T)initialInstance;
        return initialInstance == NULL ? null : typedInitialIntance;
      }
    }
    /** 
 * Helper method to create beautiful and rich error descriptions. Best effort and slow. Tries its best to provide dependency information from injectors currently available in a global internal context. <p>The main thing being done is creating a list of Dependencies involved into lock cycle across all the threads involved. This is a structure we're creating: <pre> { Current Thread, C.class, B.class, Other Thread, B.class, C.class, Current Thread } To be inserted in the beginning by Guice: { A.class, B.class, C.class } </pre> When we're calling Guice to create A and it fails in the deadlock while trying to create C, which is being created by another thread, which waits for B. List would be reversed before printing it to the end user.
 */
    private Message createCycleDependenciesMessage(    ListMultimap<Thread,Key<?>> locksCycle,    Message proxyCreationError){
      StringBuilder sb=new StringBuilder();
      Formatter fmt=new Formatter(sb);
      fmt.format("String_Node_Str");
      if (proxyCreationError != null) {
        fmt.format("String_Node_Str",proxyCreationError.getMessage());
      }
      fmt.format("String_Node_Str");
      for (      Thread lockedThread : locksCycle.keySet()) {
        List<Key<?>> lockedKeys=locksCycle.get(lockedThread);
        fmt.format("String_Node_Str",lockedThread);
        for (        Key<?> lockedKey : lockedKeys) {
          fmt.format("String_Node_Str",Errors.convert(lockedKey));
        }
        for (        StackTraceElement traceElement : lockedThread.getStackTrace()) {
          fmt.format("String_Node_Str",traceElement);
        }
      }
      fmt.close();
      return new Message(Thread.currentThread(),sb.toString());
    }
    @Override public String toString(){
      return String.format("String_Node_Str",creator,Scopes.SINGLETON);
    }
  }
;
}

Fixed version (ground truth):
/** 
 * Provides singleton scope with the following properties: <ul> <li>creates no more than one instance per Key as a creator is used no more than once <li>result is cached and returned quickly on subsequent calls <li>exception in a creator is not treated as instance creation and is not cached <li>creates singletons in parallel whenever possible <li>waits for dependent singletons to be created even across threads and when dependencies are shared as long as no circular dependencies are detected <li>returns circular proxy only when circular dependencies are detected <li>aside from that, blocking synchronization is only used for proxy creation and initialization </ul>
 * @see CycleDetectingLockFactory
 */
@Override public <T>Provider<T> scope(final Key<T> key,final Provider<T> creator){
  return new Provider<T>(){
    /** 
 * The lazily initialized singleton instance. Once set, this will either have type T or will be equal to NULL. Would never be reset to null. <p>Locking strategy: double-checked locking for quick exit when scope is initialized.
 */
    volatile Object instance;
    /** 
 * Circular proxies are used when potential deadlocks are detected. Guarded by itself. ConstructionContext is not thread-safe, so each call should be synchronized. <p>Locking strategy: manipulations with proxies list or instance initialization.
 */
    final ConstructionContext<T> constructionContext=new ConstructionContext<>();
    /** 
 * For each binding there is a separate lock that we hold during object creation. <p>Locking strategy: singleton instance creation. <ul> <li>allows to guarantee only one instance per singleton, <li>special type of a lock, that prevents potential deadlocks, <li>guards constructionContext for all operations except proxy creation </ul>
 */
    final CycleDetectingLock<Key<?>> creationLock=cycleDetectingLockFactory.create(key);
    /** 
 * The singleton provider needs a reference back to the injector, in order to get ahold of InternalContext during instantiation.
 */
    final InjectorImpl injector;
{
      if (creator instanceof ProviderToInternalFactoryAdapter) {
        injector=((ProviderToInternalFactoryAdapter)creator).getInjector();
      }
 else {
        injector=null;
      }
    }
    @SuppressWarnings("String_Node_Str") @Override public T get(){
      final Object initialInstance=instance;
      if (initialInstance == null) {
        final InternalContext context=injector == null ? null : injector.getLocalContext();
        final ListMultimap<Thread,Key<?>> locksCycle=creationLock.lockOrDetectPotentialLocksCycle();
        if (locksCycle.isEmpty()) {
          try {
            if (instance == null) {
              T provided=creator.get();
              Object providedNotNull=provided == null ? NULL : provided;
              if (instance == null) {
                if (Scopes.isCircularProxy(provided)) {
                  return provided;
                }
synchronized (constructionContext) {
                  instance=providedNotNull;
                  constructionContext.setProxyDelegates(provided);
                }
              }
 else {
                Preconditions.checkState(instance == providedNotNull,"String_Node_Str");
              }
            }
          }
 catch (          RuntimeException e) {
synchronized (constructionContext) {
              constructionContext.finishConstruction();
            }
            throw e;
          }
 finally {
            creationLock.unlock();
          }
        }
 else {
          if (context == null) {
            throw new ProvisionException(ImmutableList.of(createCycleDependenciesMessage(locksCycle,null)));
          }
synchronized (constructionContext) {
            if (instance == null) {
              Dependency<?> dependency=Preconditions.checkNotNull(context.getDependency(),"String_Node_Str");
              Class<?> rawType=dependency.getKey().getTypeLiteral().getRawType();
              try {
                @SuppressWarnings("String_Node_Str") T proxy=(T)constructionContext.createProxy(context.getInjectorOptions(),rawType);
                return proxy;
              }
 catch (              InternalProvisionException e) {
                Message proxyCreationError=Iterables.getOnlyElement(e.getErrors());
                Message cycleDependenciesMessage=createCycleDependenciesMessage(locksCycle,proxyCreationError);
                throw new ProvisionException(ImmutableList.of(cycleDependenciesMessage,proxyCreationError));
              }
            }
          }
        }
        final Object initializedInstance=instance;
        Preconditions.checkState(initializedInstance != null,"String_Node_Str");
        @SuppressWarnings("String_Node_Str") T initializedTypedInstance=(T)initializedInstance;
        return initializedInstance == NULL ? null : initializedTypedInstance;
      }
 else {
        @SuppressWarnings("String_Node_Str") T typedInitialIntance=(T)initialInstance;
        return initialInstance == NULL ? null : typedInitialIntance;
      }
    }
    /** 
 * Helper method to create beautiful and rich error descriptions. Best effort and slow. Tries its best to provide dependency information from injectors currently available in a global internal context. <p>The main thing being done is creating a list of Dependencies involved into lock cycle across all the threads involved. This is a structure we're creating: <pre> { Current Thread, C.class, B.class, Other Thread, B.class, C.class, Current Thread } To be inserted in the beginning by Guice: { A.class, B.class, C.class } </pre> When we're calling Guice to create A and it fails in the deadlock while trying to create C, which is being created by another thread, which waits for B. List would be reversed before printing it to the end user.
 */
    private Message createCycleDependenciesMessage(    ListMultimap<Thread,Key<?>> locksCycle,    Message proxyCreationError){
      StringBuilder sb=new StringBuilder();
      Formatter fmt=new Formatter(sb);
      fmt.format("String_Node_Str");
      if (proxyCreationError != null) {
        fmt.format("String_Node_Str",proxyCreationError.getMessage());
      }
      fmt.format("String_Node_Str");
      for (      Thread lockedThread : locksCycle.keySet()) {
        List<Key<?>> lockedKeys=locksCycle.get(lockedThread);
        fmt.format("String_Node_Str",lockedThread);
        for (        Key<?> lockedKey : lockedKeys) {
          fmt.format("String_Node_Str",Errors.convert(lockedKey));
        }
        for (        StackTraceElement traceElement : lockedThread.getStackTrace()) {
          fmt.format("String_Node_Str",traceElement);
        }
      }
      fmt.close();
      return new Message(Thread.currentThread(),sb.toString());
    }
    @Override public String toString(){
      return String.format("String_Node_Str",creator,Scopes.SINGLETON);
    }
  }
;
}