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

[dev-launcher] Fix port -1 error when no port in bundle url #19136

Merged
merged 3 commits into from Sep 16, 2022

Conversation

Kudo
Copy link
Contributor

@Kudo Kudo commented Sep 16, 2022

Why

when load js bundle from a URL without an explicit port, e.g. http://www.example.com/ , the app will crash with the following stacktrace.

                         E  java.lang.RuntimeException: Unable to start activity ComponentInfo{dev.expo.payments/dev.expo.payments.MainActivity}: java.lang.IllegalArgumentException: Invalid URL port: "-1"
                         E      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
                         E      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
                         E      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
                         E      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
                         E      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
                         E      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
                         E      at android.os.Handler.dispatchMessage(Handler.java:106)
                         E      at android.os.Looper.loopOnce(Looper.java:201)
                         E      at android.os.Looper.loop(Looper.java:288)
                         E      at android.app.ActivityThread.main(ActivityThread.java:7839)
                         E      at java.lang.reflect.Method.invoke(Native Method)
                         E      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                         E      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                         E  Caused by: java.lang.IllegalArgumentException: Invalid URL port: "-1"
                         E      at okhttp3.HttpUrl$Builder.parse$okhttp(HttpUrl.kt:1329)
                         E      at okhttp3.HttpUrl$Companion.get(HttpUrl.kt:1633)
                         E      at okhttp3.Request$Builder.url(Request.kt:184)
                         E      at com.facebook.react.devsupport.PackagerStatusCheck.run(PackagerStatusCheck.java:47)
                         E      at com.facebook.react.devsupport.DevServerHelper.isPackagerRunning(DevServerHelper.java:483)
                         E      at com.facebook.react.devsupport.DevSupportManagerBase$19.run(DevSupportManagerBase.java:850)
                         E      at com.facebook.react.devsupport.DevSupportManagerBase.isPackagerRunning(DevSupportManagerBase.java:856)
                         E      at com.facebook.react.ReactInstanceManager.recreateReactContextInBackgroundInner(ReactInstanceManager.java:454)
                         E      at com.facebook.react.ReactInstanceManager.createReactContextInBackground(ReactInstanceManager.java:420)
                         E      at com.facebook.react.ReactRootView.startReactApplication(ReactRootView.java:507)
                         E      at com.facebook.react.ReactRootView.startReactApplication(ReactRootView.java:476)
                         E      at com.facebook.react.ReactDelegate.loadApp(ReactDelegate.java:103)
                         E      at expo.modules.ReactActivityDelegateWrapper.loadApp(ReactActivityDelegateWrapper.kt:76)
                         E      at expo.modules.ReactActivityDelegateWrapper.onCreate(ReactActivityDelegateWrapper.kt:116)
                         E      at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:46)
                         E      at dev.expo.payments.MainActivity.onCreate(MainActivity.java:29)
                         E      at android.app.Activity.performCreate(Activity.java:8051)
                         E      at android.app.Activity.performCreate(Activity.java:8031)
                         E      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
                         E      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)

the root cause is that android.net.Uri.getPort() will return -1 when there's no port.

related facebook/react-native#34705 and ENG-5932

How

use okhttp3.HttpUrl.defaultPort(scheme) as fallback when port is -1.

since the parameter in injectReactInterceptor is already android.net.Uri, i don't want to use HttpUrl to parse it again. only change the default port would be lower risk.

Test Plan

test on bare-expo

  1. enable dev-launcher
  2. EXPO_NO_DEFAULT_PORT=1 npx expo start --tunnel
  3. in dev-launcher, load the bundle url, e.g. exp://nquwmbi.kudochien.19000.exp.direct

Checklist

@expo-bot expo-bot added the bot: passed checks ExpoBot has nothing to complain about label Sep 16, 2022
Copy link
Contributor

@lukmccall lukmccall left a comment

Choose a reason for hiding this comment

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

🔥

@Kudo Kudo marked this pull request as ready for review September 16, 2022 08:29
@Kudo Kudo merged commit aa71dbf into main Sep 16, 2022
@Kudo Kudo deleted the @kudo/android-dev-server-port branch September 16, 2022 08:29
lukmccall pushed a commit that referenced this pull request Sep 16, 2022
# Why

when load js bundle from a URL without an explicit port, e.g. http://www.example.com/ , the app will crash.
the root cause is that `android.net.Uri.getPort()` will return -1 when there's no port.

related facebook/react-native#34705 and ENG-5932

# How

use `okhttp3.HttpUrl.defaultPort(scheme)` as fallback when port is -1.

since the parameter in `injectReactInterceptor` is already android.net.Uri, i don't want to use HttpUrl to parse it again. only change the default port would be lower risk.

# Test Plan

test on bare-expo
1. [enable dev-launcher](https://github.com/expo/expo/blob/7331e13192c5b798ec67ec180949e1ef4e4da228/apps/bare-expo/android/app/src/main/java/dev/expo/payments/MainApplication.java#L25)
2. `EXPO_NO_DEFAULT_PORT=1 npx expo start --tunnel`
3. in dev-launcher, load the bundle url, e.g. `exp://nquwmbi.kudochien.19000.exp.direct`
Ddv0623 pushed a commit to preciofishbone/expo that referenced this pull request Sep 26, 2022
# Why

when load js bundle from a URL without an explicit port, e.g. http://www.example.com/ , the app will crash.
the root cause is that `android.net.Uri.getPort()` will return -1 when there's no port.

related facebook/react-native#34705 and ENG-5932

# How

use `okhttp3.HttpUrl.defaultPort(scheme)` as fallback when port is -1.

since the parameter in `injectReactInterceptor` is already android.net.Uri, i don't want to use HttpUrl to parse it again. only change the default port would be lower risk.

# Test Plan

test on bare-expo
1. [enable dev-launcher](https://github.com/expo/expo/blob/7331e13192c5b798ec67ec180949e1ef4e4da228/apps/bare-expo/android/app/src/main/java/dev/expo/payments/MainApplication.java#L25)
2. `EXPO_NO_DEFAULT_PORT=1 npx expo start --tunnel`
3. in dev-launcher, load the bundle url, e.g. `exp://nquwmbi.kudochien.19000.exp.direct`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants