Replace identity-hash Koin scopeId with a monotonic counter in context classes#759
Merged
Merged
Conversation
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
c3a599f to
08f93d8
Compare
d47ca41 to
a635cf0
Compare
Correct the misleading comment on scopeIdsAreUniqueAcrossManyEditingContexts (5,000 is below the ~77,000 birthday-bound 50% point of the old 31-bit identity-hash space, so the guard primarily validates the new counter, not the old collision) and add a strict-ordering assertion to nextContextScopeIdIsMonotonicAndUnique so it actually verifies monotonicity as its name implies (parsing ids to Long to avoid lexicographic-string comparison). Co-Authored-By: Claude <noreply@anthropic.com>
bedaHovorka
approved these changes
Jul 13, 2026
|
bedaHovorka
pushed a commit
that referenced
this pull request
Jul 13, 2026
…t classes (#759) * Fix non-unique Koin scope ids in DefaultSimulationContext/DefaultEditingContext * Fix review findings on ContextScopeIdUniquenessTest (#759) Correct the misleading comment on scopeIdsAreUniqueAcrossManyEditingContexts (5,000 is below the ~77,000 birthday-bound 50% point of the old 31-bit identity-hash space, so the guard primarily validates the new counter, not the old collision) and add a strict-ordering assertion to nextContextScopeIdIsMonotonicAndUnique so it actually verifies monotonicity as its name implies (parsing ids to Long to avoid lexicographic-string comparison).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



DefaultSimulationContextandDefaultEditingContextderived their KoinscopeIdfrom 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, throwingScopeAlreadyCreatedException.Scope id generation
ContextScopeIds.ktwithnextContextScopeId(), a process-wide,atomicfu-backed monotonic counter that is unique by construction regardless of context creation rate.DefaultSimulationContextandDefaultEditingContextnow usenextContextScopeId()instead of an identity hash code:Documentation
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
ContextScopeIdUniquenessTest(commonTest) asserting scope ids stay unique across thousands of contexts and thatnextContextScopeId()never repeats.