|
1 | 1 | #!/bin/bash |
2 | 2 | set -e |
3 | 3 |
|
| 4 | + |
| 5 | +# Avoid virtualenv/pip trying to download/upgrade tools from PyPI on host |
| 6 | +export VIRTUALENV_NO_DOWNLOAD=1 |
| 7 | +export PIP_DISABLE_PIP_VERSION_CHECK=1 |
| 8 | + |
| 9 | +# Pass these environment variables to the cibuildwheel Docker container |
| 10 | +export CIBW_ENVIRONMENT="VIRTUALENV_NO_DOWNLOAD=1 PIP_DISABLE_PIP_VERSION_CHECK=1" |
| 11 | +export CIBW_DEPENDENCY_VERSIONS="latest" |
| 12 | + |
4 | 13 | # If running locally (not on Kokoro), authenticate with gcloud. |
5 | 14 | if [ -z "${KOKORO_BUILD_ID}" ]; then |
6 | 15 | if ! gcloud auth application-default print-access-token --quiet > /dev/null; then |
7 | 16 | gcloud auth application-default login |
8 | 17 | fi |
9 | 18 | fi |
10 | 19 |
|
11 | | -pip install -U keyring keyrings.google-artifactregistry-auth twine cibuildwheel |
| 20 | +# We use --no-cache-dir to force pip to download packages fresh and bypass the local |
| 21 | +# cache. In Kokoro/RBE sandboxed environments, writing to the default cache directory |
| 22 | +# (~/.cache/pip) can encounter permission/sandbox restrictions or lead to stale |
| 23 | +# dependency resolution. Disabling the cache ensures a reliable, reproducible install. |
| 24 | +pip install --no-cache-dir -U keyring keyrings.google-artifactregistry-auth twine cibuildwheel |
| 25 | + |
| 26 | +# Patch cibuildwheel at runtime to bypass the RBE stdout buffering deadlock. |
| 27 | +# The RBE proxy buffers the persistent container bash stdout. By appending a 4KB |
| 28 | +# padding line to the end of every command output, we force the proxy to flush the |
| 29 | +# buffer immediately. We then read and discard this padding to keep the stream clean. |
| 30 | +OCI_PATH=$(python3 -c "import cibuildwheel.oci_container; print(cibuildwheel.oci_container.__file__)") |
| 31 | +echo "Patching cibuildwheel at $OCI_PATH..." |
| 32 | + |
| 33 | +cat << 'EOF' > patch_oci.py |
| 34 | +import sys |
| 35 | +import re |
| 36 | +
|
| 37 | +path = sys.argv[1] |
| 38 | +with open(path, 'r') as f: |
| 39 | + content = f.read() |
| 40 | +
|
| 41 | +# 1. Force a 4KB flush at the end of every command execution |
| 42 | +target_write = 'printf "%04d%s\\n" $? {end_of_message}' |
| 43 | +replacement_write = 'printf "%04d%s\\n%4096s\\n" $? {end_of_message} " "' |
| 44 | +if target_write in content: |
| 45 | + content = content.replace(target_write, replacement_write) |
| 46 | + print("Patched write loop.") |
| 47 | +
|
| 48 | +# 2. Read and discard the 4KB padding to keep the stream clean |
| 49 | +target_read = """ # add the last line to output, without the footer |
| 50 | + output_io.write(line[0:footer_offset]) |
| 51 | + output_io.flush() |
| 52 | + break""" |
| 53 | +
|
| 54 | +replacement_read = """ # add the last line to output, without the footer |
| 55 | + output_io.write(line[0:footer_offset]) |
| 56 | + output_io.flush() |
| 57 | + # Read and discard the 4KB padding line to clear the stream! |
| 58 | + self.bash_stdout.readline() |
| 59 | + break""" |
| 60 | +
|
| 61 | +if target_read in content: |
| 62 | + content = content.replace(target_read, replacement_read) |
| 63 | + print("Patched read loop.") |
| 64 | +
|
| 65 | +# 3. Patch copy_into else block using regex |
| 66 | +pattern = re.compile(r' else:.*? def copy_out', re.DOTALL) |
| 67 | +
|
| 68 | +replacement_copy = """ else: |
| 69 | + self.call(["mkdir", "-p", to_path.parent]) |
| 70 | + # Use native docker cp to copy the file, avoiding stdin EOF deadlocks in RBE |
| 71 | + subprocess.run( |
| 72 | + [ |
| 73 | + self.engine.name, |
| 74 | + "cp", |
| 75 | + str(from_path), |
| 76 | + f"{self.name}:{to_path}", |
| 77 | + ], |
| 78 | + check=True, |
| 79 | + ) |
| 80 | +
|
| 81 | + def copy_out""" |
| 82 | +
|
| 83 | +if pattern.search(content): |
| 84 | + content = pattern.sub(replacement_copy, content) |
| 85 | + print("Patched copy_into using regex.") |
| 86 | +else: |
| 87 | + print("Error: copy_into pattern not found!") |
| 88 | + sys.exit(1) |
| 89 | +
|
| 90 | +with open(path, 'w') as f: |
| 91 | + f.write(content) |
| 92 | +
|
| 93 | +print("Successfully patched oci_container.py!") |
| 94 | +EOF |
| 95 | + |
| 96 | +python3 patch_oci.py "$OCI_PATH" |
| 97 | +rm patch_oci.py |
12 | 98 |
|
13 | 99 | REPO_DIR=$(mktemp -d) |
14 | 100 | echo "Created temporary directory: ${REPO_DIR}" |
@@ -52,17 +138,121 @@ cp -r "${SRC_DIR}"/{*,.*} . 2>/dev/null || true |
52 | 138 | cp -r "${SRC_DIR}"/release/* . 2>/dev/null || true |
53 | 139 | rm -rf cel_expr_python/*_test.py |
54 | 140 |
|
| 141 | +echo "Downloading bazelisk on host..." |
| 142 | +curl -LO https://github.com/bazelbuild/bazelisk/releases/download/v1.19.0/bazelisk-linux-amd64 |
| 143 | +chmod +x bazelisk-linux-amd64 |
| 144 | + |
55 | 145 | # Check if pyproject.toml exists before running sed |
56 | 146 | if [ -f pyproject.toml ]; then |
57 | 147 | sed -i "" "s/\$VERSION/${VERSION}/g" pyproject.toml || sed -i "s/\$VERSION/${VERSION}/g" pyproject.toml |
58 | 148 | fi |
59 | 149 |
|
| 150 | +export CIBW_CONTAINER_ENGINE_EXTRA_ARGS="--network=host" |
| 151 | + |
60 | 152 | echo "Running cibuildwheel: ${CIBWHEEL_BIN}" |
61 | 153 | # Default CIBWHEEL_BIN if not set |
62 | 154 | if [ -z "${CIBWHEEL_BIN}" ]; then |
63 | 155 | CIBWHEEL_BIN="python3 -m cibuildwheel" |
64 | 156 | fi |
65 | | -${CIBWHEEL_BIN} --platform linux --output-dir dist |
| 157 | + |
| 158 | +echo "Installing diagnostic tools (psmisc, strace) on host..." |
| 159 | +# We try to install them, but don't fail the build if we can't (e.g. if no sudo or apt) |
| 160 | +sudo apt-get update && sudo apt-get install -y psmisc strace || echo "Failed to install diagnostic tools, proceeding anyway..." |
| 161 | + |
| 162 | +echo "Running cibuildwheel in background..." |
| 163 | +${CIBWHEEL_BIN} --platform linux --output-dir dist > cibuildwheel.log 2>&1 & |
| 164 | +CIBW_PID=$! |
| 165 | + |
| 166 | +echo "Started cibuildwheel in background with PID $CIBW_PID" |
| 167 | + |
| 168 | +# Poll the log file waiting for the hang |
| 169 | +# We look for the "mkdir -p" line followed by no activity for 60 seconds. |
| 170 | +TIMEOUT=900 # 15 minutes total timeout |
| 171 | +ELAPSED=0 |
| 172 | +LAST_SIZE=0 |
| 173 | +STUCK_COUNT=0 |
| 174 | +HANG_DETECTED=false |
| 175 | + |
| 176 | +while kill -0 $CIBW_PID 2>/dev/null; do |
| 177 | + if [ -f cibuildwheel.log ]; then |
| 178 | + # Check if the log contains the test setup line |
| 179 | + if grep -q "mkdir -p" cibuildwheel.log; then |
| 180 | + CURRENT_SIZE=$(stat -c%s cibuildwheel.log) |
| 181 | + if [ "$CURRENT_SIZE" -eq "$LAST_SIZE" ]; then |
| 182 | + # Log size hasn't changed. If this persists for 60 seconds, we assume it is stuck. |
| 183 | + STUCK_COUNT=$((STUCK_COUNT + 10)) |
| 184 | + echo "Log size unchanged for ${STUCK_COUNT}s at mkdir -p..." |
| 185 | + if [ $STUCK_COUNT -ge 60 ]; then |
| 186 | + HANG_DETECTED=true |
| 187 | + break |
| 188 | + fi |
| 189 | + else |
| 190 | + STUCK_COUNT=0 |
| 191 | + LAST_SIZE=$CURRENT_SIZE |
| 192 | + fi |
| 193 | + fi |
| 194 | + fi |
| 195 | + |
| 196 | + sleep 10 |
| 197 | + ELAPSED=$((ELAPSED + 10)) |
| 198 | + if [ $ELAPSED -ge $TIMEOUT ]; then |
| 199 | + echo "Timeout waiting for build to complete." |
| 200 | + break |
| 201 | + fi |
| 202 | +done |
| 203 | + |
| 204 | +if [ "$HANG_DETECTED" = "true" ]; then |
| 205 | + echo "====================================================" |
| 206 | + echo "!!! DETECTED HANG AT mkdir -p !!! STARTING DIAGNOSTICS" |
| 207 | + echo "====================================================" |
| 208 | + |
| 209 | + echo "=== HOST PROCESSES ===" |
| 210 | + ps aux |
| 211 | + |
| 212 | + echo "=== PROCESS TREE ===" |
| 213 | + pstree -p -a || echo "pstree not available" |
| 214 | + |
| 215 | + echo "=== DOCKER CONTAINERS ===" |
| 216 | + docker ps -a |
| 217 | + |
| 218 | + CONTAINER_ID=$(docker ps -q | head -n 1) |
| 219 | + if [ -n "$CONTAINER_ID" ]; then |
| 220 | + echo "=== CONTAINER PROCESSES ($CONTAINER_ID) ===" |
| 221 | + docker exec "$CONTAINER_ID" ps aux |
| 222 | + |
| 223 | + echo "=== CONTAINER LSOF ===" |
| 224 | + docker exec "$CONTAINER_ID" lsof || echo "lsof not available" |
| 225 | + |
| 226 | + echo "=== CONTAINER DOCKER INSPECT ===" |
| 227 | + docker inspect "$CONTAINER_ID" |
| 228 | + |
| 229 | + echo "=== STRACE DOCKER PROCESSES ===" |
| 230 | + DOCKER_PID=$(pgrep -f "docker start|docker exec" | head -n 1) |
| 231 | + if [ -n "$DOCKER_PID" ]; then |
| 232 | + echo "Stracing host docker process $DOCKER_PID for 15 seconds..." |
| 233 | + timeout 15 strace -p "$DOCKER_PID" -f || true |
| 234 | + fi |
| 235 | + else |
| 236 | + echo "No active docker container found!" |
| 237 | + fi |
| 238 | + |
| 239 | + echo "=== LAST 100 LINES OF CIBUILDWHEEL LOG ===" |
| 240 | + tail -n 100 cibuildwheel.log |
| 241 | + |
| 242 | + echo "Diagnostics complete. Killing cibuildwheel." |
| 243 | + kill -9 $CIBW_PID |
| 244 | + exit 99 |
| 245 | +fi |
| 246 | + |
| 247 | +# If it didn't hang, wait for it to finish and print the log |
| 248 | +wait $CIBW_PID |
| 249 | +RC=$? |
| 250 | +echo "=== CIBUILDWHEEL LOG ===" |
| 251 | +cat cibuildwheel.log |
| 252 | +if [ $RC -ne 0 ]; then |
| 253 | + echo "cibuildwheel failed with exit code $RC" |
| 254 | + exit $RC |
| 255 | +fi |
66 | 256 |
|
67 | 257 | if [ "${DRY_RUN}" = "true" ]; then |
68 | 258 | echo "[DRY RUN] Skipping upload to PyPI exit gate." |
|
0 commit comments