|
| 1 | +package python.multiplatform.ffi |
| 2 | + |
| 3 | +import java.util.concurrent.CountDownLatch |
| 4 | +import java.util.concurrent.TimeUnit |
| 5 | +import java.util.concurrent.atomic.AtomicReference |
| 6 | +import kotlin.test.Test |
| 7 | +import kotlin.test.assertEquals |
| 8 | +import kotlin.test.assertNull |
| 9 | +import kotlin.test.assertTrue |
| 10 | + |
| 11 | +/** |
| 12 | + * SPEC C-4 / T-1: the GIL is released once [Python3.initialize] returns (it saves the main thread |
| 13 | + * state), so a second Kotlin thread can run Python while the first waits in Kotlin. |
| 14 | + * |
| 15 | + * `jvmTest` is shared by `desktopTest` and `androidInstrumentedTest`, so one file covers both. |
| 16 | + * Modelled on `PyTypeCacheConcurrencyTest` (desktopTest) for the thread and latch shape and on |
| 17 | + * `PythonTestFixture.withInterpreter` for the interpreter set-up. |
| 18 | + * |
| 19 | + * Every wait is bounded. If the GIL were still held, the second thread would block in |
| 20 | + * `PyGILState_Ensure`, the bounded wait would time out and the assertion would fail; nothing |
| 21 | + * waits forever on the thread that is blocked. |
| 22 | + * |
| 23 | + * Negative control (deliberately not a test, it would hang on a GIL build): thread A waiting inside |
| 24 | + * `withGIL` WITHOUT `withoutGIL` keeps the GIL, so B could never get in. |
| 25 | + */ |
| 26 | +class GilReleaseTest { |
| 27 | + |
| 28 | + private val timeoutSeconds = 20L |
| 29 | + |
| 30 | + /** Runs `6 * 7` under the GIL on a new thread; the answer or failure lands in [result]/[error]. */ |
| 31 | + private fun pythonThread(done: CountDownLatch, result: AtomicReference<String?>, error: AtomicReference<Throwable?>) = |
| 32 | + Thread { |
| 33 | + try { |
| 34 | + result.set(withGIL { PythonTestFixture.eval("6 * 7").toString() }) |
| 35 | + } catch (t: Throwable) { |
| 36 | + error.set(t) |
| 37 | + } finally { |
| 38 | + done.countDown() |
| 39 | + } |
| 40 | + }.apply { isDaemon = true } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun secondThreadRunsPythonWhileTheInitialisingThreadWaitsInKotlin() = PythonTestFixture.withInterpreter { |
| 44 | + // The initialising thread is not inside withGIL here: it only waits in Kotlin. |
| 45 | + val done = CountDownLatch(1) |
| 46 | + val result = AtomicReference<String?>(null) |
| 47 | + val error = AtomicReference<Throwable?>(null) |
| 48 | + pythonThread(done, result, error).start() |
| 49 | + val finished = done.await(timeoutSeconds, TimeUnit.SECONDS) |
| 50 | + assertTrue(finished, "second thread could not run Python: the GIL is still held after initialisation") |
| 51 | + assertNull(error.get(), "second thread failed: ${error.get()}") |
| 52 | + assertEquals("42", result.get()) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun withoutGilLetsAnotherThreadRunPythonWhileThisOneWaits() = PythonTestFixture.withInterpreter { |
| 57 | + val done = CountDownLatch(1) |
| 58 | + val result = AtomicReference<String?>(null) |
| 59 | + val error = AtomicReference<Throwable?>(null) |
| 60 | + val b = pythonThread(done, result, error) |
| 61 | + var observed = false |
| 62 | + withGIL { |
| 63 | + b.start() |
| 64 | + // Wait in Kotlin with the GIL given up; no Python call inside this block. |
| 65 | + observed = withoutGIL { done.await(timeoutSeconds, TimeUnit.SECONDS) } |
| 66 | + // The GIL is held again here, so B (if it was stuck) can only proceed after we leave. |
| 67 | + } |
| 68 | + b.join(TimeUnit.SECONDS.toMillis(timeoutSeconds)) |
| 69 | + assertTrue(observed, "B did not finish while A waited in withoutGIL: withoutGIL did not release the GIL") |
| 70 | + assertNull(error.get(), "B failed: ${error.get()}") |
| 71 | + assertEquals("42", result.get()) |
| 72 | + } |
| 73 | +} |
0 commit comments