Program: 4481
Project: 4481
Buggy source:
/** 
 * Like tryDownloads2, but does not deal with the library, cleaning up corrupt files, etc.  Caller should look at corruptState to determine if the file is corrupted; a return value of COMPLETE does not mean no corruptions where encountered.
 * @return COMPLETE if a file was successfully downloadedWAITING_FOR_RETRY if no file was downloaded, but it makes sense  to try again later because some hosts reported busy. The caller should usually wait before retrying. GAVE_UP the download attempt failed, and there are  no more locations to try. COULDNT_MOVE_TO_LIBRARY couldn't write the incomplete file
 * @exception InterruptedException if the someone stop()'ed this download.stop() was called either because the user killed the download or a corruption was detected and they chose to kill and discard the download.  Calls to resume() do not result in InterruptedException.
 */
private int tryAllDownloads3() throws InterruptedException {
  LOG.trace("String_Node_Str");
  int completedSize=-1;
synchronized (this) {
    needed=new IntervalSet();
{
      Assert.that(incompleteFile != null);
synchronized (incompleteFileManager) {
        if (commonOutFile != null)         commonOutFile.clearManagedDownloader();
        commonOutFile=incompleteFileManager.getEntry(incompleteFile);
      }
      if (commonOutFile == null) {
        LOG.trace("String_Node_Str");
        commonOutFile=new VerifyingFile(true);
        incompleteFileManager.addEntry(incompleteFile,commonOutFile);
{
          FileDesc fd=fileManager.getFileDescForFile(incompleteFile);
          URN bHash=buckets.getURNForBucket(bucketNumber);
          if (bHash != null && fd != null && !bHash.equals(fd.getSHA1Urn())) {
            File canon=null;
            Exception ioe=null;
            try {
              canon=FileUtils.getCanonicalFile(incompleteFile);
            }
 catch (            IOException e) {
              ioe=e;
            }
            Assert.silent(false,"String_Node_Str" + "String_Node_Str" + bHash + "String_Node_Str"+ "String_Node_Str"+ fd.getSHA1Urn()+ "String_Node_Str"+ "String_Node_Str"+ incompleteFile+ "String_Node_Str"+ "String_Node_Str"+ fd.getFile()+ "String_Node_Str"+ "String_Node_Str"+ canon+ "String_Node_Str"+ "String_Node_Str"+ (ioe == null ? "String_Node_Str" : ioe.getMessage())+ "String_Node_Str"+ "String_Node_Str"+ incompleteFileManager.dumpHashes());
            fileManager.removeFileIfShared(incompleteFile);
          }
        }
      }
      try {
        commonOutFile.open(incompleteFile,this);
      }
 catch (      IOException e) {
        if (StringUtils.contains(e.getMessage(),"String_Node_Str",true)) {
          MessageService.showMessage("String_Node_Str");
        }
 else {
          ErrorService.error(e);
        }
        return COULDNT_MOVE_TO_LIBRARY;
      }
      completedSize=(int)IncompleteFileManager.getCompletedSize(incompleteFile);
      Iterator iter=commonOutFile.getFreeBlocks(completedSize);
      while (iter.hasNext())       addToNeeded((Interval)iter.next());
    }
  }
  currentRFDs=new LinkedList();
  int size=-1;
  int connectTo=-1;
  int dloadsCount=-1;
  Assert.that(threads.size() == 0);
  while (true) {
synchronized (stealLock) {
synchronized (this) {
        if (stopped) {
          LOG.warn("String_Node_Str");
          throw new InterruptedException();
        }
        if (dloaders.size() == 0 && needed.isEmpty()) {
          int doneSize=(int)IncompleteFileManager.getCompletedSize(incompleteFile);
          Assert.that(completedSize == doneSize,"String_Node_Str");
          Iterator freeBlocks=commonOutFile.getFreeBlocks(doneSize);
          if (freeBlocks.hasNext()) {
            while (freeBlocks.hasNext())             addToNeeded((Interval)freeBlocks.next());
            Assert.silent(false,"String_Node_Str" + "String_Node_Str" + needed);
          }
 else {
            for (int i=threads.size(); i > 0; i--) {
              Thread t=(Thread)threads.get(i - 1);
              t.interrupt();
            }
            LOG.trace("String_Node_Str");
            return COMPLETE;
          }
        }
        if (threads.size() == 0) {
          if (files.size() > 0 && calculateWaitTime() > 0) {
            LOG.trace("String_Node_Str");
            return WAITING_FOR_RETRY;
          }
 else           if (files.size() == 0) {
            LOG.trace("String_Node_Str");
            return GAVE_UP;
          }
        }
        size=files.size();
        connectTo=getNumAllowedDownloads();
        dloadsCount=dloaders.size();
      }
    }
    for (int i=0; i < (connectTo + 1) && i < size && dloadsCount < getSwarmCapacity(); i++) {
      final RemoteFileDesc rfd;
synchronized (this) {
        rfd=removeBest(files);
        if (rfd.isBusy()) {
          files.add(rfd);
          break;
        }
        currentRFDs.add(rfd);
      }
      Thread connectCreator=new Thread("String_Node_Str"){
        public void run(){
          boolean iterate=false;
          try {
            iterate=connectAndDownload(rfd);
          }
 catch (          Throwable e) {
            iterate=true;
            if (!(e instanceof InterruptedException)) {
              ErrorService.error(e);
            }
          }
 finally {
synchronized (ManagedDownloader.this) {
              currentRFDs.remove(rfd);
              threads.remove(this);
              if (iterate)               ManagedDownloader.this.notifyAll();
            }
          }
        }
      }
;
synchronized (this) {
        threads.add(connectCreator);
      }
      connectCreator.start();
    }
synchronized (this) {
      try {
        this.wait(4000);
      }
 catch (      InterruptedException ee) {
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Like tryDownloads2, but does not deal with the library, cleaning up corrupt files, etc.  Caller should look at corruptState to determine if the file is corrupted; a return value of COMPLETE does not mean no corruptions where encountered.
 * @return COMPLETE if a file was successfully downloadedWAITING_FOR_RETRY if no file was downloaded, but it makes sense  to try again later because some hosts reported busy. The caller should usually wait before retrying. GAVE_UP the download attempt failed, and there are  no more locations to try. COULDNT_MOVE_TO_LIBRARY couldn't write the incomplete file
 * @exception InterruptedException if the someone stop()'ed this download.stop() was called either because the user killed the download or a corruption was detected and they chose to kill and discard the download.  Calls to resume() do not result in InterruptedException.
 */
private int tryAllDownloads3() throws InterruptedException {
  LOG.trace("String_Node_Str");
  int completedSize=-1;
synchronized (this) {
    needed=new IntervalSet();
{
      Assert.that(incompleteFile != null);
synchronized (incompleteFileManager) {
        if (commonOutFile != null)         commonOutFile.clearManagedDownloader();
        commonOutFile=incompleteFileManager.getEntry(incompleteFile);
      }
      if (commonOutFile == null) {
        LOG.trace("String_Node_Str");
        commonOutFile=new VerifyingFile(true);
        try {
          incompleteFileManager.addEntry(incompleteFile,commonOutFile);
        }
 catch (        IOException ioe) {
          ErrorService.error(ioe,"String_Node_Str" + incompleteFile);
          return COULDNT_MOVE_TO_LIBRARY;
        }
{
          FileDesc fd=fileManager.getFileDescForFile(incompleteFile);
          URN bHash=buckets.getURNForBucket(bucketNumber);
          if (bHash != null && fd != null && !bHash.equals(fd.getSHA1Urn())) {
            File canon=null;
            Exception ioe=null;
            try {
              canon=FileUtils.getCanonicalFile(incompleteFile);
            }
 catch (            IOException e) {
              ioe=e;
            }
            Assert.silent(false,"String_Node_Str" + "String_Node_Str" + this.getClass().getName() + "String_Node_Str"+ "String_Node_Str"+ bHash+ "String_Node_Str"+ "String_Node_Str"+ fd.getSHA1Urn()+ "String_Node_Str"+ "String_Node_Str"+ incompleteFile+ "String_Node_Str"+ "String_Node_Str"+ fd.getFile()+ "String_Node_Str"+ "String_Node_Str"+ canon+ "String_Node_Str"+ "String_Node_Str"+ (ioe == null ? "String_Node_Str" : ioe.getMessage())+ "String_Node_Str"+ "String_Node_Str"+ incompleteFileManager.dumpHashes());
            fileManager.removeFileIfShared(incompleteFile);
          }
        }
      }
      try {
        commonOutFile.open(incompleteFile,this);
      }
 catch (      IOException e) {
        if (StringUtils.contains(e.getMessage(),"String_Node_Str",true)) {
          MessageService.showMessage("String_Node_Str");
        }
 else {
          ErrorService.error(e);
        }
        return COULDNT_MOVE_TO_LIBRARY;
      }
      completedSize=(int)IncompleteFileManager.getCompletedSize(incompleteFile);
      Iterator iter=commonOutFile.getFreeBlocks(completedSize);
      while (iter.hasNext())       addToNeeded((Interval)iter.next());
    }
  }
  currentRFDs=new LinkedList();
  int size=-1;
  int connectTo=-1;
  int dloadsCount=-1;
  Assert.that(threads.size() == 0);
  while (true) {
synchronized (stealLock) {
synchronized (this) {
        if (stopped) {
          LOG.warn("String_Node_Str");
          throw new InterruptedException();
        }
        if (dloaders.size() == 0 && needed.isEmpty()) {
          int doneSize=(int)IncompleteFileManager.getCompletedSize(incompleteFile);
          Assert.that(completedSize == doneSize,"String_Node_Str");
          Iterator freeBlocks=commonOutFile.getFreeBlocks(doneSize);
          if (freeBlocks.hasNext()) {
            while (freeBlocks.hasNext())             addToNeeded((Interval)freeBlocks.next());
            Assert.silent(false,"String_Node_Str" + "String_Node_Str" + needed);
          }
 else {
            for (int i=threads.size(); i > 0; i--) {
              Thread t=(Thread)threads.get(i - 1);
              t.interrupt();
            }
            LOG.trace("String_Node_Str");
            return COMPLETE;
          }
        }
        if (threads.size() == 0) {
          if (files.size() > 0 && calculateWaitTime() > 0) {
            LOG.trace("String_Node_Str");
            return WAITING_FOR_RETRY;
          }
 else           if (files.size() == 0) {
            LOG.trace("String_Node_Str");
            return GAVE_UP;
          }
        }
        size=files.size();
        connectTo=getNumAllowedDownloads();
        dloadsCount=dloaders.size();
      }
    }
    for (int i=0; i < (connectTo + 1) && i < size && dloadsCount < getSwarmCapacity(); i++) {
      final RemoteFileDesc rfd;
synchronized (this) {
        rfd=removeBest(files);
        if (rfd.isBusy()) {
          files.add(rfd);
          break;
        }
        currentRFDs.add(rfd);
      }
      Thread connectCreator=new Thread("String_Node_Str"){
        public void run(){
          boolean iterate=false;
          try {
            iterate=connectAndDownload(rfd);
          }
 catch (          Throwable e) {
            iterate=true;
            if (!(e instanceof InterruptedException)) {
              ErrorService.error(e);
            }
          }
 finally {
synchronized (ManagedDownloader.this) {
              currentRFDs.remove(rfd);
              threads.remove(this);
              if (iterate)               ManagedDownloader.this.notifyAll();
            }
          }
        }
      }
;
synchronized (this) {
        threads.add(connectCreator);
      }
      connectCreator.start();
    }
synchronized (this) {
      try {
        this.wait(4000);
      }
 catch (      InterruptedException ee) {
      }
    }
  }
}