Program: 4566
Project: 4566
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 {
  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) {
        debug("String_Node_Str");
        commonOutFile=new VerifyingFile(true);
        incompleteFileManager.addEntry(incompleteFile,commonOutFile);
{
          FileDesc f=fileManager.getFileDescForFile(incompleteFile);
          URN bucketHash=buckets.getURNForBucket(bucketNumber);
          if (bucketHash != null && f != null) {
            Assert.silent(bucketHash.equals(f.getSHA1Urn()),"String_Node_Str");
            fileManager.removeFileIfShared(incompleteFile);
          }
        }
      }
      try {
        commonOutFile.open(incompleteFile,this);
      }
 catch (      IOException e) {
        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());
    }
  }
  busy=new LinkedList();
  int size=-1;
  int connectTo=-1;
  int dloadsCount=-1;
  Assert.that(threads.size() == 0);
  while (true) {
synchronized (stealLock) {
synchronized (this) {
        if (stopped) {
          debug("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();
            }
            debug("String_Node_Str");
            return COMPLETE;
          }
        }
        if (threads.size() == 0 && files.size() == 0) {
          if (busy.size() > 0) {
            debug("String_Node_Str");
            files.addAll(busy);
            return WAITING_FOR_RETRY;
          }
 else {
            debug("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=removeBest(files);
      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) {
              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 {
  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) {
        debug("String_Node_Str");
        commonOutFile=new VerifyingFile(true);
        incompleteFileManager.addEntry(incompleteFile,commonOutFile);
{
          FileDesc f=fileManager.getFileDescForFile(incompleteFile);
          URN bucketHash=buckets.getURNForBucket(bucketNumber);
          if (bucketHash != null && f != null && !bucketHash.equals(f.getSHA1Urn())) {
            Assert.silent(false,"String_Node_Str");
            fileManager.removeFileIfShared(incompleteFile);
          }
        }
      }
      try {
        commonOutFile.open(incompleteFile,this);
      }
 catch (      IOException e) {
        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());
    }
  }
  busy=new LinkedList();
  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) {
          debug("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();
            }
            debug("String_Node_Str");
            return COMPLETE;
          }
        }
        if (threads.size() == 0 && files.size() == 0) {
          if (busy.size() > 0) {
            debug("String_Node_Str");
            files.addAll(busy);
            return WAITING_FOR_RETRY;
          }
 else {
            debug("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);
        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) {
      }
    }
  }
}