Skip to content

Commit

Permalink
Magazines must be freed under the expand lock (netty#14336)
Browse files Browse the repository at this point in the history
Motivation:
It's unlikely, but possible, for our freeing of magazines to run
concurrently with magazine expansion. If that happens, we might not free
all magazines.

Modification:
Free the magazines under the expansion lock, and short-circuit the
magazine expansion if the allocator has been freed.

Result:
Fixes a data race that could leave magazines and their chunks un-freed.
  • Loading branch information
chrisvest authored Sep 17, 2024
1 parent afb475d commit 2b7de5a
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private boolean tryExpandMagazines(int currentLength) {
if (writeLock != 0) {
try {
Magazine[] mags = magazines;
if (mags.length >= MAX_STRIPES || mags.length > currentLength) {
if (mags.length >= MAX_STRIPES || mags.length > currentLength || freed) {
return true;
}
int preferredChunkSize = mags[0].sharedPrefChunkSize;
Expand Down Expand Up @@ -335,8 +335,14 @@ protected void finalize() throws Throwable {

private void free() {
freed = true;
for (Magazine magazine : magazines) {
magazine.free();
long stamp = magazineExpandLock.writeLock();
try {
Magazine[] mags = magazines;
for (Magazine magazine : mags) {
magazine.free();
}
} finally {
magazineExpandLock.unlockWrite(stamp);
}
for (;;) {
Chunk chunk = centralQueue.poll();
Expand Down

0 comments on commit 2b7de5a

Please sign in to comment.