Skip to content

Commit 73a76a8

Browse files
coeuvrecopybara-github
authored andcommitted
Fix that bepTransports are not properly closed if --bes_timeout is set.
Related bazelbuild#14576. PiperOrigin-RevId: 424026771
1 parent f6dbd1e commit 73a76a8

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.collect.ImmutableSet;
2424
import com.google.common.collect.Iterables;
2525
import com.google.common.flogger.GoogleLogger;
26+
import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
2627
import com.google.common.util.concurrent.Futures;
2728
import com.google.common.util.concurrent.ListenableFuture;
2829
import com.google.common.util.concurrent.MoreExecutors;
@@ -533,14 +534,26 @@ private void waitForBuildEventTransportsToClose(
533534
final ListenableFuture<Void> enclosingFuture =
534535
Futures.nonCancellationPropagating(closeFuture);
535536

536-
closeFutureWithTimeout =
537+
ListenableFuture<Void> timeoutFuture =
537538
Futures.withTimeout(
538539
enclosingFuture,
539540
bepTransport.getTimeout().toMillis(),
540541
TimeUnit.MILLISECONDS,
541542
timeoutExecutor);
542-
closeFutureWithTimeout.addListener(
543-
timeoutExecutor::shutdown, MoreExecutors.directExecutor());
543+
timeoutFuture.addListener(timeoutExecutor::shutdown, MoreExecutors.directExecutor());
544+
545+
// Cancellation is not propagated to the `closeFuture` for the reasons above. But in
546+
// order to cancel the returned future by our explicit mechanism elsewhere in this
547+
// class, we need to delegate the `cancel` to `closeFuture` so that cancellation
548+
// from Futures.withTimeout is ignored and cancellation from our mechanism is properly
549+
// handled.
550+
closeFutureWithTimeout =
551+
new SimpleForwardingListenableFuture<>(timeoutFuture) {
552+
@Override
553+
public boolean cancel(boolean mayInterruptIfRunning) {
554+
return closeFuture.cancel(mayInterruptIfRunning);
555+
}
556+
};
544557
}
545558
builder.put(bepTransport, closeFutureWithTimeout);
546559
});

src/test/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModuleTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.common.base.Preconditions;
2525
import com.google.common.collect.ImmutableList;
2626
import com.google.common.collect.ImmutableMap;
27+
import com.google.common.collect.ImmutableSet;
2728
import com.google.common.collect.Iterables;
2829
import com.google.common.collect.Sets;
2930
import com.google.common.util.concurrent.Uninterruptibles;
@@ -317,8 +318,13 @@ public void testAfterCommand_waitForUploadComplete_slowFullCloseError() throws E
317318
"--bes_backend=inprocess",
318319
"--bes_upload_mode=WAIT_FOR_UPLOAD_COMPLETE",
319320
"--bes_timeout=5s");
321+
ImmutableSet<BuildEventTransport> bepTransports = besModule.getBepTransports();
322+
assertThat(bepTransports).hasSize(1);
320323
afterBuildCommand();
321324
assertContainsError("The Build Event Protocol upload timed out");
325+
for (BuildEventTransport bepTransport : bepTransports) {
326+
assertThat(bepTransport.close().isDone()).isTrue();
327+
}
322328
}
323329

324330
@Test

0 commit comments

Comments
 (0)