Skip to content

Replace identity-hash Koin scopeId with a monotonic counter in context classes#759

Draft
bedaHovorka with Copilot wants to merge 2 commits into
toKdisco0.6.1from
copilot/fix-koin-scope-id-collision
Draft

Replace identity-hash Koin scopeId with a monotonic counter in context classes#759
bedaHovorka with Copilot wants to merge 2 commits into
toKdisco0.6.1from
copilot/fix-koin-scope-id-collision

Conversation

Copilot AI commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

DefaultSimulationContext and DefaultEditingContext derived their Koin scopeId from identity hash codes (platformIdentityCode / hashCode()). Identity hash codes live in a 31-bit space and are not unique, so under high context-creation volume (e.g. JMH benchmarks creating tens of thousands of contexts) two live contexts can collide, throwing ScopeAlreadyCreatedException.

Scope id generation

  • Added ContextScopeIds.kt with nextContextScopeId(), a process-wide, atomicfu-backed monotonic counter that is unique by construction regardless of context creation rate.
  • DefaultSimulationContext and DefaultEditingContext now use nextContextScopeId() instead of an identity hash code:
override val scope =
    KoinPlatformTools.defaultContext().get()
        .createScope(
            scopeId = nextContextScopeId(),
            qualifier = named<DefaultSimulationContext>(),
            source = this
        )

Documentation

  • Clarified platformIdentityCode's KDoc to explicitly state it must not be used as a unique identifier (debug logging only), since its own collision caveat was previously being ignored at a load-bearing call site.

Tests

  • Added ContextScopeIdUniquenessTest (commonTest) asserting scope ids stay unique across thousands of contexts and that nextContextScopeId() never repeats.

Copilot AI changed the title [WIP] Fix Koin scope id generation in DefaultSimulationContext Replace identity-hash Koin scopeId with a monotonic counter in context classes Jul 12, 2026
Copilot AI requested a review from bedaHovorka July 12, 2026 04:48
@bedaHovorka bedaHovorka changed the base branch from goal-10 to toKdisco0.6.1 July 12, 2026 06:56
@bedaHovorka bedaHovorka force-pushed the copilot/fix-koin-scope-id-collision branch from d47ca41 to a635cf0 Compare July 12, 2026 18:48
@sonarqubecloud

Copy link
Copy Markdown

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.

DefaultSimulationContext derives its Koin scope id from an identity hash code (not unique)

2 participants