|
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 os |
| 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 not in content: |
| 45 | + print("Error: target_write not found!") |
| 46 | + sys.exit(1) |
| 47 | +content = content.replace(target_write, replacement_write) |
| 48 | +
|
| 49 | +# 2. Read and discard the 4KB padding to keep the stream clean |
| 50 | +target_read = """ # add the last line to output, without the footer |
| 51 | + output_io.write(line[0:footer_offset]) |
| 52 | + output_io.flush() |
| 53 | + break""" |
| 54 | +
|
| 55 | +replacement_read = """ # add the last line to output, without the footer |
| 56 | + output_io.write(line[0:footer_offset]) |
| 57 | + output_io.flush() |
| 58 | + # Read and discard the 4KB padding line to clear the stream! |
| 59 | + self.bash_stdout.readline() |
| 60 | + break""" |
| 61 | +
|
| 62 | +if target_read not in content: |
| 63 | + print("Error: target_read not found!") |
| 64 | + sys.exit(1) |
| 65 | +content = content.replace(target_read, replacement_read) |
| 66 | +
|
| 67 | +with open(path, 'w') as f: |
| 68 | + f.write(content) |
| 69 | +
|
| 70 | +print("Successfully patched oci_container.py to bypass RBE buffering deadlock!") |
| 71 | +EOF |
| 72 | + |
| 73 | +python3 patch_oci.py "$OCI_PATH" |
| 74 | +rm patch_oci.py |
12 | 75 |
|
13 | 76 | REPO_DIR=$(mktemp -d) |
14 | 77 | echo "Created temporary directory: ${REPO_DIR}" |
@@ -52,17 +115,121 @@ cp -r "${SRC_DIR}"/{*,.*} . 2>/dev/null || true |
52 | 115 | cp -r "${SRC_DIR}"/release/* . 2>/dev/null || true |
53 | 116 | rm -rf cel_expr_python/*_test.py |
54 | 117 |
|
| 118 | +echo "Downloading bazelisk on host..." |
| 119 | +curl -LO https://github.com/bazelbuild/bazelisk/releases/download/v1.19.0/bazelisk-linux-amd64 |
| 120 | +chmod +x bazelisk-linux-amd64 |
| 121 | + |
55 | 122 | # Check if pyproject.toml exists before running sed |
56 | 123 | if [ -f pyproject.toml ]; then |
57 | 124 | sed -i "" "s/\$VERSION/${VERSION}/g" pyproject.toml || sed -i "s/\$VERSION/${VERSION}/g" pyproject.toml |
58 | 125 | fi |
59 | 126 |
|
| 127 | +export CIBW_CONTAINER_ENGINE_EXTRA_ARGS="--network=host" |
| 128 | + |
60 | 129 | echo "Running cibuildwheel: ${CIBWHEEL_BIN}" |
61 | 130 | # Default CIBWHEEL_BIN if not set |
62 | 131 | if [ -z "${CIBWHEEL_BIN}" ]; then |
63 | 132 | CIBWHEEL_BIN="python3 -m cibuildwheel" |
64 | 133 | fi |
65 | | -${CIBWHEEL_BIN} --platform linux --output-dir dist |
| 134 | + |
| 135 | +echo "Installing diagnostic tools (psmisc, strace) on host..." |
| 136 | +# We try to install them, but don't fail the build if we can't (e.g. if no sudo or apt) |
| 137 | +sudo apt-get update && sudo apt-get install -y psmisc strace || echo "Failed to install diagnostic tools, proceeding anyway..." |
| 138 | + |
| 139 | +echo "Running cibuildwheel in background..." |
| 140 | +${CIBWHEEL_BIN} --platform linux --output-dir dist > cibuildwheel.log 2>&1 & |
| 141 | +CIBW_PID=$! |
| 142 | + |
| 143 | +echo "Started cibuildwheel in background with PID $CIBW_PID" |
| 144 | + |
| 145 | +# Poll the log file waiting for the hang |
| 146 | +# We look for the "mkdir -p" line followed by no activity for 60 seconds. |
| 147 | +TIMEOUT=900 # 15 minutes total timeout |
| 148 | +ELAPSED=0 |
| 149 | +LAST_SIZE=0 |
| 150 | +STUCK_COUNT=0 |
| 151 | +HANG_DETECTED=false |
| 152 | + |
| 153 | +while kill -0 $CIBW_PID 2>/dev/null; do |
| 154 | + if [ -f cibuildwheel.log ]; then |
| 155 | + # Check if the log contains the test setup line |
| 156 | + if grep -q "mkdir -p" cibuildwheel.log; then |
| 157 | + CURRENT_SIZE=$(stat -c%s cibuildwheel.log) |
| 158 | + if [ "$CURRENT_SIZE" -eq "$LAST_SIZE" ]; then |
| 159 | + # Log size hasn't changed. If this persists for 60 seconds, we assume it is stuck. |
| 160 | + STUCK_COUNT=$((STUCK_COUNT + 10)) |
| 161 | + echo "Log size unchanged for ${STUCK_COUNT}s at mkdir -p..." |
| 162 | + if [ $STUCK_COUNT -ge 60 ]; then |
| 163 | + HANG_DETECTED=true |
| 164 | + break |
| 165 | + fi |
| 166 | + else |
| 167 | + STUCK_COUNT=0 |
| 168 | + LAST_SIZE=$CURRENT_SIZE |
| 169 | + fi |
| 170 | + fi |
| 171 | + fi |
| 172 | + |
| 173 | + sleep 10 |
| 174 | + ELAPSED=$((ELAPSED + 10)) |
| 175 | + if [ $ELAPSED -ge $TIMEOUT ]; then |
| 176 | + echo "Timeout waiting for build to complete." |
| 177 | + break |
| 178 | + fi |
| 179 | +done |
| 180 | + |
| 181 | +if [ "$HANG_DETECTED" = "true" ]; then |
| 182 | + echo "====================================================" |
| 183 | + echo "!!! DETECTED HANG AT mkdir -p !!! STARTING DIAGNOSTICS" |
| 184 | + echo "====================================================" |
| 185 | + |
| 186 | + echo "=== HOST PROCESSES ===" |
| 187 | + ps aux |
| 188 | + |
| 189 | + echo "=== PROCESS TREE ===" |
| 190 | + pstree -p -a || echo "pstree not available" |
| 191 | + |
| 192 | + echo "=== DOCKER CONTAINERS ===" |
| 193 | + docker ps -a |
| 194 | + |
| 195 | + CONTAINER_ID=$(docker ps -q | head -n 1) |
| 196 | + if [ -n "$CONTAINER_ID" ]; then |
| 197 | + echo "=== CONTAINER PROCESSES ($CONTAINER_ID) ===" |
| 198 | + docker exec "$CONTAINER_ID" ps aux |
| 199 | + |
| 200 | + echo "=== CONTAINER LSOF ===" |
| 201 | + docker exec "$CONTAINER_ID" lsof || echo "lsof not available" |
| 202 | + |
| 203 | + echo "=== CONTAINER DOCKER INSPECT ===" |
| 204 | + docker inspect "$CONTAINER_ID" |
| 205 | + |
| 206 | + echo "=== STRACE DOCKER PROCESSES ===" |
| 207 | + DOCKER_PID=$(pgrep -f "docker start|docker exec" | head -n 1) |
| 208 | + if [ -n "$DOCKER_PID" ]; then |
| 209 | + echo "Stracing host docker process $DOCKER_PID for 15 seconds..." |
| 210 | + timeout 15 strace -p "$DOCKER_PID" -f || true |
| 211 | + fi |
| 212 | + else |
| 213 | + echo "No active docker container found!" |
| 214 | + fi |
| 215 | + |
| 216 | + echo "=== LAST 100 LINES OF CIBUILDWHEEL LOG ===" |
| 217 | + tail -n 100 cibuildwheel.log |
| 218 | + |
| 219 | + echo "Diagnostics complete. Killing cibuildwheel." |
| 220 | + kill -9 $CIBW_PID |
| 221 | + exit 99 |
| 222 | +fi |
| 223 | + |
| 224 | +# If it didn't hang, wait for it to finish and print the log |
| 225 | +wait $CIBW_PID |
| 226 | +RC=$? |
| 227 | +echo "=== CIBUILDWHEEL LOG ===" |
| 228 | +cat cibuildwheel.log |
| 229 | +if [ $RC -ne 0 ]; then |
| 230 | + echo "cibuildwheel failed with exit code $RC" |
| 231 | + exit $RC |
| 232 | +fi |
66 | 233 |
|
67 | 234 | if [ "${DRY_RUN}" = "true" ]; then |
68 | 235 | echo "[DRY RUN] Skipping upload to PyPI exit gate." |
|
0 commit comments