Skip to content

Catch OverlappingFilelockException to prevent app crashes #2036

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

Merged
merged 2 commits into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;

/** Use file locking to acquire a lock that will also block other processes. */
class CrossProcessLock {
Expand Down Expand Up @@ -51,11 +52,13 @@ static CrossProcessLock acquire(Context appContext, String lockName) {
// This method blocks until it can retrieve the lock.
lock = channel.lock();
return new CrossProcessLock(channel, lock);
} catch (IOException | Error e) {
} catch (IOException | Error | OverlappingFileLockException e) {
// Certain conditions can cause file locking to fail, such as out of disk or bad permissions.
// In any case, the acquire will fail and return null instead of a held lock.
// NOTE: In Java 7 & 8, FileKey creation failure might wrap IOException into Error. See
// https://bugs.openjdk.java.net/browse/JDK-8025619 for details.
// Sometimes, an attempt to acquire the lock might throw an OverlappingFileLockException if a
// lock already exists on Android 7 & 8.
Log.e(TAG, "encountered error while creating and acquiring the lock, ignoring", e);

// Clean up any dangling resources
Expand Down