This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new c7d3f1fa9 Prune FileLockNamedLock thread entries and align the 
composite unlock error (#2300)
c7d3f1fa9 is described below

commit c7d3f1fa95ee772ea42960ba1182de3f9017b7c5
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Nov 16 20:41:25 2026 +0100

    Prune FileLockNamedLock thread entries and align the composite unlock error 
(#2300)
    
    threadSteps kept every thread that ever used the lock; CompositeNamedLock 
threw
    NoSuchElementException instead of the IllegalStateException the other locks 
throw.
---
 .../aether/named/support/CompositeNamedLock.java   |  3 ++
 .../aether/named/support/FileLockNamedLock.java    | 41 ++++++++++++++++++----
 .../aether/named/NamedLockFactoryTestSupport.java  | 13 +++++++
 3 files changed, 50 insertions(+), 7 deletions(-)

diff --git 
a/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/CompositeNamedLock.java
 
b/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/CompositeNamedLock.java
index f4a1e7058..eb6f7b69a 100644
--- 
a/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/CompositeNamedLock.java
+++ 
b/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/CompositeNamedLock.java
@@ -101,6 +101,9 @@ public final class CompositeNamedLock extends 
NamedLockSupport {
 
     @Override
     protected void doUnlock() {
+        if (steps.isEmpty()) {
+            throw new IllegalStateException("Wrong API usage: unlock without 
lock");
+        }
         unlockAll(steps.pop());
     }
 
diff --git 
a/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/FileLockNamedLock.java
 
b/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/FileLockNamedLock.java
index b584805e1..b91d4c679 100644
--- 
a/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/FileLockNamedLock.java
+++ 
b/maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/FileLockNamedLock.java
@@ -87,12 +87,36 @@ public final class FileLockNamedLock extends 
NamedLockSupport {
 
     @Override
     protected boolean doLockShared(final long time, final TimeUnit unit) 
throws InterruptedException {
-        return retry(time, unit, RETRY_SLEEP_MILLIS, () -> 
doLockSharedPerform(time, unit), null, false);
+        boolean locked = retry(time, unit, RETRY_SLEEP_MILLIS, () -> 
doLockSharedPerform(time, unit), null, false);
+        if (!locked) {
+            forgetThreadWithoutSteps();
+        }
+        return locked;
     }
 
     @Override
     protected boolean doLockExclusively(final long time, final TimeUnit unit) 
throws InterruptedException {
-        return retry(time, unit, RETRY_SLEEP_MILLIS, () -> 
doLockExclusivelyPerform(time, unit), null, false);
+        boolean locked = retry(time, unit, RETRY_SLEEP_MILLIS, () -> 
doLockExclusivelyPerform(time, unit), null, false);
+        if (!locked) {
+            forgetThreadWithoutSteps();
+        }
+        return locked;
+    }
+
+    /**
+     * Drops the current thread's entry if it holds no step, so that the map 
does not retain threads that no longer
+     * use this lock.
+     */
+    private void forgetThreadWithoutSteps() {
+        criticalRegion.lock();
+        try {
+            Deque<Boolean> steps = threadSteps.get(Thread.currentThread());
+            if (steps != null && steps.isEmpty()) {
+                threadSteps.remove(Thread.currentThread());
+            }
+        } finally {
+            criticalRegion.unlock();
+        }
     }
 
     private Boolean doLockSharedPerform(final long time, final TimeUnit unit) 
throws InterruptedException {
@@ -180,11 +204,14 @@ public final class FileLockNamedLock extends 
NamedLockSupport {
                 throw new IllegalStateException("Wrong API usage: unlock 
without lock");
             }
             steps.pop();
-            if (steps.isEmpty() && !anyOtherThreadHasSteps()) {
-                try {
-                    fileLockRef.getAndSet(null).release();
-                } catch (IOException e) {
-                    throw new UncheckedIOException(e);
+            if (steps.isEmpty()) {
+                threadSteps.remove(Thread.currentThread());
+                if (!anyOtherThreadHasSteps()) {
+                    try {
+                        fileLockRef.getAndSet(null).release();
+                    } catch (IOException e) {
+                        throw new UncheckedIOException(e);
+                    }
                 }
             }
         } finally {
diff --git 
a/maven-resolver-named-locks/src/test/java/org/eclipse/aether/named/NamedLockFactoryTestSupport.java
 
b/maven-resolver-named-locks/src/test/java/org/eclipse/aether/named/NamedLockFactoryTestSupport.java
index fcc6fc65f..22c453932 100644
--- 
a/maven-resolver-named-locks/src/test/java/org/eclipse/aether/named/NamedLockFactoryTestSupport.java
+++ 
b/maven-resolver-named-locks/src/test/java/org/eclipse/aether/named/NamedLockFactoryTestSupport.java
@@ -18,6 +18,7 @@
  */
 package org.eclipse.aether.named;
 
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.concurrent.CountDownLatch;
@@ -96,6 +97,18 @@ public abstract class NamedLockFactoryTestSupport {
         });
     }
 
+    @Test
+    void unlockWoLockComposite(TestInfo testInfo) {
+        assertThrows(IllegalStateException.class, () -> {
+            final String name = lockName(testInfo).iterator().next().name();
+            final Collection<NamedLockKey> keys =
+                    Arrays.asList(NamedLockKey.of(name + "-1"), 
NamedLockKey.of(name + "-2"));
+            try (NamedLock one = namedLockFactory.getLock(keys)) {
+                one.unlock();
+            }
+        });
+    }
+
     @Test
     void wwBoxing(TestInfo testInfo) throws InterruptedException {
         final Collection<NamedLockKey> keys = lockName(testInfo);

Reply via email to