Skip to content

Commit

Permalink
Retry extract_DT_NEEDED when interrupted
Browse files Browse the repository at this point in the history
Summary: FileChannel will throw ClosedByInterruptException if it's running within a thread that is interrupted. If that happens, we need to catch the exception and retry. If not the class load failure will get memoized and any future attempt to load the same class will also fail.

Reviewed By: tophyr

Differential Revision: D21438754

fbshipit-source-id: af914c83ca60a0572cc9a6b05ed35318b69795a1
  • Loading branch information
Tom Mulcahy authored and facebook-github-bot committed May 7, 2020
1 parent 30af7ce commit a5e96b9
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions java/com/facebook/soloader/MinElf.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package com.facebook.soloader;

import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.FileChannel;

/**
Expand All @@ -32,6 +34,8 @@
*/
public final class MinElf {

private static final String TAG = "MinElf";

public static enum ISA {
NOT_SO("not_so"),
X86("x86"),
Expand Down Expand Up @@ -65,10 +69,18 @@ public String toString() {

public static String[] extract_DT_NEEDED(File elfFile) throws IOException {
FileInputStream is = new FileInputStream(elfFile);
try {
return extract_DT_NEEDED(is.getChannel());
} finally {
is.close(); // Won't throw
while (true) {
try {
return extract_DT_NEEDED(is.getChannel());
} catch (ClosedByInterruptException e) {
// Some other thread interrupted us. We need to try again. This is
// especially important since this is often used within the context of
// a static initializer. A failure here will get memoized resulting in
// all future attempts to load the same class to fail.
Log.e(TAG, "retrying extract_DT_NEEDED due to ClosedByInterruptException", e);
} finally {
is.close(); // Won't throw
}
}
}

Expand Down

0 comments on commit a5e96b9

Please sign in to comment.