okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Crash Fatal Exception: java.lang.IllegalArgumentException: port out of range

Open nitinjain12 opened this issue 5 years ago • 6 comments

This crash is not reproducible but heavily reported on crashlytics. crashlytics suggests It happens within the first second of the session starts.

OKHttp version: 3.12.8

Below is a stack trace of the crash.

Fatal Exception: java.lang.IllegalArgumentException: port out of range:123456 at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:144) at java.net.InetSocketAddress.createUnresolved(InetSocketAddress.java:269) at sun.net.spi.DefaultProxySelector$1.run(DefaultProxySelector.java:315) at sun.net.spi.DefaultProxySelector$1.run(DefaultProxySelector.java:219) at java.security.AccessController.doPrivileged(AccessController.java:43) at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:218) at okhttp3.internal.connection.RouteSelector.resetNextProxy(RouteSelector.java:129) at okhttp3.internal.connection.RouteSelector.(RouteSelector.java:63) at okhttp3.internal.connection.StreamAllocation.(StreamAllocation.java:101) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:113) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)

nitinjain12 avatar Oct 11 '21 10:10 nitinjain12

This is a combination of a bug/poor handling of bad proxy config in DefaultProxySelector.select, and obviously the bad proxy config with a dodgy port.

I don't think we can fix this in OkHttp, apart from maybe making it an IOException. But really the best workaround is probably for you to detect a bad proxy config in your app and override the proxy manually.

We wouldn't change OkHttp to swallow this and continue.

Sorry.

yschimke avatar Oct 12 '21 20:10 yschimke

Hey @nitinjain12
excatly same issue, stacktrce -

Fatal Exception: java.lang.IllegalArgumentException: port out of range:123456
       at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:144)
       at java.net.InetSocketAddress.createUnresolved(InetSocketAddress.java:269)
       at sun.net.spi.DefaultProxySelector$1.run(DefaultProxySelector.java:315)
       at sun.net.spi.DefaultProxySelector$1.run(DefaultProxySelector.java:219)
       at java.security.AccessController.doPrivileged(AccessController.java:43)
       at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:218)
       at com.android.okhttp.internal.http.RouteSelector.resetNextProxy(RouteSelector.java:123)
       at com.android.okhttp.internal.http.RouteSelector.<init>(RouteSelector.java:63)
       at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:172)
       at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
       at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
       at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
       at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262)
       at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:219)
       at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:30)
       at com.google.firebase.installations.remote.FirebaseInstallationServiceClient.writeRequestBodyToOutputStream(FirebaseInstallationServiceClient.java:234)
       at com.google.firebase.installations.remote.FirebaseInstallationServiceClient.writeGenerateAuthTokenRequestBodyToOutputStream(FirebaseInstallationServiceClient.java:275)
       at com.google.firebase.installations.remote.FirebaseInstallationServiceClient.generateAuthToken(FirebaseInstallationServiceClient.java:410)
       at com.google.firebase.installations.FirebaseInstallations.fetchAuthTokenFromServer(FirebaseInstallations.java:563)
       at com.google.firebase.installations.FirebaseInstallations.doNetworkCallIfNecessary(FirebaseInstallations.java:387)
       at com.google.firebase.installations.FirebaseInstallations.lambda$doRegistrationOrRefresh$2(FirebaseInstallations.java:374)
       at com.google.firebase.installations.-$$Lambda$FirebaseInstallations$SC1awUtshakVcZWKWh8TCfIkd4g.run(-.java:4)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
       at java.lang.Thread.run(Thread.java:1012)

Were you able to resolve this?

vikas0 avatar Dec 08 '22 10:12 vikas0

com.android.okhttp.internal.http is not this project, it's part of AOSP.

yschimke avatar Dec 08 '22 12:12 yschimke

How to handle this issue? or what is the reason port is configured to 123456 in the SDK

san-sk avatar Feb 09 '23 12:02 san-sk

@yschimke We are facing a similar issue with users of our app and even though we have the code written to catch the exception and handle it appropriately, we are still seeing app crash happening because the OkHttp thread crashes. Is there any way we can suppress this as this issue happens at the user level where they might have proxy misconfigured on their device.

shivramg94 avatar Feb 21 '23 13:02 shivramg94

If you are seeing these lines

Fatal Exception: java.lang.IllegalArgumentException: port out of range:123456
...
at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:218)
at okhttp3.internal.connection.RouteSelector.resetNextProxy(RouteSelector.java:129)

then try something like

           okHttpClientBuilder.proxySelector(object : ProxySelector() {
                override fun select(uri: URI?): List<Proxy> {
                    return try {
                        getDefault().select(uri)
                    } catch (e: Exception) {
                        listOf(Proxy.NO_PROXY)
                    }
                }

                override fun connectFailed(uri: URI?, sa: SocketAddress?, ioe: IOException?) {
                    getDefault().connectFailed(uri, sa, ioe)
                }
            })
            .build()

If you are seeing com.android.okhttp.internal.http.RouteSelector, then please raise a bug with AOSP.

yschimke avatar Feb 21 '23 14:02 yschimke