Skip to content

Commit aab07f1

Browse files
yuyue730copybara-github
authored andcommitted
Move Profiler.instance() closes before executing runtime.commandComplete().
PiperOrigin-RevId: 709850756 Change-Id: If4d4ccbfd716a2973dad4e0b5dcc2ecbf1db57fe
1 parent 3eb3c12 commit aab07f1

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java

+3
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,9 @@ private BlazeCommandResult execExclusively(
745745
return result;
746746
} finally {
747747
try {
748+
// Profiler might still be running when an exception is thrown before BuildCompleteEvent is
749+
// emitted or BlazeModule#completeCommand() is called. So we still need to try to stop the
750+
// profiler here.
748751
Profiler.instance().stop();
749752
if (profilerStartedEvent.getProfile() instanceof LocalInstrumentationOutput profile) {
750753
profile.makeConvenienceLink();

src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java

+9
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,15 @@ public BlazeCommandResult afterCommand(
765765
}
766766
env.getBlazeWorkspace().clearEventBus();
767767

768+
// Some module's commandComplete() relies on the stoppage of profiler. And it is impossible the
769+
// profiler is needed after all `BlazeModule.afterCommand`s are executed.
770+
// See b/331203854#comment124 for more details.
771+
try {
772+
Profiler.instance().stop();
773+
} catch (IOException e) {
774+
env.getReporter().handle(Event.error("Error while writing profile file: " + e.getMessage()));
775+
}
776+
768777
for (BlazeModule module : blazeModules) {
769778
try (SilentCloseable closeable = Profiler.instance().profile(module + ".commandComplete")) {
770779
module.commandComplete();

src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,16 @@ public void buildComplete(BuildCompleteEvent event) {
192192
}
193193
}
194194
if (profileEvent != null && profileEvent.getProfile() != null) {
195-
// This leads to missing the afterCommand profiles of the other modules in the profile.
196-
// Since the BEP currently shuts down at the BuildCompleteEvent, we cannot just move posting
197-
// the BuildToolLogs to afterCommand of this module.
195+
// The profiler has to be stopped before `BuildEventServiceModule#afterCommand` is called,
196+
// especially when it is a bep artifact. An unstopped bep artifact could lead to a deadlock
197+
// in `BuildEventServiceModule#afterCommand`.
198+
//
199+
// We choose to stop profiler here instead of in `BuildSummaryStatsModule#afterCommand` so
200+
// that no ordering between GoogleBuildSummaryStatsModule and BuildEventServiceModule's
201+
// `afterCommand`s needs to be assumed. See b/253394502.
202+
//
203+
// Stopping the profiler here leads to missing the afterCommand profiles of the other
204+
// modules in the profile, which is a compromise we are willing to make.
198205
try {
199206
Profiler.instance().stop();
200207
profileEvent.getProfile().publish(event.getResult().getBuildToolLogCollection());

0 commit comments

Comments
 (0)