Skip to content

Binder: HandleTable owns a mutex (#117) - #302

Merged
b-re-w merged 1 commit into
developfrom
feat/pmp-handle-lock
Oct 25, 2026
Merged

b-re-w merged 1 commit into
developfrom
feat/pmp-handle-lock

Conversation

@b-re-w

@b-re-w b-re-w commented Oct 25, 2026

Copy link
Copy Markdown
Member

Closes #117

  • Root cause: on desktop, _pm_release and _pm_resolve are ctypes.CFUNCTYPE bindings. ctypes releases the GIL around the foreign call, so UpcallTrampoline.releaseObject changed HandleTable without the GIL even on the GIL build. With free-threading there is no GIL at all.
  • Fix: HandleTable now has its own mutex, internal expect class PlatformMutex (jvm uses ReentrantLock, native a pthread mutex, wasm a no-op). Every read and write takes it. The critical sections are array and counter work only, with no Python call inside, so a stop-the-world pause cannot deadlock on it. The KDoc claims in HandleTable, AsyncUpcall and UpcallTable are corrected.
  • Test: HandleTableConcurrencyTest (jvmTest, run under desktopTest). 8 threads do register/resolve/release cycles. It is red against the old table (AssertionError) and green with the mutex. The reflection tests also pass on -PpythonFreeThreaded=true.
  • The full desktopTest (GIL) passes with 0 failures. compileKotlinAndroidNativeArm64, compileKotlinIosSimulatorArm64 and compileKotlinWasmJs pass.
  • Not added: a ctypes _pm_release stress test from several Python threads. The pure-table test covers the lock itself.

…se (#117)

On desktop _pm_release and _pm_resolve are ctypes.CFUNCTYPE bindings, and ctypes releases the GIL
around the call, so HandleTable mutated without the GIL even on the default build. Every table
read and write now runs under a PlatformMutex (jvm ReentrantLock, native pthread mutex, wasm
no-op). The critical sections are array and counter work only, never a Python call, so a
free-threaded stop-the-world pause cannot deadlock on it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant