Skip to content

Commit

Permalink
fix bug that messes up the max usage count when the entries' max usag…
Browse files Browse the repository at this point in the history
…e counter overflows

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Dec 1, 2020
1 parent df3a326 commit 0ff1bfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion jetty-util/src/main/java/org/eclipse/jetty/util/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ public class Entry
this.state = new AtomicBiInteger(Integer.MIN_VALUE, 0);
}

// for testing only
void setUsageCount(int usageCount)
{
this.state.getAndSetHi(usageCount);
}

/** Enable a reserved entry {@link Entry}.
* An entry returned from the {@link #reserve(int)} method must be enabled with this method,
* once and only once, before it is usable by the pool.
Expand Down Expand Up @@ -527,7 +533,9 @@ boolean tryAcquire()
if (closed || multiplexingCount >= maxMultiplex || (currentMaxUsageCount > 0 && usageCount >= currentMaxUsageCount))
return false;

if (state.compareAndSet(encoded, usageCount + 1, multiplexingCount + 1))
// Prevent overflowing the usage counter by capping it at Integer.MAX_VALUE.
int newUsageCount = usageCount == Integer.MAX_VALUE ? Integer.MAX_VALUE : usageCount + 1;
if (state.compareAndSet(encoded, newUsageCount, multiplexingCount + 1))
return true;
}
}
Expand Down
18 changes: 18 additions & 0 deletions jetty-util/src/test/java/org/eclipse/jetty/util/PoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,24 @@ public void testUsageCountAfterReachingMaxMultiplexLimit(Factory factory)
assertThat(e1.getUsageCount(), is(2));
}

@ParameterizedTest
@MethodSource(value = "strategy")
public void testDynamicMaxUsageCountChangeOverflowMaxInt(Factory factory)
{
Pool<String> pool = factory.getPool(1);
Pool<String>.Entry entry = pool.reserve(-1);
entry.enable("aaa", false);
entry.setUsageCount(Integer.MAX_VALUE);

Pool<String>.Entry acquired1 = pool.acquire();
assertThat(acquired1, notNullValue());
assertThat(pool.release(acquired1), is(true));

pool.setMaxUsageCount(1);
Pool<String>.Entry acquired2 = pool.acquire();
assertThat(acquired2, nullValue());
}

@Test
public void testConfigLimits()
{
Expand Down

0 comments on commit 0ff1bfd

Please sign in to comment.