From cf4d3ef5f5e7989edd75b2b7439813c88656cf88 Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Tue, 10 Sep 2024 09:08:16 -0700 Subject: [PATCH] Use write locks in license store to avoid deadlocks Signed-off-by: Gary O'Neall --- .../SpdxListedLicenseModelStore.java | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseModelStore.java b/src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseModelStore.java index 046b4c57..6fcf0830 100644 --- a/src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseModelStore.java +++ b/src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseModelStore.java @@ -302,7 +302,7 @@ public void create(TypedValue typedValue) throws InvalidSPDXAnalysisException { @Override public List getPropertyValueDescriptors(String objectUri) throws InvalidSPDXAnalysisException { String id = objectUriToId(objectUri); - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { LicenseJson license = fetchLicenseJson(licenseIds.get(id.toLowerCase())); @@ -323,7 +323,7 @@ public List getPropertyValueDescriptors(String objectUri) th throw new SpdxIdNotFoundException("ID "+id+" is not a listed license ID. crossRef ID nor a listed exception ID"); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } } @@ -479,7 +479,7 @@ public void setValue(String objectUri, PropertyDescriptor propertyDescriptor, Ob boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -489,7 +489,7 @@ public void setValue(String objectUri, PropertyDescriptor propertyDescriptor, Ob crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -518,7 +518,7 @@ public void clearValueCollection(String objectUri, PropertyDescriptor propertyDe boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -528,7 +528,7 @@ public void clearValueCollection(String objectUri, PropertyDescriptor propertyDe crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -557,7 +557,7 @@ public boolean addValueToCollection(String objectUri, PropertyDescriptor propert boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -567,7 +567,7 @@ public boolean addValueToCollection(String objectUri, PropertyDescriptor propert crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -614,7 +614,7 @@ public boolean removeValueFromCollection(String objectUri, PropertyDescriptor pr boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -624,7 +624,7 @@ public boolean removeValueFromCollection(String objectUri, PropertyDescriptor pr crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -674,7 +674,7 @@ public Iterator listValues(String objectUri, PropertyDescriptor property boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -684,7 +684,7 @@ public Iterator listValues(String objectUri, PropertyDescriptor property crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -760,7 +760,7 @@ public Optional getValue(String objectUri, PropertyDescriptor propertyDe boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -770,7 +770,7 @@ public Optional getValue(String objectUri, PropertyDescriptor propertyDe crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (SpdxConstantsV3.PROP_CREATION_INFO.equals(propertyDescriptor) && (isLicenseId || isExceptionId)) { return Optional.of(licenseCreationInfo.getTypedValue()); @@ -888,7 +888,7 @@ public Optional getTypedValue(String objectUri) throws InvalidSPDXAn //NOTE: We only return the SPDX 3.0 version of the typed value, SPDX 2.X versions are also supported // but there is no API to specify the version of the typedValue String id = objectUriToId(objectUri); - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { return Optional.of(new TypedValue(objectUri, SpdxConstantsV3.EXPANDED_LICENSING_LISTED_LICENSE, SpdxConstantsV3.MODEL_SPEC_VERSION)); @@ -905,7 +905,7 @@ public Optional getTypedValue(String objectUri) throws InvalidSPDXAn return Optional.empty(); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } } @@ -915,7 +915,7 @@ public void removeProperty(String objectUri, PropertyDescriptor propertyDescript boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -925,7 +925,7 @@ public void removeProperty(String objectUri, PropertyDescriptor propertyDescript crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -949,7 +949,7 @@ public void removeProperty(String objectUri, PropertyDescriptor propertyDescript public Stream getAllItems(String documentUri, @Nullable String typeFilter) throws InvalidSPDXAnalysisException { Objects.requireNonNull(typeFilter, "Type filter can not be null"); - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { List allItems = new ArrayList(); if (SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE.equals(typeFilter)) { @@ -985,7 +985,7 @@ public Stream getAllItems(String documentUri, @Nullable String typeF } return Collections.unmodifiableList(allItems).stream(); } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } } @@ -998,7 +998,7 @@ public int collectionSize(String objectUri, PropertyDescriptor propertyDescripto boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -1008,7 +1008,7 @@ public int collectionSize(String objectUri, PropertyDescriptor propertyDescripto crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -1036,7 +1036,7 @@ public boolean collectionContains(String objectUri, PropertyDescriptor propertyD boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -1046,7 +1046,7 @@ public boolean collectionContains(String objectUri, PropertyDescriptor propertyD crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -1083,7 +1083,7 @@ public boolean isCollectionMembersAssignableTo(String objectUri, PropertyDescrip boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -1093,7 +1093,7 @@ public boolean isCollectionMembersAssignableTo(String objectUri, PropertyDescrip crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -1121,7 +1121,7 @@ public boolean isPropertyValueAssignableTo(String objectUri, PropertyDescriptor boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -1131,7 +1131,7 @@ public boolean isPropertyValueAssignableTo(String objectUri, PropertyDescriptor crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id); @@ -1158,7 +1158,7 @@ public boolean isCollectionProperty(String objectUri, PropertyDescriptor propert boolean isLicenseId = false; boolean isExceptionId = false; CrossRefJson crossRef = null; - listedLicenseModificationLock.readLock().lock(); + listedLicenseModificationLock.writeLock().lock(); try { if (licenseIds.containsKey(id.toLowerCase())) { isLicenseId = true; @@ -1168,7 +1168,7 @@ public boolean isCollectionProperty(String objectUri, PropertyDescriptor propert crossRef = crossRefs.get(id); } } finally { - listedLicenseModificationLock.readLock().unlock(); + listedLicenseModificationLock.writeLock().unlock(); } if (isLicenseId) { LicenseJson license = fetchLicenseJson(id);