Fix infinite spin-wait and missing cancellation propagation in TaskDeduplicator.#28938
Closed
tjgq wants to merge 1 commit intobazelbuild:masterfrom
Closed
Fix infinite spin-wait and missing cancellation propagation in TaskDeduplicator.#28938tjgq wants to merge 1 commit intobazelbuild:masterfrom
tjgq wants to merge 1 commit intobazelbuild:masterfrom
Conversation
Collaborator
|
@iancha1992 @meteorcloudy This is another PR for a 9.0.2 release. |
Collaborator
|
@bazel-io fork 9.1.0 |
fmeum
reviewed
Mar 10, 2026
src/main/java/com/google/devtools/build/lib/concurrent/TaskDeduplicator.java
Show resolved
Hide resolved
src/main/java/com/google/devtools/build/lib/concurrent/TaskDeduplicator.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/devtools/build/lib/concurrent/TaskDeduplicator.java
Outdated
Show resolved
Hide resolved
Member
|
@bazel-io fork 9.0.2 |
f78e7a3 to
df18226
Compare
fmeum
reviewed
Mar 12, 2026
src/main/java/com/google/devtools/build/lib/concurrent/TaskDeduplicator.java
Show resolved
Hide resolved
…duplicator. The test TaskDeduplicatorTest.executeIfNeeded_executeAndCancelLoop_noErrors sporadically hung during the ExecutorService.close() call. This was due to two primary issues: 1. In executeIfNew, if a thread encountered a RefcountedFuture that was already canceled (refcount = 0), it would call Thread.yield() and continue the while(true) loop. It relied on a listener attached to the canceled future to eventually remove it from the map. However, when using virtual threads, many threads could enter this spin loop simultaneously, saturating the underlying carrier threads. This prevented the listener from being scheduled, creating a deadlock where the spinning threads never saw the entry removed. 2. RefcountedFuture was not propagating the cancel() call to its delegate future. This meant that even if all callers canceled their interest in a task, the task would continue to execute in the background, wasting resources and increasing contention. This PR makes the following changes: 1. executeIfNew and maybeJoinExecution are modified to explicitly call inFlightTasks.remove(key, future) if retain() fails. This ensures the spin loop is broken immediately by the next thread to encounter the canceled future, rather than waiting for an asynchronous listener. 2. RefcountedFuture.cancel now calls delegate.cancel(mayInterruptIfRunning) when the internal reference count drops to zero. 3. Thread.yield() and the associated @SuppressWarnings("ThreadPriorityCheck") are removed, as they are no longer necessary. Fixes bazelbuild#28302.
fmeum
approved these changes
Mar 12, 2026
copybara-service bot
pushed a commit
that referenced
this pull request
Mar 13, 2026
Due to limitation of GitHub, the workflow won't have permission to edit labels of a PR when it's triggered by pull_request_review https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#workflows-in-forked-repositories-1 Caused failure in #28938 PiperOrigin-RevId: 883083524 Change-Id: I4320841489e884a17602271afc37b4a03783898b
bazel-io
pushed a commit
to bazel-io/bazel
that referenced
this pull request
Mar 13, 2026
…duplicator. (bazelbuild#28938) The test TaskDeduplicatorTest.executeIfNeeded_executeAndCancelLoop_noErrors sporadically hung during the ExecutorService.close() call. This was due to two issues: 1. In executeIfNew, if a thread encountered a RefcountedFuture that was already canceled (refcount = 0), it would call Thread.yield() and continue the while(true) loop. It relied on a listener attached to the canceled future to eventually remove it from the map. However, when using virtual threads, many threads could enter this spin loop simultaneously, saturating the underlying carrier threads. This prevented the listener from being scheduled, creating a deadlock where the spinning threads never saw the entry removed. 2. RefcountedFuture was not propagating the cancel() call to its delegate future. This meant that even if all callers canceled their interest in a task, the task would continue to execute in the background, wasting resources and increasing contention. This PR makes the following changes: 1. executeIfNew and maybeJoinExecution are modified to explicitly call inFlightTasks.remove(key, future) if retain() fails. This ensures the spin loop is broken immediately by the next thread to encounter the canceled future, rather than waiting for an asynchronous listener. 2. RefcountedFuture.cancel now calls delegate.cancel(mayInterruptIfRunning) when the internal reference count drops to zero. 3. Thread.yield() and the associated @SuppressWarnings("ThreadPriorityCheck") are removed, as they are no longer necessary. Fixes bazelbuild#28302. Closes bazelbuild#28938. PiperOrigin-RevId: 883147973 Change-Id: I28c1db252573a4c39b1a9e53d32e218327340054
bazel-io
pushed a commit
to bazel-io/bazel
that referenced
this pull request
Mar 13, 2026
…duplicator. (bazelbuild#28938) The test TaskDeduplicatorTest.executeIfNeeded_executeAndCancelLoop_noErrors sporadically hung during the ExecutorService.close() call. This was due to two issues: 1. In executeIfNew, if a thread encountered a RefcountedFuture that was already canceled (refcount = 0), it would call Thread.yield() and continue the while(true) loop. It relied on a listener attached to the canceled future to eventually remove it from the map. However, when using virtual threads, many threads could enter this spin loop simultaneously, saturating the underlying carrier threads. This prevented the listener from being scheduled, creating a deadlock where the spinning threads never saw the entry removed. 2. RefcountedFuture was not propagating the cancel() call to its delegate future. This meant that even if all callers canceled their interest in a task, the task would continue to execute in the background, wasting resources and increasing contention. This PR makes the following changes: 1. executeIfNew and maybeJoinExecution are modified to explicitly call inFlightTasks.remove(key, future) if retain() fails. This ensures the spin loop is broken immediately by the next thread to encounter the canceled future, rather than waiting for an asynchronous listener. 2. RefcountedFuture.cancel now calls delegate.cancel(mayInterruptIfRunning) when the internal reference count drops to zero. 3. Thread.yield() and the associated @SuppressWarnings("ThreadPriorityCheck") are removed, as they are no longer necessary. Fixes bazelbuild#28302. Closes bazelbuild#28938. PiperOrigin-RevId: 883147973 Change-Id: I28c1db252573a4c39b1a9e53d32e218327340054
github-merge-queue bot
pushed a commit
that referenced
this pull request
Mar 13, 2026
…n TaskDeduplicator. (#28938) (#28990) The test TaskDeduplicatorTest.executeIfNeeded_executeAndCancelLoop_noErrors sporadically hung during the ExecutorService.close() call. This was due to two issues: 1. In executeIfNew, if a thread encountered a RefcountedFuture that was already canceled (refcount = 0), it would call Thread.yield() and continue the while(true) loop. It relied on a listener attached to the canceled future to eventually remove it from the map. However, when using virtual threads, many threads could enter this spin loop simultaneously, saturating the underlying carrier threads. This prevented the listener from being scheduled, creating a deadlock where the spinning threads never saw the entry removed. 2. RefcountedFuture was not propagating the cancel() call to its delegate future. This meant that even if all callers canceled their interest in a task, the task would continue to execute in the background, wasting resources and increasing contention. This PR makes the following changes: 1. executeIfNew and maybeJoinExecution are modified to explicitly call inFlightTasks.remove(key, future) if retain() fails. This ensures the spin loop is broken immediately by the next thread to encounter the canceled future, rather than waiting for an asynchronous listener. 2. RefcountedFuture.cancel now calls delegate.cancel(mayInterruptIfRunning) when the internal reference count drops to zero. 3. Thread.yield() and the associated @SuppressWarnings("ThreadPriorityCheck") are removed, as they are no longer necessary. Fixes #28302. Closes #28938. PiperOrigin-RevId: 883147973 Change-Id: I28c1db252573a4c39b1a9e53d32e218327340054 Commit a0760f1 Co-authored-by: Tiago Quelhas <tjgq@google.com>
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.
The test TaskDeduplicatorTest.executeIfNeeded_executeAndCancelLoop_noErrors sporadically hung during the ExecutorService.close() call. This was due to two primary issues:
In executeIfNew, if a thread encountered a RefcountedFuture that was already canceled (refcount = 0), it would call Thread.yield() and continue the while(true) loop. It relied on a listener attached to the canceled future to eventually remove it from the map. However, when using virtual threads, many threads could enter this spin loop simultaneously, saturating the underlying carrier threads. This prevented the listener from being scheduled, creating a deadlock where the spinning threads never saw the entry removed.
RefcountedFuture was not propagating the cancel() call to its delegate future. This meant that even if all callers canceled their interest in a task, the task would continue to execute in the background, wasting resources and increasing contention.
This PR makes the following changes:
executeIfNew and maybeJoinExecution are modified to explicitly call inFlightTasks.remove(key, future) if retain() fails. This ensures the spin loop is broken immediately by the next thread to encounter the canceled future, rather than waiting for an asynchronous listener.
RefcountedFuture.cancel now calls delegate.cancel(mayInterruptIfRunning) when the internal reference count drops to zero.
Thread.yield() and the associated @SuppressWarnings("ThreadPriorityCheck") are removed, as they are no longer necessary.
Fixes #28302.