Skip to content

Commit fb8fc86

Browse files
committed
Make ModelObject threadsafe on create and getObjectPropertyValue
Partial fix to issue #167 Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent da021df commit fb8fc86

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

src/main/java/org/spdx/library/model/ModelObject.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,15 @@ public ModelObject(IModelStore modelStore, String documentUri, String id, @Nulla
201201
}
202202
} else {
203203
if (create) {
204-
modelStore.create(documentUri, id, getType());
204+
IModelStoreLock lock = enterCriticalSection(false);
205+
// re-check since previous check was done outside of the lock
206+
try {
207+
if (!modelStore.exists(documentUri, id)) {
208+
modelStore.create(documentUri, id, getType());
209+
}
210+
} finally {
211+
lock.unlock();
212+
}
205213
} else {
206214
throw new SpdxIdNotFoundException(id+" does not exist in document "+documentUri);
207215
}
@@ -337,13 +345,19 @@ protected Optional<Object> getObjectPropertyValue(String propertyName) throws In
337345
*/
338346
protected static Optional<Object> getObjectPropertyValue(IModelStore stModelStore, String stDocumentUri,
339347
String stId, String propertyName, ModelCopyManager copyManager) throws InvalidSPDXAnalysisException {
340-
if (!stModelStore.exists(stDocumentUri, stId)) {
341-
return Optional.empty();
342-
} else if (stModelStore.isCollectionProperty(stDocumentUri, stId, propertyName)) {
343-
return Optional.of(new ModelCollection<>(stModelStore, stDocumentUri, stId, propertyName, copyManager, null));
344-
} else {
345-
return ModelStorageClassConverter.optionalStoredObjectToModelObject(stModelStore.getValue(stDocumentUri, stId, propertyName),
346-
stDocumentUri, stModelStore, copyManager);
348+
IModelStoreLock lock = stModelStore.enterCriticalSection(stDocumentUri, false);
349+
// NOTE: we use a write lock since the ModelStorageClassConverter may end up creating objects in the store
350+
try {
351+
if (!stModelStore.exists(stDocumentUri, stId)) {
352+
return Optional.empty();
353+
} else if (stModelStore.isCollectionProperty(stDocumentUri, stId, propertyName)) {
354+
return Optional.of(new ModelCollection<>(stModelStore, stDocumentUri, stId, propertyName, copyManager, null));
355+
} else {
356+
return ModelStorageClassConverter.optionalStoredObjectToModelObject(stModelStore.getValue(stDocumentUri, stId, propertyName),
357+
stDocumentUri, stModelStore, copyManager);
358+
}
359+
} finally {
360+
lock.unlock();
347361
}
348362
}
349363

0 commit comments

Comments
 (0)