From 2bc85d5d9967de1fcabca0af0562e015bfe8aadf Mon Sep 17 00:00:00 2001 From: Dang Dat Date: Thu, 5 Nov 2026 12:25:05 +0700 Subject: [PATCH] ci: run the memory leak check on release tags and memory-check PRs Run the check on web and Android in profile mode against a local backend, in parallel with the security scan on tags. Add the Android profile build type and the docs. Refs #4365 --- .github/workflows/image-sentry.yaml | 8 + .../workflows/memory-leak-check-label.yaml | 17 +++ .github/workflows/memory-leak-check.yaml | 141 ++++++++++++++++++ android/app/build.gradle | 11 ++ docs/dev/memory-leak-check.md | 63 ++++++++ scripts/memory-leak-check.sh | 112 ++++++++++++++ 6 files changed, 352 insertions(+) create mode 100644 .github/workflows/memory-leak-check-label.yaml create mode 100644 .github/workflows/memory-leak-check.yaml create mode 100644 docs/dev/memory-leak-check.md create mode 100644 scripts/memory-leak-check.sh diff --git a/.github/workflows/image-sentry.yaml b/.github/workflows/image-sentry.yaml index ec738deb5..ba809d2ce 100644 --- a/.github/workflows/image-sentry.yaml +++ b/.github/workflows/image-sentry.yaml @@ -148,3 +148,11 @@ jobs: uses: ./.github/workflows/security-release.yaml # zizmor: ignore[self-repository] # the report secrets must reach the called workflow secrets: inherit # zizmor: ignore[secrets-inherit] + + memory-leak-check: + name: Memory leak check + # manual image builds aren't release tags + if: github.event_name == 'push' + permissions: + contents: read + uses: ./.github/workflows/memory-leak-check.yaml # zizmor: ignore[self-repository] diff --git a/.github/workflows/memory-leak-check-label.yaml b/.github/workflows/memory-leak-check-label.yaml new file mode 100644 index 000000000..bc4a92cfa --- /dev/null +++ b/.github/workflows/memory-leak-check-label.yaml @@ -0,0 +1,17 @@ +name: Memory leak check (label) + +# Runs the memory check on any PR labelled `memory-check`, for example a suspected leak fix. +on: + pull_request: + types: [labeled, synchronize] + +permissions: + contents: read + +jobs: + memory: + name: Memory leak check + if: >- + contains(github.event.pull_request.labels.*.name, 'memory-check') + && (github.event.action != 'labeled' || github.event.label.name == 'memory-check') + uses: ./.github/workflows/memory-leak-check.yaml # zizmor: ignore[self-repository] diff --git a/.github/workflows/memory-leak-check.yaml b/.github/workflows/memory-leak-check.yaml new file mode 100644 index 000000000..1b5ff2e0f --- /dev/null +++ b/.github/workflows/memory-leak-check.yaml @@ -0,0 +1,141 @@ +name: Memory leak check + +# Profile-mode memory check of three heavy-image flows on web and Android. +# Called by: +# - image-sentry.yaml (on tag push, in parallel with the security scan) +# - memory-leak-check-label.yaml (any PR labelled `memory-check`) +# It also runs on PRs that touch the check itself, so the check is proven before merge. +on: + workflow_call: + workflow_dispatch: + pull_request: + types: [opened, synchronize, reopened] + paths: + - ".github/workflows/memory-leak-check*.yaml" + - "scripts/memory-leak-check.sh" + - "scripts/memory-leak-report.py" + - "scripts/memory_report/**" + - "scripts/test_memory_leak_report.py" + - "scripts/ci-memory-sidecar.js" + - "integration_test/memory/**" + - "integration_test/scenarios/memory/**" + - "integration_test/tests/memory/**" + +concurrency: + group: memory-leak-check-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +env: + FLUTTER_VERSION: 3.38.9 + +permissions: + contents: read + +jobs: + web: + name: Memory leak check (web) + runs-on: ubuntu-24.04 + timeout-minutes: 60 + steps: + - name: Checkout repository + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + + - name: Setup Flutter + uses: subosito/flutter-action@1a449444c387b1966244ae4d4f8c696479add0b2 # v2.23.0 + with: + flutter-version: ${{ env.FLUTTER_VERSION }} + channel: "stable" + cache: true + + - name: Run prebuild + run: ./scripts/prebuild.sh + + - name: Test the report script + run: python3 -I -m unittest discover -s scripts -p 'test_memory_leak_report.py' + + - name: Memory check + run: bash scripts/memory-leak-check.sh web + + - name: Compress heap snapshots + if: always() + run: gzip -f ci-logs/memory/*.heapsnapshot || true + + - name: Upload heap snapshots + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: memory-heapsnapshots-web + path: ci-logs/memory/*.heapsnapshot.gz + retention-days: 7 + if-no-files-found: ignore + + - name: Upload logs + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: memory-logs-web + path: | + ci-logs/ + !ci-logs/memory/*.heapsnapshot.gz + retention-days: 30 + if-no-files-found: ignore + + android: + name: Memory leak check (android) + runs-on: ubuntu-24.04 + timeout-minutes: 60 + steps: + - name: Checkout repository + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + + - name: Enable KVM + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + + - name: Setup Flutter + uses: subosito/flutter-action@1a449444c387b1966244ae4d4f8c696479add0b2 # v2.23.0 + with: + flutter-version: ${{ env.FLUTTER_VERSION }} + channel: "stable" + cache: true + + - name: Setup Java + uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4.9.1 + with: + java-version: 17 + distribution: temurin + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@d9c87d481d55275bb5441eef3fe0e46805f9ef70 # v3.5.0 + + - name: Run prebuild + run: ./scripts/prebuild.sh + + - name: Memory check + uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0 + with: + api-level: 34 + target: google_apis + arch: x86_64 + profile: pixel_6 + cores: 4 + force-avd-creation: false + emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -memory 4096 + disable-animations: true + emulator-boot-timeout: 1000 + script: bash scripts/memory-leak-check.sh android + + - name: Upload logs + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: memory-logs-android + path: ci-logs/ + retention-days: 30 + if-no-files-found: ignore diff --git a/android/app/build.gradle b/android/app/build.gradle index d9a918e3c..557aea6ef 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -88,12 +88,23 @@ android { } } } + profile { + // Release signing has no keystore in CI + signingConfig signingConfigs.debug + } } testOptions { execution "ANDROIDX_TEST_ORCHESTRATOR" } + testBuildType System.getenv('ANDROID_TEST_BUILD_TYPE') ?: 'debug' + + // KeyboardTestActivity only exists in the debug source set + if (System.getenv('ANDROID_TEST_BUILD_TYPE') == 'profile') { + sourceSets.androidTest.java.filter.exclude '**/WebViewKeyboardTest.java' + } + namespace "com.linagora.android.tmail" } diff --git a/docs/dev/memory-leak-check.md b/docs/dev/memory-leak-check.md new file mode 100644 index 000000000..69a8cd560 --- /dev/null +++ b/docs/dev/memory-leak-check.md @@ -0,0 +1,63 @@ +# Memory leak check + +Profile-mode Patrol tests that compare memory before and after a heavy flow. +Runs on web and Android. +Tag: `memory`. Excluded from the normal Patrol scripts (`--exclude-tags=memory`). + +## Flows + +- `open_heavy_inline_email`: open a mail with a 5 MB inline image, then leave it. +- `two_composers_heavy_send`: compose and send mails with a heavy inline image and attachment. +- `identity_heavy_inline`: create and delete an identity with a heavy signature image. + +Each flow runs twice. +The first run warms caches, the second is measured. +Memory is sampled before and after the second run. + +## Limits + +- The test cannot see inside the sandboxed mail viewer, so it does not check that the inline image finished loading. +- Composers run one after the other, concurrent composers are not covered. + +## Rules + +Limits live in `integration_test/memory/memory_rules.json`. + +| Rule | Checks | +| --- | --- | +| R1 | Classes listed in `mustReturnToBaseline` do not grow | +| R2 | Buffer growth stays under `maxBufferGrowthBytes` | +| R3 | Total heap growth stays under `maxTotalGrowthBytes` | +| R4 | No tmail class grows more than `maxTmailClassGrowthBytes` | +| R5 | Android RSS growth stays under `maxRssGrowthBytes` | + +Web totals exclude the CanvasKit WebAssembly heap. + +## Run locally + +- Needs Docker (local JMAP backend) and `bash scripts/prebuild.sh` done once. +- Web: `bash scripts/memory-leak-check.sh web` +- Android: boot an emulator (`emulator-5554`), then `bash scripts/memory-leak-check.sh android` +- Output goes to `$CI_LOGS` (default `./ci-logs`), report is printed at the end. + +Useful variables: + +- `CI_LOGS`: output folder. +- `MEMORY_REPORT_ONLY=1`: print the report, always exit 0 (calibration). +- `MEMORY_PATROL_ARGS`: extra Patrol args, for example `--show-flutter-logs --target integration_test/tests/memory/.dart`. + +Report unit tests: `python3 -m unittest discover -s scripts -p 'test_memory_leak_report.py'`. + +## CI + +- Workflow: `.github/workflows/memory-leak-check.yaml`. +- Runs on release tags (called by `image-sentry.yaml`), on PRs touching the check, and on PRs labelled `memory-check`. +- The report is always published, even when a rule fails. + +## Add a case + +- Add a scenario in `integration_test/scenarios/memory/` extending `MemoryLeakScenario`. +- Implement `caseId` and `runCycle` (and `prepare` if the flow starts on another screen). +- Add a test in `integration_test/tests/memory/` with tags `web`, `android`, `memory` and a timeout. +- Add the case to `memory_rules.json`, with `mustReturnToBaseline` for controllers that must be disposed. +- Run it with `MEMORY_REPORT_ONLY=1` several times, then set limits above the observed maximum. diff --git a/scripts/memory-leak-check.sh b/scripts/memory-leak-check.sh new file mode 100644 index 000000000..679e26302 --- /dev/null +++ b/scripts/memory-leak-check.sh @@ -0,0 +1,112 @@ +#!/bin/bash +# Usage: memory-leak-check.sh +# Runs the memory-tagged Patrol tests in profile mode against one local backend, +# collects samples through ci-memory-sidecar.js and applies the leak rules. +# Android expects a booted emulator (emulator-5554). +set -uo pipefail + +PLATFORM="${1:?usage: memory-leak-check.sh }" +ROOT_DIR="$PWD" +CI_LOGS="${CI_LOGS:-$ROOT_DIR/ci-logs}" +MEMORY_DIR="$CI_LOGS/memory" +mkdir -p "$MEMORY_DIR" +rm -f "$MEMORY_DIR"/* + +case "$PLATFORM" in + web) HOST=localhost ;; + android) HOST=10.0.2.2 ;; # the emulator's alias for the host + *) echo "unknown platform: $PLATFORM"; exit 2 ;; +esac +RESET_PORT=9999 +SIDECAR_PORT=9998 +WEBSOCKET_URL="ws://$HOST" +export BASIC_AUTH_URL="http://$HOST" +export MEMORY_OUT_DIR="$MEMORY_DIR" +export MEMORY_SIDECAR_PORT="$SIDECAR_PORT" + +RESET_SERVER_PID="" +SIDECAR_PID="" + +cleanup() { + echo "Cleaning up memory check..." + cd "$ROOT_DIR" || return 0 + [ -n "$SIDECAR_PID" ] && kill "$SIDECAR_PID" 2>/dev/null + [ -n "$RESET_SERVER_PID" ] && kill "$RESET_SERVER_PID" 2>/dev/null + wait "$SIDECAR_PID" "$RESET_SERVER_PID" 2>/dev/null + bash "$ROOT_DIR/scripts/ci-backend-teardown.sh" "$CI_LOGS/backend.log" + [ -f backend-docker/jmap.properties.bak ] && mv backend-docker/jmap.properties.bak backend-docker/jmap.properties + rm -f backend-docker/jwt_privatekey backend-docker/jwt_publickey + [ -f "$CI_LOGS/env.file.backup" ] && mv "$CI_LOGS/env.file.backup" env.file + return 0 +} +if [ -f backend-docker/jmap.properties.bak ] || [ -f "$CI_LOGS/env.file.backup" ]; then + echo "Leftovers of an interrupted run (backend-docker/jmap.properties.bak or env.file.backup): restore them first." + exit 2 +fi +[ -f env.file ] && cp env.file "$CI_LOGS/env.file.backup" +trap cleanup EXIT + +export PATH="$PATH:$HOME/.pub-cache/bin" +dart pub global activate patrol_cli 4.3.1 || exit 1 + +cd backend-docker || exit 1 +docker compose down || true +openssl genpkey -algorithm rsa -pkeyopt rsa_keygen_bits:4096 -out jwt_privatekey || exit 1 +openssl rsa -in jwt_privatekey -pubout -out jwt_publickey || exit 1 +# James reports this prefix in its JMAP session, and the app follows it. +sed -i.bak "s|url.prefix=.*|url.prefix=$BASIC_AUTH_URL|" jmap.properties +sed -i.bak2 "s|websocket.url.prefix=.*|websocket.url.prefix=$WEBSOCKET_URL|" jmap.properties +rm -f jmap.properties.bak2 +docker compose up -d || exit 1 +for attempt in $(seq 1 150); do + docker compose logs tmail-backend | grep -qi "JAMES server started" && break + if [ "$attempt" -eq 150 ]; then + echo "tmail-backend did not start in 5 minutes" + docker compose logs --tail 50 tmail-backend + exit 1 + fi + echo "Waiting for tmail-backend to start..." + sleep 2 +done +docker exec tmail-backend ./root/conf/integration_test/provisioning.sh > /dev/null 2>&1 || exit 1 +cd "$ROOT_DIR" || exit 1 + +RESET_PORT="$RESET_PORT" python3 scripts/backend-reset-server.py > "$CI_LOGS/backend-reset-server.log" 2>&1 & +RESET_SERVER_PID=$! +node --experimental-websocket scripts/ci-memory-sidecar.js > "$CI_LOGS/memory-sidecar.log" 2>&1 & +SIDECAR_PID=$! + +DART_DEFINES=( + --dart-define=USERNAME=bob + --dart-define=PASSWORD=bob + --dart-define=ADDITIONAL_MAIL_RECIPIENT=alice@example.com + --dart-define=BASIC_AUTH_EMAIL=bob@example.com + --dart-define=BASIC_AUTH_URL="$BASIC_AUTH_URL" + --dart-define=RESET_SERVER_URL="http://$HOST:$RESET_PORT" + --dart-define=MEMORY_SIDECAR_URL="http://$HOST:$SIDECAR_PORT" +) + +# Local debugging only, e.g. MEMORY_PATROL_ARGS="--show-flutter-logs --target integration_test/tests/memory/.dart" +read -r -a EXTRA_PATROL_ARGS <<< "${MEMORY_PATROL_ARGS:-}" + +if [ "$PLATFORM" = web ]; then + cp integration_test/integration_test_env.file env.file + # Fixed debug port: the sidecar finds the app tab over CDP. + WEB_BROWSER_ARGS='["--disable-web-security","--disable-dev-shm-usage","--disable-gpu","--lang=en-US","--accept-lang=en-US,en","--remote-debugging-port=9333"]' + # Timeouts nest: Dart test 12 min < Playwright 15 min. + patrol test -v --profile --tags=memory --device=chrome --web-port=3000 \ + --web-headless=true --web-server-timeout=300 --web-timeout=900000 \ + --web-global-timeout=3300000 --web-browser-args="$WEB_BROWSER_ARGS" \ + ${EXTRA_PATROL_ARGS[@]+"${EXTRA_PATROL_ARGS[@]}"} "${DART_DEFINES[@]}" +else + export ANDROID_TEST_BUILD_TYPE=profile + patrol test -v --profile --tags=memory --device=emulator-5554 \ + ${EXTRA_PATROL_ARGS[@]+"${EXTRA_PATROL_ARGS[@]}"} "${DART_DEFINES[@]}" +fi +PATROL_EXIT=$? + +python3 -I scripts/memory-leak-report.py "$MEMORY_DIR" integration_test/memory/memory_rules.json +REPORT_EXIT=$? + +[ "$PATROL_EXIT" -ne 0 ] && exit "$PATROL_EXIT" +exit "$REPORT_EXIT"