android-build-an-app-architecture-components icon indicating copy to clipboard operation
android-build-an-app-architecture-components copied to clipboard

Race Condition in SunshineRepository.getInstance

Open ronykrell opened this issue 6 years ago • 0 comments
trafficstars

There is a race condition in the getInstance method in SunshineRepository.java.

Imagine there are 2 threads - A and B - both calling getInstance() and that sInstance is null to start with

Thread A Checks if sInstance is null. Returns True

Thread B Checks if sInstance is null. Returns True

Thread A Obtains the lock and creates a new instance for sInstance. Releases lock

Thread B Remember Thread B already checked if sInstance was null earlier. Obtains the lock and creates a new instance for sInstance. Releases lock

There should be a second null check inside the synchronized block.

ronykrell avatar Mar 28 '19 00:03 ronykrell