-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only wipe the whole sandbox base during the first build in a server.
During the lifetime of a Bazel server, assign unique identifiers to each sandboxed action so that their symlink trees are guaranteed to be disjoint as they are keyed on that identifier. This was in theory already happening... but it actually wasn't and there was no test to validate this assumption. With that done, there is no need to ensure that the sandbox base is clean before a build -- unless we are the very first build of a server, in which case we must ensure we don't clash with possible leftovers from a past server. Note that we already clean up each action's tree as soon as the action completes, so the only thing we are trying to clean up here are stale files that may be left if those individual deletions didn't work (e.g. because there were still stray processes writing to the directories) or if --sandbox_debug was in use. This is a prerequisite before making deletions asynchronous for two reasons: first, we don't want to delay build starts if old deletions are still ongoing; and, second, we don't want to schedule too broad deletions that could step over subsequent builds (i.e. we only want to delete the contents of the *-sandbox directories, which contain one subdirectory per action, and not the whole tree). Lastly, add a test for this expected behavior (which is what actually triggered the fix) and for the fact that we expect the identifiers to be always different. Partially addresses #7527. RELNOTES: None. PiperOrigin-RevId: 243635557
- Loading branch information
1 parent
460843f
commit b93e61b
Showing
8 changed files
with
210 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright 2019 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# Test sandboxing spawn strategy | ||
# | ||
|
||
set -euo pipefail | ||
|
||
# Load the test setup defined in the parent directory | ||
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
source "${CURRENT_DIR}/../integration_test_setup.sh" \ | ||
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; } | ||
|
||
function tear_down() { | ||
bazel clean --expunge | ||
bazel shutdown | ||
rm -rf pkg | ||
} | ||
|
||
function test_sandbox_base_contents_wiped_only_on_startup() { | ||
mkdir pkg | ||
cat >pkg/BUILD <<EOF | ||
genrule(name = "pkg", outs = ["pkg.out"], cmd = "echo >\$@") | ||
EOF | ||
|
||
local output_base="$(bazel info output_base)" | ||
|
||
do_build() { | ||
bazel build --genrule_strategy=sandboxed //pkg | ||
} | ||
|
||
do_build >"${TEST_log}" 2>&1 || fail "Expected build to succeed" | ||
find "${output_base}" >>"${TEST_log}" 2>&1 || true | ||
|
||
local sandbox_dir="$(echo "${output_base}/sandbox"/*-sandbox)" | ||
[[ -d "${sandbox_dir}" ]] \ | ||
|| fail "${sandbox_dir} is missing; prematurely deleted?" | ||
|
||
local garbage="${output_base}/sandbox/garbage" | ||
mkdir -p "${garbage}/some/nested/contents" | ||
do_build >"${TEST_log}" 2>&1 || fail "Expected build to succeed" | ||
expect_not_log "Deleting stale sandbox" | ||
[[ -d "${garbage}" ]] \ | ||
|| fail "Spurious contents deleted from sandbox base too early" | ||
|
||
bazel shutdown | ||
do_build >"${TEST_log}" 2>&1 || fail "Expected build to succeed" | ||
expect_log "Deleting stale sandbox" | ||
[[ ! -d "${garbage}" ]] \ | ||
|| fail "sandbox base was not cleaned on restart" | ||
} | ||
|
||
function test_sandbox_base_can_be_rm_rfed() { | ||
mkdir pkg | ||
cat >pkg/BUILD <<EOF | ||
genrule(name = "pkg", outs = ["pkg.out"], cmd = "echo >\$@") | ||
EOF | ||
|
||
local output_base="$(bazel info output_base)" | ||
|
||
do_build() { | ||
bazel build --genrule_strategy=sandboxed //pkg | ||
} | ||
|
||
do_build >"${TEST_log}" 2>&1 || fail "Expected build to succeed" | ||
find "${output_base}" >>"${TEST_log}" 2>&1 || true | ||
|
||
local sandbox_base="${output_base}/sandbox" | ||
[[ -d "${sandbox_base}" ]] \ | ||
|| fail "${sandbox_base} is missing; build did not use sandboxing?" | ||
|
||
# Ensure the sandbox base does not contain protected files that would prevent | ||
# a simple "rm -rf" from working under an unprivileged user. | ||
rm -rf "${sandbox_base}" || fail "Cannot clean sandbox base" | ||
|
||
# And now ensure Bazel reconstructs the sandbox base on a second build. | ||
do_build >"${TEST_log}" 2>&1 || fail "Expected build to succeed" | ||
} | ||
|
||
function test_sandbox_old_contents_not_reused_in_consecutive_builds() { | ||
mkdir pkg | ||
cat >pkg/BUILD <<EOF | ||
genrule( | ||
name = "pkg", | ||
srcs = ["pkg.in"], | ||
outs = ["pkg.out"], | ||
cmd = "cp \$(location :pkg.in) \$@", | ||
) | ||
EOF | ||
touch pkg/pkg.in | ||
|
||
for i in $(seq 5); do | ||
# Ensure that, even if we don't clean up the sandbox at all (with | ||
# --sandbox_debug), consecutive builds don't step on each other by trying to | ||
# reuse previous spawn identifiers. | ||
bazel build --genrule_strategy=sandboxed --sandbox_debug //pkg \ | ||
>"${TEST_log}" 2>&1 || fail "Expected build to succeed" | ||
echo foo >>pkg/pkg.in | ||
done | ||
} | ||
|
||
run_suite "sandboxing" |