Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "End of stream on null" network issue?

Some users of my Android application are frequently running into a network error with the following error message: "Unexpected end of stream on null".

I didn't find any clear way to solve it by looking at other similar question. I haven't been able to reproduce the network error on my end.

1) I have unsuccessfully appended Connection=close to the requests headers as this answer suggests

2) I have unsucessflly added .retryOnConnectionFailure(true) as this answer suggests

3) I have unsuccessfully searched for a server side issue as this answer suggests but the requests with network issues are not appearing in the nginx access.log file

Here is how I init Retrofit

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(40, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();

Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient);

this.retrofit = retrofitBuilder.build();

Here is my endpoint definition (in IUserService.java)

@POST("/v2/android/login/phone")
@Headers("Content-Type: application/json;charset=UTF-8;Connection=close")
Call<APIResult<Session>> phoneLogin(@Body APIRequest request);

And here is how the endpoint is queried

userService = retrofit.create(IUserService.class);
Call<APIResult<Session>> call = userService.phoneLogin(request);
call.enqueue(new GenericCallback<>(context, callback));

Our configuration:

Android (Java)

Retrofit 2.3.0: 'com.squareup.retrofit2:retrofit:2.3.0'

Okhttp 3.12.1: 'com.squareup.okhttp3:okhttp:3.12.1'

Server running on AWS Elastic Beanstalk, platform: Puma with Ruby 2.6 running on 64bit Amazon Linux/2.9.0

Can anyone explain better where the problem is coming from? Any idea how to reproduce this issue locally? Any way to solve this issue?

like image 972
LAcrym0 Avatar asked Aug 20 '19 09:08

LAcrym0


1 Answers

From the stack trace, in the comments, its possible to see that the problems lies in:

Caused by java.io.EOFException: \n not found: limit=0 content=…

So, this is the original issue. It is likely that a GZIP reduction of the content is not being performed as intended, or at least, the receiver is not expecting it, and not decoding. MDN source. Try to do as following:

.addHeader("Accept-Encoding", "identity")

If this does not work, then its likely a Server error, as this other stackoverflow answer states. Note that you should test the page URL on another tool (such as CURL or something, to rule server issues out)

like image 60
Bonatti Avatar answered Oct 05 '22 01:10

Bonatti