Skip to content

Commit

Permalink
Be resilient to the case where the worker gets closed due to memory p…
Browse files Browse the repository at this point in the history
…ressure while the host Skyframe thread is inactive
  • Loading branch information
Wyverald committed May 2, 2024
1 parent 5cc3839 commit 92a3661
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class RepoFetchingSkyKeyComputeState implements SkyKeyComputeState {
/** The executor service that manages the worker thread. */
// We hold on to this alongside `workerFuture` because it offers a convenient mechanism to make
// sure the worker thread has shut down (with its blocking `close()` method).
private ListeningExecutorService workerExecutorService;
ListeningExecutorService workerExecutorService;

private final String repoName;

Expand Down Expand Up @@ -136,6 +136,7 @@ public void close() {
if (myWorkerFuture != null) {
myWorkerFuture.cancel(true);
}
workerExecutorService.shutdownNow();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ public RepositoryDirectoryValue.Builder fetch(
return fetchInternal(args);
}
var state = env.getState(() -> new RepoFetchingSkyKeyComputeState(rule.getName()));
if (state.workerExecutorService.isShutdown()) {
// If we get here and the worker executor is shut down, this can only mean that the worker
// future was cancelled while we (the host Skyframe thread) were inactive (as in, having
// returned `null` but not yet restarted). So we wait for the previous worker thread to finish
// first.
// TODO: instead of this complicated dance, consider making it legal for
// `SkyKeyComputeState#close()` to block. This would undo the advice added in commit 8ef0a51,
// but would allow us to merge `close()` and `closeAndWaitForTermination()` and avoid some
// headache.
state.closeAndWaitForTermination();
}
boolean shouldShutDownWorkerExecutorInFinally = true;
try {
var workerFuture = state.workerFuture;
Expand Down

0 comments on commit 92a3661

Please sign in to comment.