EventSource icon indicating copy to clipboard operation
EventSource copied to clipboard

refactor: Fix DataTask cancel() method design issue

Open knine79 opened this issue 1 year ago β€’ 1 comments

Problem

The original code had a design issue where urlSession was created inside the events() function, but the cancel() method required it as a parameter. This caused the following problems:

  1. urlSession only existed within the function scope, making it impossible to access the correct session when calling cancel() from outside
  2. The cancel() method was public but couldn't receive the correct URLSession instance from external calls

Solution

  1. Store URLSession as instance variable: Added _urlSession Mutex variable to the DataTask class to manage the session object at the instance level
  2. Refactor cancel() method: Modified to be callable without parameters and use the stored session object for cancellation
  3. Clean up method signatures: Removed unnecessary urlSession parameters from handleSessionError, handleSessionResponse, parseMessages, and close methods
  4. Improve memory management: Set session to nil after cancellation to prevent memory leaks

Key Changes

  • Added _urlSession: Mutex<URLSession?> instance variable to DataTask class
  • Changed cancel() method signature from cancel(urlSession: URLSession) to cancel()
  • Removed urlSession parameter from all related methods
  • Added urlSession = nil in cancel() method for better memory management

Benefits

  1. Proper session management: The cancel() method can now accurately cancel the session that's actually in use
  2. API consistency: The cancel() method is more intuitive to call without parameters
  3. Memory safety: Clear lifecycle management of session objects
  4. Thread safety: Uses Mutex to handle concurrent access issues

Testing

  • All existing tests pass
  • Compilation successful

Breaking Changes

This change modifies the public API of the cancel() method. Previously it required a URLSession parameter, now it doesn't require any parameters. This is a breaking change but improves the API design significantly.

Related Issues

Fixes the design issue where external calls to cancel() couldn't properly cancel the URLSession due to scope limitations.

knine79 avatar Aug 21 '25 01:08 knine79

@Recouse CI / Build on iOS 15 is failing because the ios-runtime cache isn’t being hit. In another PR last month, the cache was hit, but not this time. I’m not sure why. Could you investigate?

knine79 avatar Aug 21 '25 09:08 knine79

Is there any news on this?

I updated from 0.1.4 to 0.1.5 and now I'm part of those who have a compilation issue because of the .cancel(urlSession:) param.

I don't really understand why this changed and why we are now forced to pass the URLSession since the used urlSession is an internal property inaccessible from the outside. It really seems like a bug to me.

For the moment I'm going to lock to 0.1.4

xxZap avatar Dec 04 '25 16:12 xxZap

@xxZap Maybe @Recouse is very busy. πŸ˜… As @Recouse mentioned in https://github.com/Recouse/EventSource/pull/36#issuecomment-3492947985, cancel(urlSession:) has been changed to private last month. It's been a while since I submitted the PR, and I haven't tested with the latest code, so I'm not sure if this is accurate, but I think the intention is to save the DataTask returned from dataTask() and then use dataTask.cancel(). However, that code hasn't been released yet, so you'll probably need to reference the branch (main) in spm. For reference, I switched to a different library. πŸ˜…πŸ˜…πŸ˜…

knine79 avatar Dec 05 '25 03:12 knine79