Handle reentrant case in Room's acquireTransactionThread()

During a suspending transaction Room uses a thread from the transaction executor, starts an event loop in it and dispatches suspending database operations to it so they are all encapsulated within the transaction coroutine. It is usually expected that the transaction thread is different from the one starting the transaction, but in some cases they are the same. To handle such reentrant cases the `withTransaction()` has been refactored to no longer rely joining on a control job and instead it will execute the suspending transaction block from within the `runBlocking` in the transaction thread.

Executing the suspending transaction block in the `runBlocking` prevents a deadlock situation due to the continuation bouncing between the caller to the transaction thread and back. The deadlock specifically happened when Room started the event loop via `runBlocking`, resumed the `acquireTransactionThread()` coroutine but also joined the `runBlocking` coroutine with the control job. These set of events would cause a deadlock when both the acquire thread and transaction thread are the same because `acquireTransactionThread()` is unable to continue as it is waiting for `runBlocking` to finish which in turns waits for the control job to be done. Meanwhile the job never completes because the database transaction does not finish, i.e. `TransactionElement.release` is never called. By eliminating the need to wait on a control job but still support nested transactions via the transaction context, Room is able to avoid the deadlock situation.

This behaviour was mostly observed in tests where the transaction executor is usually an instant executor, but it could happen in an actual application if Room is configured to share a thread pool executor as the transaction executor and a background coroutine dispatcher and the thread pool is sufficiently busy that only one thread is left for both starting the transaction (to acquire the transaction thread) and the transaction thread itself.

Bug: 251503005
Test: SuspendingQueryTest
Change-Id: I319d977a080b89880d5cf68d139c69a8ba096107
(cherry picked from commit on android-review.googlesource.com host: 7d8a5b162ba1a8814da51ba950b8555ca94895b3)
Merged-In: I319d977a080b89880d5cf68d139c69a8ba096107
(cherry picked from commit on android-review.googlesource.com host: a613c4f3691d3cb9ae3b9f869778178ab76be230)
Merged-In: I319d977a080b89880d5cf68d139c69a8ba096107
7 files changed
tree: 9a5f9f19d52f6c5412a6b0363ecf499414b54bfa
  1. .github/
  2. .idea/
  3. activity/
  4. ads/
  5. annotation/
  6. appcompat/
  7. appsearch/
  8. arch/
  9. asynclayoutinflater/
  10. autofill/
  11. benchmark/
  12. biometric/
  13. bluetooth/
  14. browser/
  15. buildSrc/
  16. buildSrc-tests/
  17. busytown/
  18. camera/
  19. car/
  20. cardview/
  21. collection/
  22. compose/
  23. concurrent/
  24. constraintlayout/
  25. contentpager/
  26. coordinatorlayout/
  27. core/
  28. credentials/
  29. cursoradapter/
  30. customview/
  31. datastore/
  32. development/
  33. docs/
  34. docs-kmp/
  35. docs-public/
  36. docs-tip-of-tree/
  37. documentfile/
  38. draganddrop/
  39. drawerlayout/
  40. dynamicanimation/
  41. emoji/
  42. emoji2/
  43. enterprise/
  44. exifinterface/
  45. external/
  46. fragment/
  47. frameworks/
  48. glance/
  49. gradle/
  50. graphics/
  51. gridlayout/
  52. health/
  53. heifwriter/
  54. hilt/
  55. inspection/
  56. interpolator/
  57. javascriptengine/
  58. leanback/
  59. lifecycle/
  60. lint-checks/
  61. loader/
  62. media/
  63. media2/
  64. mediarouter/
  65. metrics/
  66. navigation/
  67. paging/
  68. palette/
  69. percentlayout/
  70. placeholder/
  71. placeholder-tests/
  72. playground-common/
  73. preference/
  74. print/
  75. privacysandbox/
  76. profileinstaller/
  77. recommendation/
  78. recyclerview/
  79. remotecallback/
  80. resourceinspection/
  81. room/
  82. samples/
  83. savedstate/
  84. security/
  85. sharetarget/
  86. slice/
  87. slidingpanelayout/
  88. sqlite/
  89. startup/
  90. swiperefreshlayout/
  91. test/
  92. testutils/
  93. text/
  94. tracing/
  95. transition/
  96. tv/
  97. tvprovider/
  98. vectordrawable/
  99. versionedparcelable/
  100. viewpager/
  101. viewpager2/
  102. wear/
  103. webkit/
  104. window/
  105. work/
  106. .gitignore
  107. .mailmap
  108. build.gradle
  109. cleanBuild.sh
  110. code-review.md
  111. CONTRIBUTING.md
  112. gradle.properties
  113. gradlew
  114. libraryversions.toml
  115. LICENSE.txt
  116. OWNERS
  117. PREUPLOAD.cfg
  118. README.md
  119. settings.gradle
  120. studiow
  121. TEXT_OWNERS
README.md

Android Jetpack

Revved up by Gradle Enterprise

Jetpack is a suite of libraries, tools, and guidance to help developers write high-quality apps easier. These components help you follow best practices, free you from writing boilerplate code, and simplify complex tasks, so you can focus on the code you care about.

Jetpack comprises the androidx.* package libraries, unbundled from the platform APIs. This means that it offers backward compatibility and is updated more frequently than the Android platform, making sure you always have access to the latest and greatest versions of the Jetpack components.

Our official AARs and JARs binaries are distributed through Google Maven.

You can learn more about using it from Android Jetpack landing page.

Contribution Guide

For contributions via GitHub, see the GitHub Contribution Guide.

Note: The contributions workflow via GitHub is currently experimental - only contributions to the following projects are being accepted at this time:

Code Review Etiquette

When contributing to Jetpack, follow the code review etiquette.

Accepted Types of Contributions

  • Bug fixes - needs a corresponding bug report in the Android Issue Tracker
  • Each bug fix is expected to come with tests
  • Fixing spelling errors
  • Updating documentation
  • Adding new tests to the area that is not currently covered by tests
  • New features to existing libraries if the feature request bug has been approved by an AndroidX team member.

We are not currently accepting new modules.

Checking Out the Code

Head over to the onboarding docs to learn more about getting set up and the development workflow!

Continuous integration

Our continuous integration system builds all in progress (and potentially unstable) libraries as new changes are merged. You can manually download these AARs and JARs for your experimentation.

Password and Contributor Agreement before making a change

Before uploading your first contribution, you will need setup a password and agree to the contribution agreement:

Generate a HTTPS password: https://android-review.googlesource.com/new-password

Agree to the Google Contributor Licenses Agreement: https://android-review.googlesource.com/settings/new-agreement

Getting reviewed

  • After you run repo upload, open r.android.com
  • Sign in into your account (or create one if you do not have one yet)
  • Add an appropriate reviewer (use git log to find who did most modifications on the file you are fixing or check the OWNERS file in the project's directory)

Handling binary dependencies

AndroidX uses git to store all the binary Gradle dependencies. They are stored in prebuilts/androidx/internal and prebuilts/androidx/external directories in your checkout. All the dependencies in these directories are also available from google(), jcenter(), or mavenCentral(). We store copies of these dependencies to have hermetic builds. You can pull in a new dependency using our importMaven tool.