Upload Image and Time Out #202

jayztemplier opened this Issue Jun 2, 2011 · 6 comments


None yet
4 participants

I spend hours to detect an error : I try to upload an image from the iPhone to my RoR server. The time out is set to 10.
The server receive datas, and respond well, but ASI detects a Time out even when the image has been uploaded. So I tried to read ASIHTTPRequest.m and I found this (I added nslog):

  • (BOOL)shouldTimeOut
    NSTimeInterval secondsSinceLastActivity = [[NSDate date] timeIntervalSinceDate:lastActivityTime];
    // See if we need to timeout
    if ([self readStream] && [self readStreamIsScheduled] && [self lastActivityTime] && [self timeOutSeconds] > 0 && secondsSinceLastActivity > [self timeOutSeconds]) {

    // We have no body, or we've sent more than the upload buffer size,so we can safely time out here
    NSLog(@"postLenght(%ld) - sent(%ld) - bufferSize(%ld)",[self postLength], [self totalBytesSent],[self uploadBufferSize]);
    if ([self postLength] == 0 || ([self uploadBufferSize] > 0 && [self totalBytesSent] > [self uploadBufferSize])) {
        return YES;
    // ***Black magic warning***
    // We have a body, but we've taken longer than timeOutSeconds to upload the first small chunk of data
    // Since there's no reliable way to track upload progress for the first 32KB (iPhone) or 128KB (Mac) with CFNetwork, we'll be slightly more forgiving on the timeout, as there's a strong chance our connection is just very slow.
    } else if (secondsSinceLastActivity > [self timeOutSeconds]*1.5) {
        return YES;

    return NO;

and here are the logs of the Console :
postLenght(167649) - sent(0) - bufferSize(167649)
postLenght(167649) - sent(0) - bufferSize(167649)
postLenght(167649) - sent(0) - bufferSize(167649)
postLenght(167649) - sent(0) - bufferSize(167649)
postLenght(167649) - sent(0) - bufferSize(167649)
postLenght(167649) - sent(0) - bufferSize(167649)
postLenght(167649) - sent(0) - bufferSize(167649)
postLenght(167649) - sent(0) - bufferSize(167649)

and sometimes I had :

postLenght(167649) - sent(167649) - bufferSize(0)
postLenght(167649) - sent(167649) - bufferSize(0)
postLenght(167649) - sent(167649) - bufferSize(0)

In those two cases, ASI returns YES to the shouldTimeOut, but the server received my image and other datas ...

My temporary solution is to comment a part of the predicat :
if ([self postLength] == 0 /* || ([self uploadBufferSize] > 0 && [self totalBytesSent] > [self uploadBufferSize])*/) {
return YES;

But if you have another solution, I'll take it :)

Thank you


pokeb commented Jun 4, 2011

Are you seeing this for every request, or just every few requests? If the latter, make sure you disable persistent connections for your upload (or upgrade to the latest version of ASIHTTPRequest, which will do this for you), and see if it solves the problem.

You're commenting out the code that makes ASIHTTPRequest timeout under normal circumstances (ie: it failed to receive a response within 10 seconds), and I strongly recommend against doing this. If your server takes more than 10 seconds to process the uploaded image and return at least part of the response, the best approach would be to increase the timeout.



Thank you for your answer. The problem is that when I increase the timeout, sometime, I have a probleme of connexion between the request and the server (I do not know why..) and I must wait the big timeout to see the error ... but If I do not increase the timeout, the error appears immediatly .. So I don't think, in my case, that I have to increase the timeout...

The solution I found works ... but I know It's not "cool" to do that ...

Thank you again for your answer ... and thank you for your wonderful library :)


Serjar commented Jun 19, 2011

we are experiencing a similar issue in low speed wwan connections.
Uploading a 70k file synchronously to RoR server. I monitor the progress and it goes complete in about 6 seconds. However we need to control the response back from the server, and getting it takes more than 20 secs.
I was going nuts with the timeout at 20 sec and found that with 30 it consistenly delivers back the response.
No issue seen over wifi.
I will try Asynch to see if the callback has any timing difference.
Best regards

I have been suffering the timeout issue in multiple iPhone applications since over a year now.

Doing the same with NSURLConnection works fine!

Persistent connections were set to NO. Yet, nothing.

If you search Google for "asihttprequest", it's first suggestion will be "asihttprequest timeout". I wonder if you're in knowledge of this. http://cl.ly/103l0c2V2J0F2C0n2q24

I have a reproducible way for this, I think.

Thank you Gurpartap, is it possible to have the piece of code you developed for the upload ? With the NSURLConnection ? Thank you again !

@jayztemplier It's really very generic like any examples out there, except I wrote some wrapper around it which behaves like ASIFormDataRequest, last year.

You can find them here: https://github.com/Gurpartap/GSHTTPRequest and https://github.com/Gurpartap/GSFormDataRequest.

They could use some improvements though. I'd still like asi to work for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment