Investigate deadlocking virtual threads in low CPU count environments #47368
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.
Description
This is an investigatory PR into an issue reported when
SharedExecutorServiceuses virtual threads in environments with low platform CPU counts. Where code flows from usingSharedExecutorServicein one area calling into code also usingSharedExecutrorServiceto asynchronously execute logic (primarily withazure-identity's usage of MSAL4J which heavily usesCompletableFuture<T>).The change here is leveraging how the JVM pins virtual threads (targeting logic in Java 21, knowing Java 25 made changes where pinning isn't as common) where before
CompletableFuture.get()was being used directly, but that uses thread pinning logic. Now, instead that call is wrapped in what looks to be a weird usage of a localReentrantLockvariable where before the call toCompletableFuture.whenCompletetheReentrantLockis locked and the results ofwhenCompleteare ignored, and the only thing done is unlocking theReentrantLock. After that call theReentrantLockis attempted to be locked again, when that happens the future is either already complete and the lock is available or the executing thread is pinned in a way where the virtual thread is detatched from the platform thread and pinned. Which should result in virtual threads not deadlocking the platform threads of the system.All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines