Skip to content

Conversation

@ParaskP7
Copy link
Contributor

@ParaskP7 ParaskP7 commented Sep 26, 2022

Parent: #17173
Partially Closes: #17181

This PR resolves all Coroutines related warnings for the WordPress module:

  • Main Source:
    • 3 warnings x This declaration is experimental and its usage should be marked with '@kotlinx.coroutines.ExperimentalCoroutinesApi' or '@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)'
    • 2 warnings x 'XYZ' is deprecated. Implement CoroutineScope interface and cancel all child coroutines in xyz
    • 1 warning x @Synchronized annotation is not applicable to suspend functions and lambdas + Question [❓]: see comment @malinajirka
  • Test Source:
    • 8 warnings x This declaration is experimental and its usage should be marked with '@kotlinx.coroutines.ExperimentalCoroutinesApi' or '@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)'
    • 3 warnings x 'Xyz' is deprecated. Deprecated in the favour of 'xyz'. Please note that the provided replacement does not rethrow channel's close cause as 'poll' did, for the precise replacement please refer to the 'poll' documentation

Warnings Resolution List:

  1. Resolve opt-in experimental coroutines api warnings.
  2. Resolve deprecated coroutines thread module scope warnings.
  3. Resolve opt-in experimental coroutines api warnings for tests.
  4. Resolve deprecated coroutines channel poll warnings for tests.

Warnings Suppression List:

  1. Suppress synchronized on suspend coroutines warning.

Refactor List:

  1. Move ui dispatcher down to group scope and dispatcher funs.
  2. Rename provide background to bg dispatcher.
  3. Reorder and add experimental coroutines api on top of test suites.
  4. Remove unnecessary experimental coroutines api annotation.

Documentation List:

  1. Add scope, dispatcher and other group sections to thread module.

PS: @AjeshRPai I added you as the main reviewer, that is, in addition to @wordpress-mobile/apps-infrastructure team itself, but randomly, since I want someone from the WordPress Android team to primarily sign-off on that change. 🥇

FYI: I am going to randomly add more of you in those PRs that will follow, just so you become more aware of this change and how close we are on enabling allWarningsAsErrors by default everywhere. 🎉


To test:

  • There is nothing much to test here.
  • Verifying that all the CI checks are successful should be enough.
  • However, if you want to be thorough about reviewing this change, you could quickly smoke test both, the WordPress and Jetpack apps, and see if they are both working as expected.

PS: Also note the additional testing instructions as specified in this comment here.


Regression Notes

  1. Potential unintended areas of impact

N/A

  1. What I did to test those areas of impact (or what existing automated tests I relied on)

See To test section above.

  1. What automated tests I added (or what prevented me from doing so)

N/A


PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

1st Warning Messages: "This declaration is experimental and its usage
should be marked with '@kotlinx.coroutines.ExperimentalCoroutinesApi' or
'@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)'"

Adding the '@OptIn(ExperimentalCoroutinesApi::class)' annotation on
these functions is the recommended action to resolve this 'GlobalScope'
warnings. However, this results to the below '@OptIn' warnings:

2nd Warning Message: "This annotation should be used with the
compiler argument '-Xopt-in=kotlin.RequiresOptIn'"

Adding the '-Xopt-in=kotlin.RequiresOptIn' free compiler argument is the
recommended action to resolve this '@OptIn' warning.
Warning Messages: "'UI/DEFAULT_SCOPE: String' is deprecated. Implement
CoroutineScope interface and cancel all child coroutines in
onCleared/onDestroy/.."ª

It seems that both, the deprecated named 'UI_SCOPE' and 'DEFAULT_SCOPE'
coroutine scopes, that are being provided by the [ThreadModule] module,
are no longer being utilized within this codebase and thus, they can be
now safely removed. There was no need for any migration either.
Warning Messages: "@synchronized annotation is not applicable to suspend
functions and lambdas"

This coroutines warning is suppressed, that is, instead of it being
resolved, since it seems that:
1. Adding the '@synchronized' annotation was done deliberately, and
2. Changing that is a bit complicated as it would need a bit more
thought to properly fix this, using [Mutex] or otherwise.

For more info see:
- Fix crash in UploadStarter on Samsung devices with Android 5 #10878
(Introduced In): https://github.com/wordpress-mobile/WordPress-Android/
pull/10878
- Forbid @synchronized annotation on suspend functions (Kotlin Issue):
https://youtrack.jetbrains.com/issue/KT-27333/
Forbid-Synchronized-annotation-on-suspend-functions
- Coroutines and Java Synchronization Don't Mix (Blog Post:
https://blog.danlew.net/2020/01/28/
coroutines-and-java-synchronization-dont-mix/
Warning Messages: "This declaration is experimental and its usage
should be marked with '@kotlinx.coroutines.ExperimentalCoroutinesApi' or
'@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)'"
Warning Messages: "'poll(): E?' is deprecated. Deprecated in the favour
of 'tryReceive'. Please note that the provided replacement does not
rethrow channel's close cause as 'poll' did, for the precise replacement
please refer to the 'poll' documentation"
This is done so that the '@ExperimentalCoroutinesApi' annotation is only
presented itself once per test suite and avoid having to think and add
this boilerplate configuration multiple times, per test configuration or
function, whenever and if it is needed.
@wpmobilebot
Copy link
Contributor

wpmobilebot commented Sep 26, 2022

