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

NSURLSession returns nil upload task in background #1675

Closed
erbenmo opened this Issue Dec 8, 2013 · 8 comments

Comments

Projects
None yet
7 participants
@erbenmo

erbenmo commented Dec 8, 2013

Hi,

I had an assertion error in AFN in AFURLSessionManager.m

  • (void)setDelegate:(AFURLSessionManagerTaskDelegate *)delegate
    forTask:(NSURLSessionTask *)task;
    it is complaining that the task is nil.

which is called by

  • (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
    fromFile:(NSURL *)fileURL
    progress:(NSProgress * __autoreleasing *)progress
    completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler;

It seems [self.session uploadTaskWithRequest:request fromFile:fileURL] is returning a nil.

All of the above error happened in background and it is not happening every time.
I checked that self.session is not nil. request seems valid and the file exists at fileURL.

Has anyone had this issue before?

@JayZhao

This comment has been minimized.

Show comment
Hide comment
@JayZhao

JayZhao Jan 22, 2014

I found that downloadTaskWithRequest also returns nil, I have no idea how it happened.

JayZhao commented Jan 22, 2014

I found that downloadTaskWithRequest also returns nil, I have no idea how it happened.

@kolyuchiy

This comment has been minimized.

Show comment
Hide comment
@kolyuchiy

kolyuchiy Jan 22, 2014

It looks like this is a bug in iOS. More info here: https://devforums.apple.com/message/926113#926113

It looks like this is a bug in iOS. More info here: https://devforums.apple.com/message/926113#926113

@yechunjun

This comment has been minimized.

Show comment
Hide comment
@yechunjun

yechunjun Feb 11, 2014

I also found this issue that downloadTaskWithRequest returns nil. Anybody knows how to fix?

I also found this issue that downloadTaskWithRequest returns nil. Anybody knows how to fix?

@donholly

This comment has been minimized.

Show comment
Hide comment
@donholly

donholly Apr 1, 2014

Anyone have any update on this by chance? Seeing the same thing :(

donholly commented Apr 1, 2014

Anyone have any update on this by chance? Seeing the same thing :(

@kolyuchiy

This comment has been minimized.

Show comment
Hide comment
@kolyuchiy

kolyuchiy Apr 24, 2014

I tried to fix it by making more attempts to create the task after nil has been returned (denivip@4872bf2). This hasn't fixed the problem completely, but now we get much less of these errors (on the order of 10-100 times less).

I tried to fix it by making more attempts to create the task after nil has been returned (denivip@4872bf2). This hasn't fixed the problem completely, but now we get much less of these errors (on the order of 10-100 times less).

@mattt

This comment has been minimized.

Show comment
Hide comment
@mattt

mattt May 17, 2014

Contributor

Proposed workaround in #2074. Please continue discussion on that thread.

Contributor

mattt commented May 17, 2014

Proposed workaround in #2074. Please continue discussion on that thread.

@aCatDream

This comment has been minimized.

Show comment
Hide comment
@aCatDream

aCatDream Sep 26, 2014

Hi

Today i have a similar bug : after 10 or 20 background downloading, [self.session downloadTaskWithRequest:request] return always nil.

In this case, i modify downloadTaskWithRequest for return nil and the application re-init AFURLSessionManager.
This hack work !

So, when downloadTask is nil, i sugest to re-init the NSURLSession instance before ask for a new download task.

I hope this help you.

Hi

Today i have a similar bug : after 10 or 20 background downloading, [self.session downloadTaskWithRequest:request] return always nil.

In this case, i modify downloadTaskWithRequest for return nil and the application re-init AFURLSessionManager.
This hack work !

So, when downloadTask is nil, i sugest to re-init the NSURLSession instance before ask for a new download task.

I hope this help you.

@donholly

This comment has been minimized.

Show comment
Hide comment
@donholly

donholly Sep 26, 2014

I've noticed this seems to be cleaned up in iOS 8. Is anyone else noticing the same?

I've noticed this seems to be cleaned up in iOS 8. Is anyone else noticing the same?

Ner1Co added a commit to Ner1Co/AFNetworking that referenced this issue Sep 6, 2017

AFURLSessionManager: Move the workaround attempts inside safe block.
Static Analyzer raised a warning that
`uploadTaskWithRequest:fromFile:progress:completionHandler:` can return
`nil` although its return type is not nullable.

The method `url_session_manager_create_task_safely` is receiving a block
that runs the uploading task under the conditions:
if iOS == 7.x: create synchronously task on serial creation_queue.
if iOS >= 8.x: just create the task.
in order to overcome another iOS 7 bug:
AFNetworking#2093

However the workaround attempts that should overcome another bug:
AFNetworking#1675
are not inside the safely method.

Ner1Co added a commit to Ner1Co/AFNetworking that referenced this issue Sep 11, 2017

Static analyzer raises a warning that
`uploadTaskWithRequest:fromFile:progress:completionHandler:` can return
`nil` although its return type is declared to be nonnull.
Inside `uploadTaskWithRequest:fromFile:progress:completionHandler:` the
creation of the upload task is done using the method
`url_session_manager_create_task_safely` passing it a block that creates
the task. This function is used to workaround a bug in iOS 7.x
(AFNetworking#2093), where task creation must be performed on a single
thread.

However there's another bug in iOS < 8.0 that causes
`NSURLSession` to sometimes return `nil` when creating a file upload
task (AFNetworking#1675). This bug has another workaround that retry to
create the task few more times. However these retry attempts are not
executed via the safety function mentioned above, potentially
reinstating the other iOS 7 bug.

This commit moves the task creation retry attempts to inside the block
that is sent to `url_session_manager_create_task_safely` and thus
ensuring retry attempts if needed, will also run on the designated
queue.

Ner1Co added a commit to Ner1Co/AFNetworking that referenced this issue Sep 12, 2017

AFURLSessionManager: Fix static analyzer issues.
Static analyzer raises a warning that
`uploadTaskWithRequest:fromFile:progress:completionHandler:` can return
`nil` although its return type is declared to be nonnull.
Inside `uploadTaskWithRequest:fromFile:progress:completionHandler:` the
creation of the upload task is done using the method
`url_session_manager_create_task_safely` passing it a block that creates
the task. This function is used to workaround a bug in iOS 7.x
(AFNetworking#2093), where task creation must be performed on a single
thread.

However there's another bug in iOS < 8.0 that causes
`NSURLSession` to sometimes return `nil` when creating a file upload
task (AFNetworking#1675). This bug has another workaround that retry to
create the task few more times. However these retry attempts are not
executed via the safety function mentioned above, potentially
reinstating the other iOS 7 bug.

This commit moves the task creation retry attempts to inside the block
that is sent to `url_session_manager_create_task_safely` and thus
ensuring retry attempts if needed, will also run on the designated
queue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment