Program: 26378
Project: 26378
Buggy source:
@Override public void ensureSynchronized(){
  TernDoc doc=new TernDoc();
  writeLock.lock();
  try {
    syncedFilesPerPath.clear();
    Set<String> synced=new HashSet<String>(sentFiles.keySet());
    Set<String> toRefreshLocal=new HashSet<String>();
synchronized (toRefresh) {
      toRefreshLocal.addAll(toRefresh);
      toRefresh.clear();
    }
    synced.removeAll(toRefreshLocal);
    long totalSize=0;
    for (    String file : synced) {
      totalSize+=sentFiles.get(file).length();
    }
    for (    ITernScriptPath path : getProject().getScriptPaths()) {
      Set<String> perPath=new HashSet<String>();
      syncedFilesPerPath.put(path,perPath);
      for (      ITernScriptResource resource : path.getScriptResources()) {
        if (totalSize >= MAX_ALLOWED_SIZE) {
          sizeExceeded();
          break;
        }
        ITernFile file=resource.getFile();
        if (file == null) {
          continue;
        }
        String name=file.getFullName(getProject());
        perPath.add(name);
        if (!synced.contains(name)) {
          try {
            TernFile tf=file.toTernServerFile(getProject());
            doc.addFile(tf);
            synced.add(name);
            totalSize+=tf.getText().length();
          }
 catch (          IOException e) {
            getProject().handleException(e);
          }
        }
      }
    }
    toRefreshLocal.removeAll(synced);
    for (    String toRemove : toRefreshLocal) {
      doc.addFile(toRemove,"String_Node_Str",null,null);
    }
    sendFiles(doc);
  }
  finally {
    writeLock.unlock();
  }
}

Fixed version (ground truth):
@Override public void ensureSynchronized(){
  TernDoc doc=new TernDoc();
  writeLock.lock();
  try {
    if (project.getTernServer() != targetServer) {
      targetServer=project.getTernServer();
      cleanIndexedFiles();
    }
    syncedFilesPerPath.clear();
    Set<String> synced=new HashSet<String>(sentFiles.keySet());
    Set<String> toRefreshLocal=new HashSet<String>();
synchronized (toRefresh) {
      toRefreshLocal.addAll(toRefresh);
      toRefresh.clear();
    }
    synced.removeAll(toRefreshLocal);
    long totalSize=0;
    for (    String file : synced) {
      totalSize+=sentFiles.get(file).length();
    }
    for (    ITernScriptPath path : getProject().getScriptPaths()) {
      Set<String> perPath=new HashSet<String>();
      syncedFilesPerPath.put(path,perPath);
      for (      ITernScriptResource resource : path.getScriptResources()) {
        if (totalSize >= MAX_ALLOWED_SIZE) {
          sizeExceeded();
          break;
        }
        ITernFile file=resource.getFile();
        if (file == null) {
          continue;
        }
        String name=file.getFullName(getProject());
        perPath.add(name);
        if (!synced.contains(name)) {
          try {
            TernFile tf=file.toTernServerFile(getProject());
            doc.addFile(tf);
            synced.add(name);
            totalSize+=tf.getText().length();
          }
 catch (          IOException e) {
            getProject().handleException(e);
          }
        }
      }
    }
    toRefreshLocal.removeAll(synced);
    for (    String toRemove : toRefreshLocal) {
      doc.addFile(toRemove,"String_Node_Str",null,null);
    }
    sendFiles(doc);
  }
  finally {
    writeLock.unlock();
  }
}