Program: 4516
Project: 4516
Buggy source:
/** 
 * Actually does the download, finding duplicate files, trying all locations, resuming, waiting, and retrying as necessary. Also takes care of moving file from incomplete directory to save directory and adding file to the library.  Called from dloadManagerThread.  
 * @param deserialized True if this downloader was deserialized from disk,false if it was newly constructed.
 */
protected void tryAllDownloads(final boolean deserializedFromDisk){
  int numQueries=getQueryCount(deserializedFromDisk);
  long timeQuerySent=System.currentTimeMillis();
  long timeSpentWaiting=0;
  boolean triedLocatingSources=false;
  while (true) {
    try {
      setState(QUEUED);
      queuePosition="String_Node_Str";
      queuedVendor="String_Node_Str";
      manager.waitForSlot(this);
      boolean waitForRetry=false;
      bucketNumber=0;
      try {
        for (Iterator iter=buckets.buckets(); iter.hasNext(); bucketNumber++) {
          cleanup();
          files=(List)iter.next();
          if (checkHosts()) {
            setState(GAVE_UP);
            return;
          }
          if (files.size() <= 0)           continue;
          int status=tryAllDownloads2();
          if (status == COMPLETE) {
            setState(COMPLETE);
            manager.remove(this,true);
            return;
          }
 else           if (status == COULDNT_MOVE_TO_LIBRARY) {
            setState(COULDNT_MOVE_TO_LIBRARY);
            manager.remove(this,false);
            return;
          }
 else           if (status == CORRUPT_FILE) {
            setState(CORRUPT_FILE);
            manager.remove(this,false);
            return;
          }
 else           if (status == WAITING_FOR_RETRY) {
            waitForRetry=true;
          }
 else {
            Assert.that(status == GAVE_UP,"String_Node_Str" + status);
          }
        }
      }
 catch (      InterruptedException e) {
        if (!stopped)         ErrorService.error(e);
      }
      manager.yieldSlot(this);
      if (stopped) {
        setState(ABORTED);
        manager.remove(this,false);
        return;
      }
      Assert.that(getState() != GAVE_UP);
      Assert.that(getState() != COMPLETE);
      Assert.that(getState() != COULDNT_MOVE_TO_LIBRARY);
      Assert.that(getState() != CORRUPT_FILE);
      if ((this.originalQueryGUID != null) && !triedLocatingSources) {
        MessageRouter mr=RouterService.getMessageRouter();
        Set guessLocs=mr.getGuessLocs(this.originalQueryGUID);
        if ((guessLocs != null) && !guessLocs.isEmpty()) {
          setState(ITERATIVE_GUESSING);
          triedLocatingSources=true;
          boolean areThereNewResults=false;
          URN bestURN=getBestURN();
          for (Iterator i=guessLocs.iterator(); i.hasNext(); ) {
            GUESSEndpoint ep=(GUESSEndpoint)i.next();
            OnDemandUnicaster.query(ep,bestURN);
            if (!areThereNewResults)             areThereNewResults=reqLock.lock(750);
          }
          if (areThereNewResults)           continue;
        }
      }
      if (stopped) {
        setState(ABORTED);
        manager.remove(this,false);
        return;
      }
      final long currTime=System.currentTimeMillis();
      if (waitForRetry) {
synchronized (this) {
          retriesWaiting=0;
          for (Iterator iter=buckets.buckets(); iter.hasNext(); ) {
            List bucket=(List)iter.next();
            retriesWaiting+=bucket.size();
          }
        }
        long time=calculateWaitTime();
        setState(WAITING_FOR_RETRY,time);
        reqLock.lock(time);
      }
 else {
        boolean areThereNewResults=false;
        final long timeToWait=TIME_BETWEEN_REQUERIES - (System.currentTimeMillis() - timeQuerySent);
        if ((numQueries > 0) && (timeToWait > 0)) {
          setState(WAITING_FOR_RESULTS,timeToWait);
          areThereNewResults=reqLock.lock(timeToWait);
        }
        if (!areThereNewResults) {
          if (pauseForRequery(numQueries,deserializedFromDisk))           continue;
          if (numQueries < REQUERY_ATTEMPTS) {
            waitForStableConnections();
            try {
              if (manager.sendQuery(this,newRequery(numQueries)))               numQueries++;
              timeQuerySent=System.currentTimeMillis();
            }
 catch (            CantResumeException ignore) {
            }
          }
 else {
            long[] instructions=getFailedState(deserializedFromDisk,timeSpentWaiting);
            if (instructions[1] > 0) {
              setState((int)instructions[0],instructions[1]);
              reqLock.lock(instructions[1]);
              timeSpentWaiting+=System.currentTimeMillis() - currTime;
            }
 else {
              setState(GAVE_UP);
              reqLock.lock(0);
            }
          }
        }
      }
    }
 catch (    InterruptedException e) {
      if (stopped) {
        setState(ABORTED);
        manager.remove(this,false);
        return;
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Actually does the download, finding duplicate files, trying all locations, resuming, waiting, and retrying as necessary. Also takes care of moving file from incomplete directory to save directory and adding file to the library.  Called from dloadManagerThread.  
 * @param deserialized True if this downloader was deserialized from disk,false if it was newly constructed.
 */
protected void tryAllDownloads(final boolean deserializedFromDisk){
  int numQueries=getQueryCount(deserializedFromDisk);
  long timeQuerySent=System.currentTimeMillis();
  long timeSpentWaiting=0;
  boolean triedLocatingSources=false;
  while (true) {
    try {
      setState(QUEUED);
      queuePosition="String_Node_Str";
      queuedVendor="String_Node_Str";
      manager.waitForSlot(this);
      boolean waitForRetry=false;
      bucketNumber=0;
      try {
        for (Iterator iter=buckets.buckets(); iter.hasNext(); bucketNumber++) {
          cleanup();
          files=(List)iter.next();
          if (checkHosts()) {
            setState(GAVE_UP);
            return;
          }
          if (files.size() <= 0)           continue;
          int status=tryAllDownloads2();
          if (status == COMPLETE) {
            setState(COMPLETE);
            manager.remove(this,true);
            return;
          }
 else           if (status == COULDNT_MOVE_TO_LIBRARY) {
            setState(COULDNT_MOVE_TO_LIBRARY);
            manager.remove(this,false);
            return;
          }
 else           if (status == CORRUPT_FILE) {
            setState(CORRUPT_FILE);
            manager.remove(this,false);
            return;
          }
 else           if (status == WAITING_FOR_RETRY) {
            waitForRetry=true;
          }
 else {
            Assert.that(status == GAVE_UP,"String_Node_Str" + status);
          }
        }
      }
 catch (      InterruptedException e) {
        if (!stopped)         ErrorService.error(e);
      }
      manager.yieldSlot(this);
      if (stopped) {
        setState(ABORTED);
        manager.remove(this,false);
        return;
      }
      Assert.that(getState() != GAVE_UP);
      Assert.that(getState() != COMPLETE);
      Assert.that(getState() != COULDNT_MOVE_TO_LIBRARY);
      Assert.that(getState() != CORRUPT_FILE);
      if ((this.originalQueryGUID != null) && !triedLocatingSources) {
        MessageRouter mr=RouterService.getMessageRouter();
        Set guessLocs=mr.getGuessLocs(this.originalQueryGUID);
        if ((guessLocs != null) && !guessLocs.isEmpty()) {
          setState(ITERATIVE_GUESSING);
          triedLocatingSources=true;
          boolean areThereNewResults=false;
          final URN bestURN=getBestURN();
          for (Iterator i=guessLocs.iterator(); (bestURN != null) && i.hasNext(); ) {
            GUESSEndpoint ep=(GUESSEndpoint)i.next();
            OnDemandUnicaster.query(ep,bestURN);
            if (!areThereNewResults)             areThereNewResults=reqLock.lock(750);
          }
          if (areThereNewResults)           continue;
        }
      }
      if (stopped) {
        setState(ABORTED);
        manager.remove(this,false);
        return;
      }
      final long currTime=System.currentTimeMillis();
      if (waitForRetry) {
synchronized (this) {
          retriesWaiting=0;
          for (Iterator iter=buckets.buckets(); iter.hasNext(); ) {
            List bucket=(List)iter.next();
            retriesWaiting+=bucket.size();
          }
        }
        long time=calculateWaitTime();
        setState(WAITING_FOR_RETRY,time);
        reqLock.lock(time);
      }
 else {
        boolean areThereNewResults=false;
        final long timeToWait=TIME_BETWEEN_REQUERIES - (System.currentTimeMillis() - timeQuerySent);
        if ((numQueries > 0) && (timeToWait > 0)) {
          setState(WAITING_FOR_RESULTS,timeToWait);
          areThereNewResults=reqLock.lock(timeToWait);
        }
        if (!areThereNewResults) {
          if (pauseForRequery(numQueries,deserializedFromDisk))           continue;
          if (numQueries < REQUERY_ATTEMPTS) {
            waitForStableConnections();
            try {
              if (manager.sendQuery(this,newRequery(numQueries)))               numQueries++;
              timeQuerySent=System.currentTimeMillis();
            }
 catch (            CantResumeException ignore) {
            }
          }
 else {
            long[] instructions=getFailedState(deserializedFromDisk,timeSpentWaiting);
            if (instructions[1] > 0) {
              setState((int)instructions[0],instructions[1]);
              reqLock.lock(instructions[1]);
              timeSpentWaiting+=System.currentTimeMillis() - currTime;
            }
 else {
              setState(GAVE_UP);
              reqLock.lock(0);
            }
          }
        }
      }
    }
 catch (    InterruptedException e) {
      if (stopped) {
        setState(ABORTED);
        manager.remove(this,false);
        return;
      }
    }
  }
}