The Initializer Is Killed for Running Out of Memory (Pro)

On Docker Compose, the init container stops part way through the migrations, and Docker reports:

$ docker inspect --format '{{.State.Status}} ExitCode={{.State.ExitCode}} OOMKilled={{.State.OOMKilled}}' init
exited ExitCode=137 OOMKilled=true

On Kubernetes, the initializer Job’s pod restarts and shows:

$ kubectl get pods -n <namespace> -l app.kubernetes.io/component=initializer
NAME                       READY   STATUS      RESTARTS      AGE
<release>-initializer-...  0/1     OOMKilled   3 (39s ago)   51s

and kubectl describe pod shows the container terminated with:

    Last State:     Terminated
      Reason:       OOMKilled
      Exit Code:    137

Once its retries are used up, the Job ends Failed with the event BackoffLimitExceeded.

Cause

The initializer applies the database migrations in one process, and its memory use grows with each migration it applies. When it passes the memory it is allowed, the kernel kills it with SIGKILL, which is exit code 137.

A killed process cannot print anything on its way out, so the log stops in the middle of the migrations. The migration summary that the initializer prints on any other failure (see Reading the initializer’s result) is missing. That missing summary, with exit code 137, is how you tell an out-of-memory kill from a failed migration.

Its peak is about 1.4 GB upgrading from 3.4.x to 3.5.0, and about 1.2 GB for a fresh install that applies every migration.

  • Kubernetes: the Helm chart limits the initializer to dojo.initializer.resources.limits.memory. Charts before 3.5.100 set it to 1Gi, below those peaks. From 3.5.100 the default is 2Gi. A values file that still sets 1Gi keeps the old limit on a newer chart.
  • Docker Compose: the init container has no memory limit of its own, so it is killed only when the host runs out of memory. The kernel log (sudo journalctl -k or sudo dmesg) records the out-of-memory kill.

Fix

Raise the memory the initializer may use, or free it, and run the initializer again. Each migration that finished before the kill is recorded, so the next run starts from the first one that did not. Restore your backup only if the rerun reports a migration that cannot complete, as Reading the initializer’s result describes. On an upgrade to 3.5.0, read Retries fail after a kill during the upgrade to 3.5.0 first.

Kubernetes: set a higher limit in your values:

dojo:
  initializer:
    resources:
      requests:
        memory: 1Gi
      limits:
        memory: 2Gi

The initializer Job’s pod template cannot change while the Job exists, so delete the failed Job before you apply the new values, then run your helm upgrade again:

kubectl delete job -n <namespace> -l app.kubernetes.io/component=initializer

Docker Compose: free memory on the host, or add memory, then stop the application and start it again. Starting it runs the initializer again:

sudo -E dojo-compose-cli app stop
sudo -E dojo-compose-cli app start

See Hardware Sizing for host sizes.

After the rerun, check the initializer’s exit code as Reading the initializer’s result shows: 0 means the upgrade is complete.

Retries fail after a kill during the upgrade to 3.5.0

After an out-of-memory kill during the upgrade to 3.5.0, every retry fails with:

relation "dojo_findvulnref_archive_batch" already exists

Migration dojo.0305_archive_batch_indexes in 3.5.0 builds its indexes with CREATE INDEX CONCURRENTLY, outside a transaction. A kill leaves some of those indexes behind while the migration stays unapplied, and the next run stops on the first one. From 3.5.100 the migration can run again after an interruption, so the simplest fix is to upgrade to 3.5.100 or later instead.

To finish the upgrade to 3.5.0 itself:

  1. Raise the memory limit or free memory, as above.

  2. Confirm that the migration is still unapplied: python3 manage.py showmigrations dojo lists it as [ ] 0305_archive_batch_indexes. Do not go on if it shows [X].

  3. Connect to the database as the DefectDojo user and drop the indexes the interrupted run left. Run each statement on its own, because DROP INDEX CONCURRENTLY cannot run inside a transaction. IF EXISTS skips the ones the run never reached, and the retry builds them all again.

    DROP INDEX CONCURRENTLY IF EXISTS dojo_findvulnref_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_endpstatus_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_engagement_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_finding_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_jiraissue_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_locfindref_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_product_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_prod_type_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_test_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_tifa_archive_batch;
    DROP INDEX CONCURRENTLY IF EXISTS dojo_vulnid_archive_batch;
  4. Run the initializer again, as above.