From 37f6417e3dfa4e576a51db19de66ab2baf8441aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Fri, 6 Feb 2026 21:06:41 +0100 Subject: [PATCH] fix(library-service): prevent concurrent library scans with a tracking set (#2637) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../service/library/LibraryService.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/booklore-api/src/main/java/org/booklore/service/library/LibraryService.java b/booklore-api/src/main/java/org/booklore/service/library/LibraryService.java index 66fe144de..fd2037176 100644 --- a/booklore-api/src/main/java/org/booklore/service/library/LibraryService.java +++ b/booklore-api/src/main/java/org/booklore/service/library/LibraryService.java @@ -38,6 +38,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @Slf4j @@ -45,6 +46,17 @@ import java.util.stream.Collectors; @AllArgsConstructor public class LibraryService { + private static final Set scanningLibraries = ConcurrentHashMap.newKeySet(); + + /** + * Checks whether a library is currently being scanned. + * Can be used by other components (e.g., file watcher) to avoid processing + * files while a full scan is in progress. + */ + public static boolean isLibraryScanning(long libraryId) { + return scanningLibraries.contains(libraryId); + } + private final LibraryRepository libraryRepository; private final LibraryPathRepository libraryPathRepository; private final BookRepository bookRepository; @@ -125,10 +137,16 @@ public class LibraryService { if (!newPaths.isEmpty()) { SecurityContextVirtualThread.runWithSecurityContext(() -> { + if (!scanningLibraries.add(libraryId)) { + log.warn("Library {} is already being scanned, skipping duplicate process request", libraryId); + return; + } try { libraryProcessingService.processLibrary(libraryId); } catch (InvalidDataAccessApiUsageException e) { log.debug("InvalidDataAccessApiUsageException - Library id: {}", libraryId); + } finally { + scanningLibraries.remove(libraryId); } log.info("Parsing task completed!"); }); @@ -169,10 +187,16 @@ public class LibraryService { } SecurityContextVirtualThread.runWithSecurityContext(() -> { + if (!scanningLibraries.add(libraryId)) { + log.warn("Library {} is already being scanned, skipping duplicate process request", libraryId); + return; + } try { libraryProcessingService.processLibrary(libraryId); } catch (InvalidDataAccessApiUsageException e) { log.debug("InvalidDataAccessApiUsageException - Library id: {}", libraryId); + } finally { + scanningLibraries.remove(libraryId); } log.info("Parsing task completed!"); }); @@ -184,6 +208,10 @@ public class LibraryService { libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); SecurityContextVirtualThread.runWithSecurityContext(() -> { + if (!scanningLibraries.add(libraryId)) { + log.warn("Library {} is already being scanned, skipping duplicate rescan request", libraryId); + return; + } try { RescanLibraryContext context = RescanLibraryContext.builder() .libraryId(libraryId) @@ -193,6 +221,8 @@ public class LibraryService { log.debug("InvalidDataAccessApiUsageException - Library id: {}", libraryId); } catch (IOException e) { log.error("Error while parsing library books", e); + } finally { + scanningLibraries.remove(libraryId); } log.info("Parsing task completed!"); });