Skip to content

Commit

Permalink
synchronize resource tree modification
Browse files Browse the repository at this point in the history
SearchablePluginsManager saves the state of the
resources in the workspace without acquiring a
lock, or not scheduling the job asynchronously.
Fix it by employing the ICoreRunnable scheduler.
  • Loading branch information
gireeshpunathil authored and vik-chand committed Mar 24, 2023
1 parent 91ab862 commit 4bf3e60
Showing 1 changed file with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
import java.util.stream.Collectors;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceRuleFactory;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ICoreRunnable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
Expand Down Expand Up @@ -400,36 +404,51 @@ public void removePluginModelListener(IPluginModelListener listener) {
}

private void saveStates() {
// persist state
IWorkspaceRoot root = PDECore.getWorkspace().getRoot();
IProject project = root.getProject(PROXY_PROJECT_NAME);
if (project.exists() && project.isOpen()) {
// modify the .searchable file only if there is any change
Set<String> loadedStates = loadStates();
String propertyToSave;
synchronized (fPluginIdSet) {
if (loadedStates.equals(fPluginIdSet)) {
return;
IWorkspace workspace = PDECore.getWorkspace();
ICoreRunnable runnable = monitor -> {
// persist state
IWorkspaceRoot root = PDECore.getWorkspace().getRoot();
IProject project = root.getProject(PROXY_PROJECT_NAME);
if (project.exists() && project.isOpen()) {
// modify the .searchable file only if there is any change
Set<String> loadedStates = loadStates();
String propertyToSave;
synchronized (fPluginIdSet) {
if (loadedStates.equals(fPluginIdSet)) {
return;
}
propertyToSave = fPluginIdSet.stream().collect(Collectors.joining(",")); //$NON-NLS-1$
}
propertyToSave = fPluginIdSet.stream().collect(Collectors.joining(",")); //$NON-NLS-1$
}
IFile file = project.getFile(PROXY_FILE_NAME);
Properties properties = new Properties();
properties.setProperty(KEY, propertyToSave);
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
properties.store(outStream, ""); //$NON-NLS-1$
outStream.flush();
outStream.close();
try (ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) {
if (file.exists()) {
file.setContents(inStream, true, false, new NullProgressMonitor());
} else {
file.create(inStream, true, new NullProgressMonitor());
IFile file = project.getFile(PROXY_FILE_NAME);
Properties properties = new Properties();
properties.setProperty(KEY, propertyToSave);
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
properties.store(outStream, ""); //$NON-NLS-1$
outStream.flush();
outStream.close();
try (ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) {
if (file.exists()) {
file.setContents(inStream, true, false, new NullProgressMonitor());
} else {
file.create(inStream, true, new NullProgressMonitor());
}
}
} catch (IOException e) {
PDECore.log(e);
}
} catch (IOException | CoreException e) {
PDECore.log(e);
}
};
try {
if (workspace.isTreeLocked()) {
runnable.run(null);
} else {
IResourceRuleFactory factory = workspace.getRuleFactory();
IWorkspaceRoot root = workspace.getRoot();
ISchedulingRule rule = factory.modifyRule(root);
workspace.run(runnable, rule, IWorkspace.AVOID_UPDATE, null);
}
} catch (CoreException e) {
PDECore.log(e);
}
}

Expand Down

0 comments on commit 4bf3e60

Please sign in to comment.