/claim #9681 Fix- #9681

Problem

The PromiseSpec test “waiter stack safety” fails with a NullPointerException on Scala Native when forking 10,000 fibers. The issue occurs in WeakConcurrentBag due to a bug in Scala Native’s ConcurrentHashMap.newKeySet implementation under high concurrency.

Root Cause

Scala Native’s ConcurrentHashMap.newKeySet() has concurrency issues that cause NPEs when multiple threads access it simultaneously during high-stress scenarios like forking 10K fibers.

Solution

Replace ConcurrentHashMap.newKeySet with Collections.synchronizedSet(new HashSet[A]()) in the Scala Native-specific PlatformSpecific.scala. This provides thread-safe set operations without the concurrency bugs present in Scala Native’s ConcurrentHashMap implementation.

Changes

  • core/native/src/main/scala/zio/internal/PlatformSpecific.scala:
    • Replace ConcurrentHashMap.newKeySet[A]() with Collections.synchronizedSet(new HashSet[A]())
    • Replace ConcurrentHashMap.newKeySet[A](initialCapacity) with Collections.synchronizedSet(new HashSet[A](initialCapacity))
    • Add HashSet import

Demo Video

https://drive.google.com/file/d/1SqJPecRrI51hFZcm9Qz_3LQpY9wmimUd/view?usp=sharing

Testing

The previously failing test now passes consistently:

sbt "project coreTestsNative" "testOnly *PromiseSpec* -- -z \"waiter stack safety\""

Claim

Total prize pool $150
Total paid $0
Status Pending
Submitted July 02, 2025
Last updated July 02, 2025

Contributors

SO

Sonal Yadav

@sonalyadav1

100%

Sponsors

ZI

ZIO

@ZIO

$150