Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#4198] Fix race-condition when allocate from multiple-thread. #4388

Closed
wants to merge 1 commit into from

Conversation

normanmaurer
Copy link
Member

Motivation:

Fix a race condition that was introduced by f18990a that could lead to a NPE when allocate from the PooledByteBufAllocator concurrently by many threads.

Modifications:

Correctly synchronize on the PoolSubPage head.

Result:

No more race.

@normanmaurer normanmaurer self-assigned this Oct 23, 2015
@normanmaurer normanmaurer added this to the 4.0.33.Final milestone Oct 23, 2015
@normanmaurer
Copy link
Member Author

@Scottmitch @nmittler PTAL

}
// Obtain the head of the PoolSubPage pool that is owned by the PoolArena and synchronize on it.
// This is need as we may add it back and so alter the linked-list structure.
PoolSubpage<T> head = arena.findSubpagePoolHead(normCapacity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just synchronize on the arena, itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we want to minimize the overhead as much as possible. With this it is possible to allocate out of different PoolSubPage pools concurrently ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are different for different requested sizes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK as long as you're confident that this will fix the race, LGTM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I am :) You can also run the test-case that is included. It will fail before and now pass :)

@normanmaurer
Copy link
Member Author

@Scottmitch I think I addressed your concerns... Also I improved the test so we are sure we not leak any buffers.

@normanmaurer
Copy link
Member Author

@merlimat once you verified I will pull in a cut a release ASAP

@merlimat
Copy link
Contributor

@normanmaurer everything looks great, thanks!

@normanmaurer
Copy link
Member Author

@merlimat verified the fix... @Scottmitch @nmittler ready for cherry-pick ?

@nmittler
Copy link
Member

Yeah, go for it!

long sleepTime = 500;
long startTime = System.currentTimeMillis();
long endTime = startTime + runningTime;
for (; startTime < endTime; startTime += sleepTime) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are still vulnerable to overflow here. Also why switch to currentTimeMillis?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we vulnerable to overflow ? If we not have enough left after adding stuff to to System.currentTimeMillis() I think we have bigger problems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scottmitch actually I think we not even need this.... Let me update

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we do have bigger problems, but lets not make those problems worse 😄

We could use something like this:

long start = nanoTime();
long expireTime = // 30 seconds...or w/e you need

while (!isExpired(start, expireTime)) {
...
}

private static boolean isExpired(long start, long expireTime) {
  return nanoTime() - start > expireTime;
}

Motivation:

Fix a race condition that was introduced by f18990a that could lead to a NPE when allocate from the PooledByteBufAllocator concurrently by many threads.

Modifications:

Correctly synchronize on the PoolSubPage head.

Result:

No more race.
@Scottmitch
Copy link
Member

@normanmaurer - lgtm

@normanmaurer
Copy link
Member Author

Cherry-picked into 4.0 (c0fd748), 4.1 (d93f906) and master (9feba62)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants