Skip to content

NetworkOnMainThreadException when unsubscribing during network call #1328

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

Closed
pbryn opened this issue Nov 26, 2015 · 8 comments
Closed

NetworkOnMainThreadException when unsubscribing during network call #1328

pbryn opened this issue Nov 26, 2015 · 8 comments

Comments

@pbryn
Copy link

pbryn commented Nov 26, 2015

Stack trace:

Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1126)
at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1121)
at com.squareup.okhttp.Connection.closeIfOwnedBy(Connection.java:148)
at com.squareup.okhttp.OkHttpClient$1.closeIfOwnedBy(OkHttpClient.java:75)
at com.squareup.okhttp.internal.http.HttpConnection.closeIfOwnedBy(HttpConnection.java:137)
at com.squareup.okhttp.internal.http.HttpTransport.disconnect(HttpTransport.java:135)
at com.squareup.okhttp.internal.http.HttpEngine.disconnect(HttpEngine.java:573)
at com.squareup.okhttp.Call.cancel(Call.java:122)
at retrofit.OkHttpCall.cancel(OkHttpCall.java:162)
at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe$1.call(RxJavaCallAdapterFactory.java:102)
at rx.subscriptions.BooleanSubscription.unsubscribe(BooleanSubscription.java:72)
at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
at rx.Subscriber.unsubscribe(Subscriber.java:98)
at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
at rx.Subscriber.unsubscribe(Subscriber.java:98)
at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:124)
at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:113)
at rx.Subscriber.unsubscribe(Subscriber.java:98)
at MainActivity.onCreate(MainActivity.java:63)

Code

OkHttpClient okHttpClient = new OkHttpClient();
if(BuildConfig.DEBUG) {
    okHttpClient.interceptors().add(new LoggingInterceptor());
}
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.setReadTimeout(20000, TimeUnit.MILLISECONDS);
okHttpClient.setConnectTimeout(20000, TimeUnit.MILLISECONDS);
Retrofit retrofit = new Retrofit.Builder()
                       .client(client)
                       .baseUrl(httpsUrl)
                       .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                       .addConverterFactory(GsonConverterFactory.create())
                       .build(); 
Api api = retrofit.create(Api.class);
Subscription subscription = api.getPoiInfo("en").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<PoiInfo[]>() {
    @Override
    public void call(PoiInfo[] poiInfos) {
   }
});
try{
    Thread.sleep(200);
}catch (Exception e){
subscription.unsubscribe();

Dependencies:

compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.google.code.gson:gson:2.4'
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
@wKovacs64
Copy link

This is an OkHttp thing. See square/okhttp#869 and square/okhttp#1592. For now, use .unsubscribeOn(Schedulers.io()) (or Schedulers.computation() or something).

@liorzam
Copy link

liorzam commented Nov 26, 2015

if(BuildConfig.DEBUG) {
    okHttpClient.interceptors().add(new LoggingInterceptor());
} 

i prefer to use retrofit Full level logger: ".setLogLevel(LogLevel.FULL)"

Retrofit retrofit = new Retrofit.Builder()
                       .client(client) 
                       .baseUrl(httpsUrl) 
                       .setLogLevel(LogLevel.FULL)
                       .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 
                       .addConverterFactory(GsonConverterFactory.create()) 
                       .build();  

@liorzam
Copy link

liorzam commented Nov 26, 2015

Before a few days ago in 'Google Summit' discussed about security and SSL Connection,
maybe you might find that this information is essential for you:

https://www.youtube.com/watch?v=YYin_N6xXxQ
2:57:30 Keep it Secret, Keep it Safe

@JakeWharton
Copy link
Collaborator

This will be fixed in OkHttp 2.7. Retrofit 2 will not be released until OkHttp 2.7 is out. Tracked by #1069.

@ajdevy
Copy link

ajdevy commented Oct 10, 2016

still not fixed

@ajdevy
Copy link

ajdevy commented Oct 10, 2016

10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: android.os.NetworkOnMainThreadException
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1237)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1232)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.Util.closeQuietly(Util.java:103)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.connection.StreamAllocation.deallocate(StreamAllocation.java:265)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.connection.StreamAllocation.streamFailed(StreamAllocation.java:312)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:139)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-10 14:32:04.564 6843-7071/me.mint.debug D/MintFunctions: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

@JakeWharton
Copy link
Collaborator

That's not a Retrofit or OkHttp problem.

@vinisauter
Copy link

Just use RxJavaCallAdapterFactory.createAsync() which creates asynchronous observables.

Retrofit retrofit = new Retrofit.Builder()
                       .client(client) 
                       .baseUrl(httpsUrl) 
                       .setLogLevel(LogLevel.FULL)
                       .addCallAdapterFactory(RxJavaCallAdapterFactory.createAsync()) 
                       .addConverterFactory(GsonConverterFactory.create()) 
                       .build();  

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

No branches or pull requests

6 participants