# How to Destroy a Locking Process

In rare cases an application using the HERE SDK may crash due to a Storage.LevelDB error or the initialization of the HERE SDK may fail with a failedToLockCacheFolder error.

This may happen when a second instance of a SDKNativeEngine is created with the same access_key_id as an existing one. Then the local map cache directory is locked by the current process.

This locked cache folder crash can be fixed by setting the following option before initializing the HERE SDK:

SDKOptions sdkOptions = SDKOptions.withAccessKeySecret(accessKeyId, accessKeySecret);
// Defaults to SDKOptions.ActionOnCacheLock.waitLockingAppFinish.
sdkOptions.actionOnCacheLock = SDKOptions.ActionOnCacheLock.killLockingApp;

// Now, use these options to initialize the HERE SDK and destroy a locking process (if any).
try {
await SDKNativeEngine.makeSharedInstance(sdkOptions);
} on InstantiationException {
throw Exception("Failed to initialize the HERE SDK.");
}


If there is a cache lock, this will kill the locking process and the app can start normally. If no cache lock occurred, the code will only initialize the HERE SDK. Beside killLockingApp, also the following values can be set:

• noAction: Nothing will be done.
• waitLockingAppFinish: The app will wait until the locking process has ended. Note that this may happen never. This is the default behavior.

## Additional Info

Alternatively, the following snippet can be used to detect and to prevent the cache lock issue. This has the same effect as killLockingApp as makeSharedInstance() calls internally destroyLockingProcess() when this flag is set, but it provides more flexibility:

// Check if there is a lock on the cache.
Integer processID = LockingProcess.getLockingProcessId(sdkOptions);
if (processID != null) {
// Warning: The cache is locked.

// This may kill another process that is trying to attempt to lock the current map cache.
// If this happens, the method will try to gracefully repair the cache.
// If no problem was detected, the method will do nothing.
long maxTimeoutInMilliseconds = 300;
LockingProcess.destroyLockingProcess(sdkOptions, maxTimeoutInMilliseconds);
}

// Now, proceed to initialize the HERE SDK.


getLockingProcessId() provides the process ID of the locking process from the Android OS. If no lock occurred, the method will return null.

By calling destroyLockingProcess() the problem will be gone. You can keep this code to solve sporadic crashes at application start. If no cache lock is detected, destroyLockingProcess() will do nothing.