### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/CompilationUnit.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java,v retrieving revision 1.231 diff -u -r1.231 CompilationUnit.java --- model/org/eclipse/jdt/internal/core/CompilationUnit.java 20 Jun 2006 11:33:14 -0000 1.231 +++ model/org/eclipse/jdt/internal/core/CompilationUnit.java 21 Jun 2006 10:09:53 -0000 @@ -1021,43 +1021,50 @@ protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException { // create buffer + BufferManager bufManager = getBufferManager(); boolean isWorkingCopy = isWorkingCopy(); IBuffer buffer = isWorkingCopy ? this.owner.createBuffer(this) - : BufferManager.getDefaultBufferManager().createBuffer(this); + : bufManager.createBuffer(this); if (buffer == null) return null; - // set the buffer source - if (buffer.getCharacters() == null) { - if (isWorkingCopy) { - ICompilationUnit original; - if (!isPrimary() - && (original = new CompilationUnit((PackageFragment)getParent(), getElementName(), DefaultWorkingCopyOwner.PRIMARY)).isOpen()) { - buffer.setContents(original.getSource()); - } else { - IFile file = (IFile)getResource(); - if (file == null || !file.exists()) { - // initialize buffer with empty contents - buffer.setContents(CharOperation.NO_CHAR); + // synchronize to ensure that 2 threads are not putting 2 different buffers at the same time + // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=146331 + synchronized(bufManager) { + IBuffer existingBuffer = bufManager.getBuffer(this); + if (existingBuffer != null) + return existingBuffer; + + // set the buffer source + if (buffer.getCharacters() == null) { + if (isWorkingCopy) { + ICompilationUnit original; + if (!isPrimary() + && (original = new CompilationUnit((PackageFragment)getParent(), getElementName(), DefaultWorkingCopyOwner.PRIMARY)).isOpen()) { + buffer.setContents(original.getSource()); } else { - buffer.setContents(Util.getResourceContentsAsCharArray(file)); + IFile file = (IFile)getResource(); + if (file == null || !file.exists()) { + // initialize buffer with empty contents + buffer.setContents(CharOperation.NO_CHAR); + } else { + buffer.setContents(Util.getResourceContentsAsCharArray(file)); + } } + } else { + IFile file = (IFile)this.getResource(); + if (file == null || !file.exists()) throw newNotPresentException(); + buffer.setContents(Util.getResourceContentsAsCharArray(file)); } - } else { - IFile file = (IFile)this.getResource(); - if (file == null || !file.exists()) throw newNotPresentException(); - buffer.setContents(Util.getResourceContentsAsCharArray(file)); } - } - - // add buffer to buffer cache - BufferManager bufManager = getBufferManager(); - bufManager.addBuffer(buffer); - - // listen to buffer changes - buffer.addBufferChangedListener(this); + // add buffer to buffer cache + bufManager.addBuffer(buffer); + + // listen to buffer changes + buffer.addBufferChangedListener(this); + } return buffer; } protected void openParent(Object childInfo, HashMap newElements, IProgressMonitor pm) throws JavaModelException {