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

Fix issues in ImageDownload and ImageDownloader #3072

Merged
merged 4 commits into from Jun 15, 2021

Conversation

S2Ler
Copy link
Contributor

@S2Ler S2Ler commented Jun 11, 2021

Fixes #3067

  • Fix a lot of thread-safety issues.
  • Fix incorrect handling of errors:
    • Completions are called two times for HTTP status code >= 400.
    • ImageDownload operation wasn't properly transition into finished
      state.
  • ImageDownload operation didn't have proper isReady state.
  • Increase test coverage for these types.

Implementation

ImageDownloader

  • ImageDownloader is fixed by properly using Dispatch Queue: perform protected state read/write inside the queue.
  • concurrent accessQueue replaced by a serial one, because most accesses to the queue modifies the protected state that require a barrier to be placed. That makes the accessQueue merely a serial queue.

ImageDownload

ImageDownload is a subclass of NSOperation.

  • The implementation is improved with a simpler to use State variable. No more multiple flags juggling.
  • A queue replaced with a lock.
  • Replace objc_sync_enter with a lock.

- Fix a lot of thread-safety issues.
- Fix incorrect handling of errors:
   - Completions are called two times for HTTP status code >= 400.
   - `ImageDownload` operation wasn't properly transition into finished
      state.
- `ImageDownload` operation didn't have proper `isReady` state.
- Increase test coverage for these types.
@S2Ler S2Ler added bug Something isn’t working op-ex Refactoring, Tech Debt or any other operational excellence work. - crash labels Jun 11, 2021
@S2Ler S2Ler requested review from 1ec5 and a team June 11, 2021 10:53
@S2Ler S2Ler self-assigned this Jun 11, 2021
@S2Ler S2Ler linked an issue Jun 11, 2021 that may be closed by this pull request
@S2Ler S2Ler force-pushed the s2ler/3067-ImageDownloader branch from 16e680b to 367eb67 Compare June 11, 2021 10:56
@1ec5 1ec5 added this to the v2.0.0 (RC) milestone Jun 11, 2021
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

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

Locking internal to a class is always scary, but I think you’ve covered your bases.

Comment on lines 41 to 42
// So we trigger `init` here so that `urlSession` is initialized in a thread safe manner.
_ = urlSession
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the lazy syntax benefit us here? Maybe it’d be simpler to inline that initialization here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good questions, thanks for asking. Without going deep into explaining the rationale why I've kept lazy var I think we can change private lazy var urlSession: URLSession to private var urlSession: URLSession!.

@S2Ler
Copy link
Contributor Author

S2Ler commented Jun 14, 2021

Locking internal to a class is always scary, but I think you’ve covered your bases.

Sorry, can you please rephrase?

@S2Ler S2Ler merged commit 6dae158 into release-v2.0 Jun 15, 2021
@S2Ler S2Ler deleted the s2ler/3067-ImageDownloader branch June 15, 2021 06:03
@1ec5 1ec5 mentioned this pull request Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn’t working op-ex Refactoring, Tech Debt or any other operational excellence work.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash in ImageDownloader
2 participants