Skip to content

Fix Scope Factory Sync Issue (#2233)#2234

Closed
cramen wants to merge 5 commits intoInsertKoinIO:4.2.0from
cramen:main
Closed

Fix Scope Factory Sync Issue (#2233)#2234
cramen wants to merge 5 commits intoInsertKoinIO:4.2.0from
cramen:main

Conversation

@cramen
Copy link
Copy Markdown

@cramen cramen commented Jun 28, 2025

@cramen cramen changed the title Fix issue 2233 Fix issue (#2233) Jun 28, 2025
@arnaudgiuliani arnaudgiuliani self-requested a review July 11, 2025 15:21
@arnaudgiuliani arnaudgiuliani added this to the 4.2.0 milestone Jul 16, 2025
@AShepelevv
Copy link
Copy Markdown

@arnaudgiuliani Could you please help, when is it planned to merge and release this fix? Thanks in advance!

@arnaudgiuliani
Copy link
Copy Markdown
Member

Any test to help would be appreciated 🙏

@arnaudgiuliani arnaudgiuliani changed the base branch from main to 4.2.0 October 20, 2025 17:10
@cramen
Copy link
Copy Markdown
Author

cramen commented Dec 1, 2025

Any test to help would be appreciated 🙏

I do not know how to reproduce this error reliably. It occurs suddenly from time to time during operation in a production environment.

Copy link
Copy Markdown
Contributor

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 PR attempts to fix issue #2233 regarding thread-insecure collection usage in ScopedInstanceFactory. The changes replace the regular HashMap with a platform-specific thread-safe map (ConcurrentHashMap on JVM, ConcurrentMutableMap on other platforms) and remove the synchronized block that was protecting instance creation.

Key changes:

  • Changed values from var with hashMapOf() to val with KoinPlatformTools.safeHashMap()
  • Removed synchronized block from the get() method
  • Removed blank lines for formatting consistency

However, the implementation introduces a race condition by removing synchronization without using atomic operations.


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

values[context.scope.id] = super.create(context)
}
if (!isCreated(context) && holdInstance) {
values[context.scope.id] = super.create(context)
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

Race condition: The check-then-act pattern here is not thread-safe. Multiple threads can pass the !isCreated(context) check simultaneously, causing super.create(context) to be invoked multiple times for the same scope ID. This violates the singleton-per-scope contract.

While safeHashMap provides a thread-safe map, it doesn't prevent this race condition. Consider using computeIfAbsent (on JVM's ConcurrentHashMap) or restoring the synchronized block to ensure atomic check-and-create operations. See SingleInstanceFactory.get() for a similar pattern that uses synchronization correctly.

Suggested change
values[context.scope.id] = super.create(context)
KoinPlatformTools.synchronized(this) {
if (!isCreated(context)) {
values[context.scope.id] = super.create(context)
}
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we need to ensure about this @cramen

@arnaudgiuliani arnaudgiuliani changed the title Fix issue (#2233) Fix Scope Factory Sync Issue (#2233) Dec 2, 2025
@arnaudgiuliani
Copy link
Copy Markdown
Member

Rewoked in #2323

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.

Thread-insecure collection without synchronized in ScopedInstanceFactory

4 participants