Skip to content

KernelDispatch / KernelRegistry are not safe for concurrent reads #1259

Description

@michalharakal

Summary

KernelDispatch (skainet-backend-api) keeps its kernel table in a plain MutableList and guards auto-install with an unsynchronized var autoInstallAttempted; KernelRegistry does the same with its provider list. KernelDispatch.matmul calls ensureInstalled() and find(key) on every dispatch, so two threads dispatching at once (e.g. per-head attention tasks, or two forwards on two contexts) can race: ConcurrentModificationException from ArrayList, a double auto-install, or a lost registration.

Where

  • skainet-backends/skainet-backend-api/src/commonMain/kotlin/sk/ainet/backend/api/kernel/KernelDispatch.ktkernels: MutableList, ensureInstalled(), register(), var defaultSink
  • .../KernelRegistry.ktproviders: MutableList, register() (its KDoc already says "not thread-safe")

Fix (in feature/skeep-005-schedules)

Immutable snapshots replaced wholesale under a lock (@Volatile list + a @OptionalExpectation JvmSynchronized that actualizes to kotlin.jvm.Synchronized on JVM/Android and is a no-op elsewhere). Reads stay lock-free. Covered by KernelDispatchConcurrencyTest (16 threads registering/finding; contended ensureInstalled() runs once).

Context

Prerequisite for SKEEP-005 (schedules / structured concurrency): schedule workers dispatch concurrently.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompute-backendCompute backends and graphsskill:kotlin-coreKotlin implementation in commonMain

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions