|
| 1 | +package com.superwall.sdk |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.superwall.sdk.dependencies.DependencyContainer |
| 5 | +import com.superwall.sdk.store.Entitlements |
| 6 | +import io.mockk.every |
| 7 | +import io.mockk.mockk |
| 8 | +import org.junit.Assert.assertTrue |
| 9 | +import org.junit.Test |
| 10 | +import java.util.concurrent.CountDownLatch |
| 11 | +import java.util.concurrent.TimeUnit |
| 12 | + |
| 13 | +/** |
| 14 | + * Regression guard for the AB-BA deadlock that caused the production ANR |
| 15 | + * tracked in expo-superwall#194 / SW-5092. |
| 16 | + * |
| 17 | + * Original cycle, present before the fix: |
| 18 | + * |
| 19 | + * Lock A — the Superwall singleton's intrinsic monitor, taken by both |
| 20 | + * setup() and the dependencyContainer getter. |
| 21 | + * |
| 22 | + * Lock B — the SynchronizedLazyImpl monitor backing the `entitlements` |
| 23 | + * property. Its initializer body `{ dependencyContainer.entitlements }` |
| 24 | + * re-entered Lock A. |
| 25 | + * |
| 26 | + * Production trace (before the fix): |
| 27 | + * worker-1: holds A inside setup() -> wants B |
| 28 | + * worker-2: holds B inside lazy initializer -> wants A |
| 29 | + * main: wants A from identify() / setUserAttrs() -> ANR |
| 30 | + * |
| 31 | + * This test arms the exact interleaving that previously deadlocked: |
| 32 | + * 1. Thread X holds the Superwall singleton monitor (Lock A) and then |
| 33 | + * reads `entitlements` — the pattern setup() uses when it calls |
| 34 | + * setSubscriptionStatus while still inside `synchronized(this@Superwall)`. |
| 35 | + * 2. Thread Y reads `entitlements` from outside the singleton monitor — |
| 36 | + * the pattern AppSessionManager.detectNewSession -> DeviceHelper takes |
| 37 | + * from a worker. This forces Y through the lazy initializer (Lock B). |
| 38 | + * |
| 39 | + * Under the previous code Thread Y's initializer would block on Lock A |
| 40 | + * while Thread X blocked on Lock B, and both threads would stay BLOCKED |
| 41 | + * indefinitely. Under the fix, the lazy initializer does not re-enter |
| 42 | + * the singleton monitor, so both threads complete promptly. |
| 43 | + * |
| 44 | + * The guard asserts that both threads finish within a short window. If |
| 45 | + * anyone reintroduces a synchronized hop into the `entitlements` / |
| 46 | + * `subscriptionStatus` lazy initializers (or anything else they call |
| 47 | + * that takes the Superwall singleton monitor), this test will fail by |
| 48 | + * timing out. |
| 49 | + * |
| 50 | + * java.lang.management is unavailable on the Android unit-test runtime, |
| 51 | + * so completion is observed via Thread.join with a timeout. |
| 52 | + */ |
| 53 | +class SuperwallConfigureDeadlockTest { |
| 54 | + @Test(timeout = 15_000) |
| 55 | + fun entitlements_lazy_initializer_does_not_reenter_singleton_monitor() { |
| 56 | + val context = mockk<Context>(relaxed = true) |
| 57 | + val sw = |
| 58 | + Superwall( |
| 59 | + context = context, |
| 60 | + apiKey = "test", |
| 61 | + purchaseController = null, |
| 62 | + options = null, |
| 63 | + activityProvider = null, |
| 64 | + completion = null, |
| 65 | + ) |
| 66 | + |
| 67 | + // Skip setup() but plant a usable _dependencyContainer so the |
| 68 | + // entitlements lazy initializer can return without throwing |
| 69 | + // UninitializedPropertyAccessException. |
| 70 | + val fakeDc = mockk<DependencyContainer>(relaxed = true) |
| 71 | + every { fakeDc.entitlements } returns mockk<Entitlements>(relaxed = true) |
| 72 | + val dcField = Superwall::class.java.getDeclaredField("_dependencyContainer") |
| 73 | + dcField.isAccessible = true |
| 74 | + dcField.set(sw, fakeDc) |
| 75 | + |
| 76 | + val xHasLockA = CountDownLatch(1) |
| 77 | + val yFinishedLazy = CountDownLatch(1) |
| 78 | + |
| 79 | + // Thread Y: read `entitlements` from outside the singleton monitor. |
| 80 | + // This goes through SynchronizedLazyImpl.getValue (Lock B). For Y to |
| 81 | + // complete while X holds Lock A, the lazy initializer must NOT take |
| 82 | + // the singleton monitor. |
| 83 | + val threadY = |
| 84 | + Thread({ |
| 85 | + xHasLockA.await() |
| 86 | + sw.entitlements |
| 87 | + yFinishedLazy.countDown() |
| 88 | + }, "deadlock-guard-Y").apply { isDaemon = true } |
| 89 | + |
| 90 | + // Thread X: hold the singleton monitor (Lock A), then read |
| 91 | + // `entitlements`. Mirrors setup() calling setSubscriptionStatus |
| 92 | + // while inside `synchronized(this@Superwall)`. Waits until Y has |
| 93 | + // finished its lazy access so we know Y did not deadlock. |
| 94 | + val threadX = |
| 95 | + Thread({ |
| 96 | + synchronized(sw) { |
| 97 | + xHasLockA.countDown() |
| 98 | + yFinishedLazy.await(5, TimeUnit.SECONDS) |
| 99 | + sw.entitlements |
| 100 | + } |
| 101 | + }, "deadlock-guard-X").apply { isDaemon = true } |
| 102 | + |
| 103 | + threadX.start() |
| 104 | + threadY.start() |
| 105 | + |
| 106 | + threadY.join(5_000) |
| 107 | + threadX.join(5_000) |
| 108 | + |
| 109 | + if (threadY.isAlive || threadX.isAlive) { |
| 110 | + val xFrames = threadX.stackTrace.take(8).joinToString("\n") { " at $it" } |
| 111 | + val yFrames = threadY.stackTrace.take(8).joinToString("\n") { " at $it" } |
| 112 | + val msg = |
| 113 | + buildString { |
| 114 | + appendLine("AB-BA deadlock regression: the entitlements lazy initializer") |
| 115 | + appendLine("appears to re-enter a synchronized scope on the Superwall singleton.") |
| 116 | + appendLine("This is the cycle that produced the production ANR in SW-5092.") |
| 117 | + appendLine() |
| 118 | + appendLine("Thread X (held singleton monitor, then read entitlements) state=${threadX.state}:") |
| 119 | + appendLine(xFrames) |
| 120 | + appendLine() |
| 121 | + appendLine("Thread Y (read entitlements from outside singleton monitor) state=${threadY.state}:") |
| 122 | + appendLine(yFrames) |
| 123 | + } |
| 124 | + // Daemon threads will be cleaned up on JVM exit; we just need them out of the way. |
| 125 | + assertTrue(msg, false) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments