Skip to content

Close native locks with Synchronizable - #121

Open
Guflly wants to merge 1 commit into
touchlab:mainfrom
Guflly:fix/concurrent-collection-lock-lifecycle
Open

Guflly wants to merge 1 commit into
touchlab:mainfrom
Guflly:fix/concurrent-collection-lock-lifecycle

Conversation

@Guflly

@Guflly Guflly commented Aug 1, 2026

Copy link
Copy Markdown

Owns the native Lock in a cleaner-backed resource so Synchronizable closes it when it becomes unreachable. Adds a native GC regression test that verifies cleanup happens exactly once.

Tests: native and JVM suites, ktlint, and API validation.

Closes #117.

@samhill303

Copy link
Copy Markdown
Member

Thanks for the PR! Stately isn't under active development, so it may take some time to get it reviewed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The native constructor visibility change breaks existing callers and needs compatibility handling or explicit documentation.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds cleaner-backed native lock ownership to Synchronizable, with a GC regression test.

Changes:

  • Adds cleaner-based native lock cleanup.
  • Verifies cleanup occurs exactly once.
  • Changes the native constructor visibility.

Review note: The constructor visibility change is API-breaking for native callers and should preserve the public overload or be explicitly documented.

File summaries
File Description
stately-concurrency/src/nativeTest/kotlin/co/touchlab/stately/concurrency/SynchronizableTest.kt Tests native lock cleanup after garbage collection.
stately-concurrency/src/nativeMain/kotlin/co/touchlab/stately/concurrency/Functions.kt Adds cleaner-backed native synchronization resources.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

import kotlin.native.ref.createCleaner

fun <R> runSynchronized(block: () -> R): R = _lock.withLock(block)
actual open class Synchronizable internal constructor(private val resource: SynchronizationResource) {
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.

maybe leak: ConcurrentCollection use Lock but seems never close() it

3 participants