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

[Expo-go][Android] Fix Debugger #849

Merged
merged 7 commits into from
Dec 17, 2024
Merged

[Expo-go][Android] Fix Debugger #849

merged 7 commits into from
Dec 17, 2024

Conversation

filip131311
Copy link
Collaborator

@filip131311 filip131311 commented Dec 12, 2024

This PR solves an issue with new debugger not being attached on android in some scenarios. In #750 we introduced a way of ignoring expo go host runtime and selecting the second one when project was expo go, but for some reason the behavior of expo go runtimes seems to be inconsistent across platforms and versions and sometime the host runtime does not exist.

The following is the result of {metro}/json/list request for android expo go application in expo version: 52.0.18:

[
  {
    "id": "5ca976e985fc33959628c11becb91a7dc7842a8f-1",
    "title": "React Native Bridgeless [C++ connection]",
    "description": "host.exp.exponent",
    "type": "node",
    "devtoolsFrontendUrl": "devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=192.168.18.153%3A8081%2Finspector%2Fdebug%3Fdevice%3D5ca976e985fc33959628c11becb91a7dc7842a8f%26page%3D1",
    "webSocketDebuggerUrl": "ws://192.168.18.153:8081/inspector/debug?device=5ca976e985fc33959628c11becb91a7dc7842a8f&page=1",
    "deviceName": "sdk_gphone64_arm64 - 15 - API 35",
    "reactNative": {
      "logicalDeviceId": "5ca976e985fc33959628c11becb91a7dc7842a8f",
      "capabilities": {
        "prefersFuseboxFrontend": true,
        "nativeSourceCodeFetching": false,
        "nativePageReloads": true
      }
    }
  }
]

Solution

To solve the problem we introduce a new way of checking if the runtime belongs to the host application based on functionality introduced in this PR.

Additional changes

Additionally testing of expo go applications on android reveled that if application is killed, without closing the host process and then lunched again, the old runtime does not leave the {metro}/json/list endpoint. This scenario is unlikely as the user would have to do that by hand on the emulator, but to avoid problems with this bug in the future in case of expo go applications we search for the correct runtime starting form the end of the array.

How Has This Been Tested:

Run booth expo go and expo prebuilt bare application on both ios and android.

Copy link

vercel bot commented Dec 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
radon-ide ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 11:11am

});

ws.on("error", (e) => {
Logger.debug(`[evaluateJsFromCdpAsync] Failed to connect ${wsUrl.toString()}`, e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this happen only when the connection fails? It's a bit misleading.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we attach an error message with more details and it is true that we failed to connect

@@ -292,6 +292,82 @@ export class Metro implements Disposable {
ws.close();
}

private async evaluateJsFromCdp(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is some generic method to call JS through CDP, shouldn't we put it in some helper instead of as part of metro?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was my initial approach but unfortunately we need our uniq metro port that is not passed through the metro endpoints.

source: string,
timeoutMs: number = 2000
): Promise<string | undefined> {
const REQUEST_ID = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is a reason why part of the variables is declared in the promise and rest outside, is there any reason?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is no reason to be defined inside the promise as for example websocket connection needs to be opened inside i lest it outside and it seems a good pattern. (I moved timeoutHandle declaration inside the promise now as it was only an empty declaration)

}
settled = true;
clearTimeout(timeoutHandle);
ws.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

             settled = true;
             clearTimeout(timeoutHandle);
             ws.close();

It repeated x times, let's extract it to some method

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx nice catch

timeoutHandle = setTimeout(() => {
Logger.debug(`[evaluateJsFromCdpAsync] Request timeout from ${wsUrl.toString()}`);
reject(new Error("Request timeout"));
settled = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds redundant. Shouldn't you just close the websocket, and the on('close') with settled will decide if it's "settled" or not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no if we did that we wouldn't know the close reason in on(close) and we want to log that

Copy link
Contributor

@maciekstosio maciekstosio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nitpick, the rest looks good, thanks for the changes!

}
} catch (e) {
reject(e);
settleConnection();
Copy link
Contributor

@maciekstosio maciekstosio Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You always settle the connection so it can go to finally or just after try catch.

// Expo go apps using the new debugger could report more then one page,
// if it exist the first one being the Expo Go host runtime.
// more over expo go on android has a bug causing newDebuggerPages
// from previously run applications to leek if the host application
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leak

return true;
}
} catch (e) {
// we ignore errors as they wer already logged by the evaluateJsFromCdp method
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were

private async evaluateJsFromCdp(
webSocketDebuggerUrl: string,
source: string,
timeoutMs: number = 2000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout is unused

Comment on lines 301 to 305
const wsUrl = new URL(webSocketDebuggerUrl);
wsUrl.hostname = "localhost";
wsUrl.port = String(this._port);

return evaluateJsFromCdp(wsUrl.toString(), source);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function doesn't seem particularily useful and it calls a method with exact same name which is confusing. I'd just move the websocket URL resolution to isExpoHostRuntime where it is now called. That method is short enough to accommodate additional logic

// is not. To perform this check we use expo host functionality
// introduced in https://github.com/expo/expo/pull/32322/files
for (const newDebuggerPage of newDebuggerPages.reverse()) {
if (!(await this.isExpoHostRuntime(newDebuggerPage.webSocketDebuggerUrl))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we only want to eliminate non-host runtimes here. Given we already connect to the websocket here, this method can actually verify if the runtime is active. This specifically makes sense given you write the old runtime can still be listed.

Since iseExpoHostRuntime returns false both in the case when we fail to connect with the runtime, and also in the case when we connected sucesfully to the runtime but it didn't have that flag set, I think we should actually check for the first case only. In the current form I'd assume that we'd end up selecting old runtime when the new one hasn't yet appeared on the list.

import WebSocket from "ws";
import { Logger } from "../Logger";

export async function evaluateJsFromCdp(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a weird choice for the method name. We don't evaluate "from" CDP but rather "over" CDP

@kmagiera kmagiera merged commit d501749 into main Dec 17, 2024
4 checks passed
@kmagiera kmagiera deleted the @Filip131311/FixExpoGoStep1 branch December 17, 2024 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants