Exception when uploading instances
Software and hardware versions
Collect latest from master on Moto X Pure
Problem description
When uploading instance, there's exception when closing the response stream. The upload is successful though.
Steps to reproduce the problem
Upload a single instance to the nafundi test server (or any aggregate server).
Expected behavior
No stacktrace in the log.
Other information
04-25 16:11:42.955 19488-26186/org.odk.collect.android I/InstanceUploaderTask: Using Uri remap for submission 104. Now: https://nafundi-test.appspot.com/submission
04-25 16:11:42.956 19488-26186/org.odk.collect.android I/InstanceUploaderTask: added xml_submission_file: basic_2017-03-06_10-39-48_1090.xml
04-25 16:11:42.957 19488-26186/org.odk.collect.android I/InstanceUploaderTask: Issuing POST request for 104 to: https://nafundi-test.appspot.com/submission
04-25 16:11:43.548 19488-26186/org.odk.collect.android E/WebUtils: Unable read the stream
java.io.IOException: Attempted read from closed stream.
at org.opendatakit.httpclientandroidlib.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:167)
at org.opendatakit.httpclientandroidlib.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
at libcore.io.Streams.skipByReading(Streams.java:159)
at java.io.InputStream.skip(InputStream.java:229)
at org.odk.collect.android.utilities.WebUtils.discardEntityBytes(WebUtils.java:290)
at org.odk.collect.android.tasks.InstanceUploaderTask.uploadOneSubmission(InstanceUploaderTask.java:463)
at org.odk.collect.android.tasks.InstanceUploaderTask.processChunk(InstanceUploaderTask.java:575)
at org.odk.collect.android.tasks.InstanceUploaderTask.doInBackground(InstanceUploaderTask.java:599)
at org.odk.collect.android.tasks.InstanceUploaderTask.doInBackground(InstanceUploaderTask.java:74)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
This exception isn't related to canceling the uploading process as I can see the same when the upload is completed. I think the current behavior itself is ok. You can cancel the process when you see "Please for a moment" message and then your form isn't sent, but you are not able to cancel sending the form which is currently sending - I mean if you can see in the dialog that form number 2 is sending you can't cancel that process but if you click on the cancel button next forms are still on the device.
It work in the same way if we try to send data to aggregate or google sheets. I think it's ok and we shouldn't change this approach as it might be dangerous
This exception is always thrown so discardEntityBytes method doesn't work.
Here is my proposal https://github.com/opendatakit/collect/pull/1047
There are 20k + noticed exceptions in Firebase during last 24 hours.
The purpose of discardEntityBytes is to consume the entity stream to release the connection in the case when the result isn't actually something we want. Without this, the stream would be blocked. I'm not certain that the current approach is the absolute best one but as I understand it, making sure this consumption happens is important. What seems to happen is that because the stream object is of type EofSensorInputStream, it closes itself and then there's a crash when trying to skip from it after.
@alxndrsn I bet you know more about this, could you please help us out with a sanity check? I believe that WebUtils.discardEntityBytes can/should be replaced by a call on EntityUtils.consume(response.getEntity()); (see here
for docs and here for a bit more about its usage).
In the mean time, I'm going to change the logging call to information rather than exception since it happens every time and it's not actually producing a crash for users.
I can't wait until we replace this whole lib.
Arg, @grzesiek2010 I thought you had just removed all of the entity stream clearing in #1047 but now I see you replaced discardEntityBytes with EntityUtils.consumeQuietly which does sound good. Can you say more about why you think the clearing isn't necessary in DownloadFormsTask?
In general, the more information you can provide about the choices you made in a pull request, the better! Thanks!!
@lognaturel
Can you say more about why you think the clearing isn't necessary in DownloadFormsTask?
because we close the stream a few lines below https://github.com/opendatakit/collect/pull/1047/files#diff-a0a2d90fbe75ec05be821f16c24e74c3R440
am I wrong? looking at consume method it just close the stream as well. So If we do that manually we don;t need that loop I believe.
As I understand it, the try block starting on line 414 could fail before the InputStream is fully consumed. That would mean that in the finally, it would be closed without being fully drained and thus the connection would be blocked and not go back to the pool. I think a call on EntityUtils.consumeQuietly is the right thing to have there as well.
In fact, I think we can get rid of discardEntityBytes entirely and replace it with EntityUtils.consumeQuietly calls.
@lognaturel
I think a call on EntityUtils.consumeQuietly is the right thing to have there as well
hmmm I'm not sure. Here is a body of consume method:
public static void consume(HttpEntity entity) throws IOException {
if(entity != null) {
if(entity.isStreaming()) {
InputStream instream = entity.getContent();
if(instream != null) {
instream.close();
}
}
}
}
so this method just close the input stream so why do we need to do that manually a few lines below: https://github.com/opendatakit/collect/pull/1047/files#diff-a0a2d90fbe75ec05be821f16c24e74c3R440 maybe we just can call EntityUtils.consumeQuietly(response.getEntity()); instead of that close and instead of the loop https://github.com/opendatakit/collect/pull/1047/files#diff-a0a2d90fbe75ec05be821f16c24e74c3L442 I removed.
Well how interesting. I figured consume/consumeQuietly did something more. The docs for getContent say very explicitly IMPORTANT: Please note all entity implementations must ensure that all allocated resources are properly deallocated after the InputStream.close() method is invoked, do you know which resources that refers to exactly? I also notice that the description for getContent() in, for example, the concrete implementing BasicHttpEntity class doesn't include that warning.
I do think you're right that calling EntityUtils.consumeQuietly(response.getEntity()); to replace both the loop and the close is the way to go.
@lognaturel
do you know which resources that refers to exactly?
no I have no idea...
@alxndrsn Any chance you know a bit about these HTTP APIs and can sanity check our thinking?
@alxndrsn and I talked about this a bit in #collect-code. Since this code has been working and we don't have any info on why discardEntityBytes was added in the first place or a firm understanding of the consume methods, let's not change the existing code much. Additionally, the goal is eventually to replace this library (#620).
The ideal would be to just fix the loop that reads/skips bytes so it doesn't crash. @grzesiek2010 Do you want to see if you can do that?
@lognaturel I was trying and my second idea is using https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available() before so please look at my updated pr.
It's still visible in firebase (a few positions in the list). @lognaturel we should reopen it. Or we can just merge this https://github.com/opendatakit/collect/pull/1232
@grzesiek2010 if that exception's expected then I agree it should be changed to an INFO message.