WordPress📲 You can test these changes on WordPress by downloading wordpress-installable-build-pr17210-43949f1.apk
💡 Scan this QR code with your Android phone to download and install the APK directly on it.
AppWordPress
Build FlavorJalapeno
Build TypeDebug
Commit43949f1
Note: This installable build uses the JalapenoDebug build flavor, and does not support Google Login.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Sep 26, 2022

Jetpack📲 You can test these changes on Jetpack by downloading jetpack-installable-build-pr17210-43949f1.apk
💡 Scan this QR code with your Android phone to download and install the APK directly on it.
AppJetpack
Build FlavorJalapeno
Build TypeDebug
Commit43949f1
Note: This installable build uses the JalapenoDebug build flavor, and does not support Google Login.

@ParaskP7
Copy link
Contributor Author

👋 @AjeshRPai !

FYI: After talking to Jirka about this comment I left for him here, I decided to go ahead and resolve this @Synchronized annotation related warning after all, that is, instead of suppressing it like I did (see here).

As such, I added an additional label to this PR, the [Status] Not Ready For Merge label. However, feel free to proceed with the code review as you would have anyway, don't let this stop you from reviewing the overall solution here.

Then, and when I'll have my finally solution to this problem ready (probably tomorrow), I'll push it here and inform you about the change so you can take a final look at it (plus test it). 🧑‍🏭

Warning Messages: "@synchronized annotation is not applicable to suspend
functions and lambdas"

It turns out that this '@synchronized' solution were already replacing
the previous (d5b9948) 'Mutex'
solution, and this, due to the fact that the existing (back then)
'Mutex' solution were causing crashes on Samsung devices running
Android 5 (API Level 21). However, this repo, and thus both, the
WordPress and Jetpack apps, are no longer supporting these devices as
the current 'minSdkVersion' is '24' (Android 7). Thus, this
'@synchronized' annotation can be now safely removed and the
'Mutex' solution reverting back.

For more info see:
- Fix crash in UploadStarter on Samsung devices with Android 5 #10877
(Introduced In): https://github.com/wordpress-mobile/WordPress-Android/
pull/10877/
- ExceptionInInitializerError #10827 (Crash Report):
#10827
- NoSuchFieldException with AtomicReferenceFieldUpdater on Samsung
Android 5.0.x devices #490 (Coroutines Issue):
Kotlin/kotlinx.coroutines#490
@ParaskP7
Copy link
Contributor Author

ParaskP7 commented Sep 27, 2022

👋 @AjeshRPai @malinajirka !

FYI: This is now done, I have resolved the @Synchronized annotation related warning by reverting the previous (d5b9948) commit via this new commit here (43949f1). I have also tested this, both, the before and after the change, by following the instructions in this PR here, everything worked as expected. For your convenience, I am copy-pasting the testing instructions here as well:

To test:
I don't have a Samsung phone running Android 5. But we should at least
verify it still works as expected on other phones.

1. Turn off the internet connection
2. Create a post
3. Publish it
4. Turn on internet connection + pull to refresh
5. Make sure the post gets uploaded

PS: You will find more info about the reasoning behind this change, along with a couple associated links, within the commit message itself.

Copy link
Contributor

@AjeshRPai AjeshRPai 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 providing me with all the details for the review in commit messages as well as in the description. As always, it’s a pleasure to review your PR since I will get the context of the change/links of the references in your PR description and commit messages themselves. 🙌

I have

  • tested the PR by going through the changes in every commit and reading the reference links mentioned in the commit messages and everything LGTM 👍🏼
  • done a smoke testing of the WordPress and jetpack app and everything looks fine 👍🏼
  • tested the synchronized to mutex changes. Published a post on offline mode with the following steps. It works as expected 🟢 👍🏼
1. Turn off the internet connection
2. Create a post
3. Publish it
4. Turn on internet connection + pull to refresh
5. Make sure the post gets uploaded

Am going to approve but not merge the PR so that anyone from @wordpress-mobile/apps-infrastructure or @Jirka can have a look. Please feel free to merge the PR at your convenience. Nice work 🙌🏼

@ParaskP7
Copy link
Contributor Author

Thank you so much for reviewing and testing this @AjeshRPai , you are awesome! 🙇

Am going to approve but not merge the PR so that anyone from https://github.com/orgs/wordpress-mobile/teams/apps-infrastructure or @Jirka can have a look. Please feel free to merge the PR at your convenience. Nice work 🙌🏼

This sounds like a good plan! 🙏

PS: As always, I am humbled by your kind words, thank you! 😊

@AliSoftware AliSoftware modified the milestones: 20.9, 21.0 Oct 3, 2022
@ParaskP7
Copy link
Contributor Author

ParaskP7 commented Oct 4, 2022

👋 @malinajirka !

Am going to approve but not merge the PR so that anyone from https://github.com/orgs/wordpress-mobile/teams/apps-infrastructure or @Jirka can have a look. Please feel free to merge the PR at your convenience. Nice work 🙌🏼

Any last thoughts on it before I go merging this to trunk. PS: Thanks a lot for your help? 🙏

@malinajirka
Copy link
Contributor

I haven't tested it but the changes LGTM! ⭐

@ParaskP7
Copy link
Contributor Author

ParaskP7 commented Oct 4, 2022

Coolio, thank you for the confirmation @malinajirka , that's all I needed! 🙇

@ParaskP7 ParaskP7 merged commit 6092a72 into trunk Oct 4, 2022
@ParaskP7 ParaskP7 deleted the analysis/wordpress-coroutines-warnings branch October 4, 2022 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler Warnings as Errors - WordPress Module

6 participants