Skip to content
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

Crash when the actualDelegate property of URLSessionProxyDelegate is lost #36

Closed
toupper opened this issue Sep 8, 2021 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@toupper
Copy link

toupper commented Sep 8, 2021

Context

Our app crashes due to the exception
[PulseCore.URLSessionProxyDelegate URLSession:didBecomeInvalidWithError:]: unrecognized selector sent to instance
when the actualDelegate is deallocated and the reference lost. This happens in our app when we open and close an UIViewController instance without letting the URLSession finish, thus causing the invalidation of that URLSession.

Explanation

In URLSessionProxyDelegate

func responds(to aSelector: Selector!) -> Bool returns true because the actualDelegate reference (e.g Alamofire.SessionDelegate) implements URLSession:didBecomeInvalidWithError:.

func forwardingTarget(for selector: Selector!) -> Any? returns nil because the actualDelegate was deallocated right after calling func responds(to aSelector: Selector!) -> Bool and the weak reference lost. (e.g when closing the view controller that triggered that network request)

Because there is no forwardingTarget to receive the message and because we claimed that URLSessionProxyDelegate can respond to the selector, [PulseCore.URLSessionProxyDelegate URLSession:didBecomeInvalidWithError:]: is called, and since it does not implements this optional delegate method, crashes.

Note

A workaround is implemented in https://github.com/toupper/Pulse by adding an empty implementation of that optional delegate method in URLSessionProxyDelegate However that is not a final solution, since it could crash for any other delegate method call.

@kean
Copy link
Owner

kean commented Sep 11, 2021

However that is not a final solution, since it could crash for any other delegate method call.

I'm thinking about completely removing these options with clever method forwarding. They've been causing issues and I don't want to continue shipping something that I'm not sure is 100% reliable.

If you are using Alamofire, and I think most people do, there is EventMonitor. If you have a custom URLSession, you have access to the delegate and can add logging methods yourself.

@kean
Copy link
Owner

kean commented Sep 11, 2021

I think the root cause is that URLSessionProxyDelegate doesn't retain the real delegate the way it should (URLSession does).

@kean
Copy link
Owner

kean commented Sep 11, 2021

Should be fixed in Pulse 0.17.0. I didn't use the workaround, but instead made sure the real delegate is retained.

I also decided not to deprecate URLSessionProxyDelegate – it seems to be fine for the most part. But I no longer recommend it as a default option in the documentation.

@kean kean closed this as completed Sep 11, 2021
@kean kean added the bug Something isn't working label Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants