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

PhoenixTransport Crash Fix for iOS 18 #275

Merged
merged 4 commits into from
Jan 13, 2025

Conversation

varun-score
Copy link
Contributor

@varun-score varun-score commented Jan 10, 2025

Problem

Since the release of iOS 18, we've been experiencing a significant number of crashes related to WebSocket connections. The issue was first reported in October 2024 (Issue #267) and appears to be specifically related to the NSURLSessionWebSocketTask.receive(completionHandler:) method.

Solution

  • Replaced the completion handler-based receive implementation with an async/await pattern
  • Implemented proper task cancellation to prevent memory leaks

This approach aligns with the solution implemented by SendBird for a similar issue in their SDK (reference).

Testing and Validation

  • Tested with the demo project
  • Validated in production environment with our application
  • All WebSocket functionalities (connection, message receiving, disconnection) working as expected

Looking forward to your review and feedback. This should resolve the iOS 18 crash issues. 🚀

@varun-score varun-score changed the title Crash Fix for iOS 18 PhoenixTransport Crash Fix for iOS 18 Jan 10, 2025


/// Holds the current receive task
private var receiveMesageTask: Task<Void, Never>? {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: receiveMessageTask

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thank you for reviewing the PR!

@yeahphil
Copy link

This is a race, right? Crashes if a message is received during teardown? (Note that the original reporter in the devforums thread from #267 just wrote back in and that was the culprit - they closed their CFStream on one thread while using it on another).

Anyway, this seems like a reasonable way to fix this issue. But it's definitely not the only thread safety issue in the library, and would be nice to clean concurrency up in a more structured way. Maybe URLSessionTransport wants to be an Actor?

@yeahphil
Copy link

One issue with using Task is that deployment targets are currently set too low for it to be available. I opened #276 about that, we should update deployment targets before shipping this.

@varun-score
Copy link
Contributor Author

varun-score commented Jan 11, 2025

This should be fine since URLSessionTransport is only available to iOS 13 and above.

However, we might want to explore a more structured approach. While using an actor could be a good option, it cannot directly conform to URLSessionWebSocketDelegate because the protocol is obj-c based and actor types are not obj-c compatible.

@yeahphil
Copy link

Oh, nice! I missed that your changes are already contained within an @available.

And great point on the ObjC protocol.

Copy link
Collaborator

@dsrees dsrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging deeper into this. I've left an open question on the code

@@ -192,6 +198,7 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD

deinit {
self.delegate = nil
receiveMessageTask?.cancel()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the receiveMessageTask also be canceled on disconnect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing the PR! I believe self.task?.cancel in disconnect should handle canceling the receiveMessageTask, but to be on the safer side, I’ve explicitly canceled it in disconnect as well.

@varun-score varun-score requested a review from dsrees January 13, 2025 14:18
@@ -231,6 +238,7 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
}

self.readyState = .closing
receiveMessageTask?.cancel()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be safe, lets place this after self.session?.finishTaskAndInvalidate. I would hate to kill the Task and then potentially miss something that comes in before the connection has actually closed. I haven't tested to ensure that this wouldn't happen but I'm just being precautionary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!. Moved cancellation after finishTaskAndInvalidate.

@varun-score varun-score requested a review from dsrees January 13, 2025 14:51
Copy link
Collaborator

@dsrees dsrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again!

@dsrees dsrees merged commit d53927d into davidstump:master Jan 13, 2025
@varun-score
Copy link
Contributor Author

Hey @dsrees, thank you for approving the PR. Could you please create a release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants