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

Race condition in acquireLock #81

Closed
nicktindall opened this issue Oct 17, 2021 · 1 comment
Closed

Race condition in acquireLock #81

nicktindall opened this issue Oct 17, 2021 · 1 comment

Comments

@nicktindall
Copy link
Contributor

nicktindall commented Oct 17, 2021

When multiple processes are attempting to acquire the same lock, the check to see if the core can be reserved (canReserve) and the subsequent locking of the core (updateLockForCurrentThread) are done as two separate steps.

if (required.canReserve(true) && anyStrategyMatches(cpuId, cpuId, strategies)) {
updateLockForCurrentThread(bind, required, false);
return required;
}

The locking of the core logs and swallows exceptions thrown in the process of locking, meaning if two threads pass the canReserve check, while it's reservable they will both go on to "lock" the core:

static void updateCpu(int cpu) {
if (!canOSSupportOperation())
return;
try {
replacePid(cpu, getPID());
} catch (IOException e) {
LOGGER.warn("Failed to update lock file for cpu " + cpu, e);
e.printStackTrace();
}
}

Meaning you end up with multiple threads locked to a single core. The check and lock should be atomic, that will prevent this scenario.

nicktindall added a commit that referenced this issue Oct 17, 2021
nicktindall added a commit that referenced this issue Oct 20, 2021
@hft-team-city
Copy link
Contributor

Released in Java-Thread-Affinity-3.21ea81, BOM-2.22ea49

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

No branches or pull requests

2 participants