Skip to content

Race condition in acquireLock #81

@nicktindall

Description

@nicktindall

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions