From bba56be3a66f4bb7e9fe15634baab4fca89a3806 Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Fri, 17 Jul 2026 02:44:29 +0100 Subject: [PATCH 1/2] Handle race condition when insert id to list Signed-off-by: Arthit Suriyawongkul --- src/main/java/org/spdx/storage/simple/StoredTypedItem.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/spdx/storage/simple/StoredTypedItem.java b/src/main/java/org/spdx/storage/simple/StoredTypedItem.java index 9cde8f3b4..9c6efdd72 100644 --- a/src/main/java/org/spdx/storage/simple/StoredTypedItem.java +++ b/src/main/java/org/spdx/storage/simple/StoredTypedItem.java @@ -242,8 +242,11 @@ public boolean addValueToList(PropertyDescriptor propertyDescriptor, Object valu List list = idValueMap.get(id); if (list == null) { // handle the very small window where this may have gotten removed - list = new ArrayList<>(); - idValueMap.putIfAbsent(id, list); + List newList = new ArrayList<>(); + List existingList = idValueMap.putIfAbsent(id, newList); + // another thread may have won the race to insert; + // use whichever list actually ended up in the map + list = existingList != null ? existingList : newList; } return list.add(value); } catch (Exception ex) { From 2982e5961ddc1f727bfab6406683bb83bde5cc7f Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Sat, 18 Jul 2026 21:52:19 +0100 Subject: [PATCH 2/2] Use atomic computeIfAbsent instead of putIfAbsent Signed-off-by: Arthit Suriyawongkul --- .../java/org/spdx/storage/simple/StoredTypedItem.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/main/java/org/spdx/storage/simple/StoredTypedItem.java b/src/main/java/org/spdx/storage/simple/StoredTypedItem.java index 9c6efdd72..6e5390a45 100644 --- a/src/main/java/org/spdx/storage/simple/StoredTypedItem.java +++ b/src/main/java/org/spdx/storage/simple/StoredTypedItem.java @@ -238,16 +238,7 @@ public boolean addValueToList(PropertyDescriptor propertyDescriptor, Object valu } else { id = NO_ID_ID; } - idValueMap.putIfAbsent(id, new ArrayList<>()); - List list = idValueMap.get(id); - if (list == null) { - // handle the very small window where this may have gotten removed - List newList = new ArrayList<>(); - List existingList = idValueMap.putIfAbsent(id, newList); - // another thread may have won the race to insert; - // use whichever list actually ended up in the map - list = existingList != null ? existingList : newList; - } + List list = idValueMap.computeIfAbsent(id, key -> new ArrayList<>()); return list.add(value); } catch (Exception ex) { throw new SpdxInvalidTypeException("Invalid list type for "+propertyDescriptor);