-
Notifications
You must be signed in to change notification settings - Fork 49
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
}); | ||
|
||
ws.on("error", (e) => { | ||
Logger.debug(`[evaluateJsFromCdpAsync] Failed to connect ${wsUrl.toString()}`, e); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
timeout is unused
const wsUrl = new URL(webSocketDebuggerUrl); | ||
wsUrl.hostname = "localhost"; | ||
wsUrl.port = String(this._port); | ||
|
||
return evaluateJsFromCdp(wsUrl.toString(), source); |
There was a problem hiding this comment.
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))) { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
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
: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.