Skip to content

Make WhisperKit Sendable and add locks#427

Open
naykutguven wants to merge 2 commits intoargmaxinc:swift-6from
naykutguven:sendable-whisperkit
Open

Make WhisperKit Sendable and add locks#427
naykutguven wants to merge 2 commits intoargmaxinc:swift-6from
naykutguven:sendable-whisperkit

Conversation

@naykutguven
Copy link
Contributor

This pull request introduces extensive improvements to the WhisperKit core class, focusing on thread safety, property synchronization, and test coverage. The main change is refactoring WhisperKit to synchronize all mutable state using an unfair lock, ensuring safe concurrent access. The update also adds comprehensive unit tests and utilities to verify correct property synchronization and callback behavior.

Thread safety and state synchronization:

  • Refactored WhisperKit to synchronize all mutable properties using an OSAllocatedUnfairLock, ensuring thread-safe access and mutation across concurrency domains. Added a withStateLock helper for internal synchronization.
  • Annotated WhisperKit as @unchecked Sendable and documented thread-safety guarantees, clarifying that injected dependencies must manage their own synchronization.

Property synchronization and callbacks:

  • Updated property setters/getters for WhisperKit to use synchronized backing variables, ensuring atomic access. Enhanced synchronization between tokenizer and textDecoder.tokenizer for consistency.
  • Improved callback invocation for modelStateCallback, ensuring correct transition order and thread safety during state changes.

Testing and utilities:

  • Added WhisperKitTests.swift with tests for tokenizer synchronization, decoder replacement, callback transition order, and concurrent property access, verifying thread safety and correct behavior.
  • Introduced LockedStore utility for safely aggregating state in tests, using an unfair lock for thread-safe mutation.
  • Added WhisperTokenizerMock for controlled testing of tokenizer-related features.

Copilot AI review requested due to automatic review settings February 20, 2026 19:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request introduces comprehensive thread safety improvements to the WhisperKit core class by synchronizing all mutable state with an unfair lock and marking the class as @unchecked Sendable. The changes also include extensive test coverage to verify property synchronization, callback behavior, and concurrent access patterns.

Changes:

  • Refactored WhisperKit to use OSAllocatedUnfairLock for synchronizing all mutable properties, ensuring thread-safe access across concurrency domains
  • Added comprehensive unit tests for tokenizer synchronization, decoder replacement, callback ordering, and concurrent property access
  • Introduced test utilities including LockedStore for thread-safe test state aggregation and WhisperTokenizerMock for controlled testing

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
Sources/WhisperKit/Core/WhisperKit.swift Transformed all public properties to use private backing variables with lock-protected getters/setters; added @unchecked Sendable conformance and comprehensive documentation
Tests/WhisperKitTests/WhisperKitTests.swift Added new test suite verifying tokenizer synchronization, decoder replacement behavior, callback transition order, and concurrent property access
Tests/WhisperKitTests/Utils/LockedStore.swift Introduced utility class for thread-safe state aggregation in tests using OSAllocatedUnfairLock
Tests/WhisperKitTests/Mocks/WhisperTokenizerMock.swift Added mock tokenizer implementation for controlled testing of tokenizer-related features

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

naykutguven and others added 2 commits February 24, 2026 10:40
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

2 participants