Skip to content
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

[android] Fix NetworkOnMainThreadException in remote debugger #15246

Closed
wants to merge 4 commits into from
Closed
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 @@ -16,6 +16,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import com.facebook.infer.annotation.Assertions;
Expand All @@ -28,6 +29,7 @@ public class WebsocketJavaScriptExecutor implements JavaJSExecutor {

private static final long CONNECT_TIMEOUT_MS = 5000;
private static final int CONNECT_RETRY_COUNT = 3;
private static final String TIMEOUT_HANDLER_THREAD = "TimeoutHandlerThread";

public interface JSExecutorConnectCallback {
void onSuccess();
Expand Down Expand Up @@ -76,29 +78,36 @@ public void onFailure(Throwable cause) {

public void connect(final String webSocketServerUrl, final JSExecutorConnectCallback callback) {
final AtomicInteger retryCount = new AtomicInteger(CONNECT_RETRY_COUNT);
final HandlerThread thread = new HandlerThread(TIMEOUT_HANDLER_THREAD);
thread.start();
final Handler timeoutHandler = new Handler(thread.getLooper());
final JSExecutorConnectCallback retryProxyCallback = new JSExecutorConnectCallback() {
@Override
public void onSuccess() {
timeoutHandler.removeCallbacksAndMessages(null);
thread.quit();
callback.onSuccess();
}

@Override
public void onFailure(Throwable cause) {
timeoutHandler.removeCallbacksAndMessages(null);
if (retryCount.decrementAndGet() <= 0) {
thread.quit();
callback.onFailure(cause);
} else {
connectInternal(webSocketServerUrl, this);
connectInternal(webSocketServerUrl, this, timeoutHandler);
}
}
};
connectInternal(webSocketServerUrl, retryProxyCallback);
connectInternal(webSocketServerUrl, retryProxyCallback, timeoutHandler);
}

private void connectInternal(
String webSocketServerUrl,
final JSExecutorConnectCallback callback) {
final JSExecutorConnectCallback callback,
Handler timeoutHandler) {
final JSDebuggerWebSocketClient client = new JSDebuggerWebSocketClient();
final Handler timeoutHandler = new Handler(Looper.getMainLooper());
client.connect(
webSocketServerUrl, new JSDebuggerWebSocketClient.JSDebuggerCallback() {
// It's possible that both callbacks can fire on an error so make sure we only
Expand All @@ -111,7 +120,6 @@ public void onSuccess(@Nullable String response) {
new JSDebuggerWebSocketClient.JSDebuggerCallback() {
@Override
public void onSuccess(@Nullable String response) {
timeoutHandler.removeCallbacksAndMessages(null);
mWebSocketClient = client;
if (!didSendResult) {
callback.onSuccess();
Expand All @@ -121,7 +129,6 @@ public void onSuccess(@Nullable String response) {

@Override
public void onFailure(Throwable cause) {
timeoutHandler.removeCallbacksAndMessages(null);
if (!didSendResult) {
callback.onFailure(cause);
didSendResult = true;
Expand All @@ -132,7 +139,6 @@ public void onFailure(Throwable cause) {

@Override
public void onFailure(Throwable cause) {
timeoutHandler.removeCallbacksAndMessages(null);
if (!didSendResult) {
callback.onFailure(cause);
didSendResult = true;
Expand Down