Skip to content

[BUG] Emulator repeatedly crashes on restart due to stale AVD .lock files (Bugfix is Included) #34

Closed

Description

@sayhere556

After an unclean shutdown / container stop, the Android emulator can enter a restart/crash loop on the next boot. The AVD folder keeps leftover *.lock files (ex: /data/android.avd/*.lock). When these stale lock files are present, the emulator often fails to start properly and keeps crashing until the lock files are manually removed.

Steps to reproduce

  1. Start the container/emulator normally.
  2. Stop it in a way that isn’t clean (ex: kill/restart container/host, force stop, etc.).
  3. Start the container/emulator again.
  4. Emulator crashes or fails to boot repeatedly.

Expected behavior

Emulator should start normally after restart, even if the previous stop was unclean.

Actual behavior

Emulator fails to start and/or crashes repeatedly until *.lock files are deleted from the AVD directory.

Diagnosis

Stale *.lock files are left behind in /data/android.avd after an unclean shutdown. On next start, the emulator treats the AVD as “in use” / locked and can crash or refuse to boot.

Proposed fix

Delete stale lock files before launching the emulator in start-emulator.sh.

Patch

Add these lines after killing any running emulator processes:

# Removes .lock files before emulator starts to prevent crashes
rm -rf /data/android.avd/*.lock

Notes

  • -f ensures startup doesn’t fail when no lock files exist.
  • -r is included for robustness (in case any lock artifacts are directories).
  • This only targets *.lock entries in the AVD directory and doesn’t touch other AVD data.

Activity

  1. sayhere556 commented on Dec 16, 2025

    @sayhere556
    ContributorAuthor

    Here is the updated start-emulator.sh that will fix this:

    #!/bin/bash
    
    # Kill any running emulator instances before starting a new one
    pkill -f "/opt/android-sdk/emulator/emulator"
    
    # Removes .lock files before emulator starts to prevent crashes
    rm -rf /data/android.avd/*.lock
    
    # Use custom ramdisk if present
    if [ -f /data/android.avd/ramdisk.img ]; then
      RAMDISK="-ramdisk /data/android.avd/ramdisk.img"
    fi
    
    # Path to the AVD config
    CONFIG_FILE="/data/android.avd/config.ini"
    
    update_config() {
      local key="$1"
      local value="$2"
      if grep -q "^$key=" "$CONFIG_FILE"; then
        sed -i "s/^$key=.*/$key=$value/" "$CONFIG_FILE"
      else
        echo "$key=$value" >> "$CONFIG_FILE"
      fi
    }
    
    # Configure optional screen resolution and density directly via config.ini
    if [ -f "$CONFIG_FILE" ]; then
      if [ -n "$SCREEN_RESOLUTION" ]; then
        WIDTH=${SCREEN_RESOLUTION%x*}
        HEIGHT=${SCREEN_RESOLUTION#*x}
        update_config "hw.lcd.width" "$WIDTH"
        update_config "hw.lcd.height" "$HEIGHT"
      fi
      if [ -n "$SCREEN_DENSITY" ]; then
        update_config "hw.lcd.density" "$SCREEN_DENSITY"
      fi
    fi
    
    # Start the emulator with the appropriate ramdisk.img
    /opt/android-sdk/emulator/emulator -avd android -nojni -netfast -writable-system -no-window -no-audio -no-boot-anim -skip-adb-auth -gpu swiftshader_indirect -no-snapshot -no-metrics $RAMDISK -qemu -m ${RAM_SIZE:-4096}
  2. Shmayro commented on Dec 16, 2025

    @Shmayro
    Owner

    Yeah, I usually get this. True, I used to do the cleanup manually.
    Good work!
    You can open a pull request so I can merge it.

  3. sayhere556 commented on Dec 17, 2025

    @sayhere556
    ContributorAuthor

    Done.

  4. sayhere556 commented on Dec 17, 2025

    @sayhere556
    ContributorAuthor

    Please reply when you pull the code so I know when to update my scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions