Skip to content

Commit 14ee18f

Browse files
committed
Fix ConcurrentModificationException in getDeclaredResources and getResourcesFromProject
Add synchronized(syncResources) blocks around iterations in getDeclaredResources() and getResourcesFromProject() to prevent ConcurrentModificationException when the resources list is modified concurrently by another thread. Also changes both methods to iterate over this.resources directly (inside the sync block) instead of this.getResources() which returns an unprotected reference. Fixes #933
1 parent 8561d98 commit 14ee18f

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ public List<LockableResource> getReadOnlyResources() {
155155
/** Get declared resources, means only defined in config file (xml or JCaC yaml). */
156156
@Restricted(NoExternalUse.class)
157157
public List<LockableResource> getDeclaredResources() {
158-
ArrayList<LockableResource> declaredResources = new ArrayList<>();
159-
for (LockableResource r : this.getResources()) {
160-
if (!r.isEphemeral() && !r.isNodeResource()) {
161-
declaredResources.add(r);
158+
synchronized (syncResources) {
159+
ArrayList<LockableResource> declaredResources = new ArrayList<>();
160+
for (LockableResource r : this.resources) {
161+
if (!r.isEphemeral() && !r.isNodeResource()) {
162+
declaredResources.add(r);
163+
}
162164
}
165+
return declaredResources;
163166
}
164-
return declaredResources;
165167
}
166168

167169
// ---------------------------------------------------------------------------
@@ -225,14 +227,16 @@ public void setDeclaredResources(List<LockableResource> declaredResources) {
225227
/** Get all resources used by project. */
226228
@Restricted(NoExternalUse.class)
227229
public List<LockableResource> getResourcesFromProject(String fullName) {
228-
List<LockableResource> matching = new ArrayList<>();
229-
for (LockableResource r : this.getResources()) {
230-
String rName = r.getQueueItemProject();
231-
if (rName != null && rName.equals(fullName)) {
232-
matching.add(r);
230+
synchronized (syncResources) {
231+
List<LockableResource> matching = new ArrayList<>();
232+
for (LockableResource r : this.resources) {
233+
String rName = r.getQueueItemProject();
234+
if (rName != null && rName.equals(fullName)) {
235+
matching.add(r);
236+
}
233237
}
238+
return matching;
234239
}
235-
return matching;
236240
}
237241

238242
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)