# Buggy code
def start_avd_from_snapshot():
    """Start AVD"""
    logger.info("Starting MobSF Emulator")
    try:
        if platform.system() == "Darwin":
            # There is a strage error in mac with the dyld one in a while..
            # this should fix it..
            if "DYLD_FALLBACK_LIBRARY_PATH" in list(os.environ.keys()):
                del os.environ["DYLD_FALLBACK_LIBRARY_PATH"]

        args = [
            settings.AVD_EMULATOR,
            "-avd",
            settings.AVD_NAME,
            "-writable-system",
            "-snapshot",
            settings.AVD_SNAPSHOT,
            "-netspeed",
            "full",
            "-netdelay",
            "none",
            "-port",
            str(settings.AVD_ADB_PORT),
        ]
        subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        # Give a few seconds and check if the snapshot load succeed
        time.sleep(5)
        result = adb_command(["getprop", "init.svc.bootanim"], True)

        if result:
            if result.strip() == b"stopped":
                return True

        # Snapshot failed, stop the avd and return an error
        adb_command(["emu", "kill"])
        return False
    except:
        PrintException("[ERROR] Starting MobSF Emulator")
        return False


# Traceback (KeyError)
[INFO] 08/Feb/2019 16:17:54 - VM Starting
[INFO] 08/Feb/2019 16:17:55 - "POST /DynamicAnalyzer/ HTTP/1.1" 200 32400
[INFO] 08/Feb/2019 16:17:55 - "GET /static/css/devices.min.css HTTP/1.1" 200 44241
[INFO] 08/Feb/2019 16:17:55 - "GET /download/screen/screen.png HTTP/1.1" 302 0
[DEBUG] 08/Feb/2019 16:17:55 - Exception while resolving variable 'exp' in template 'general/error.html'.
Traceback (most recent call last):
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/base.py", line 829, in _resolve_lookup
current = current[bit]
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/context.py", line 83, in __getitem__
raise KeyError(key)
KeyError: 'exp'

During handling of the above exception, another exception occurred:

# Fixed code
def start_avd_from_snapshot():
    """Start AVD"""
    logger.info("Starting MobSF Emulator")
    try:
        if platform.system() == "Darwin":
            # There is a strage error in mac with the dyld one in a while..
            # this should fix it..
            if "DYLD_FALLBACK_LIBRARY_PATH" in list(os.environ.keys()):
                del os.environ["DYLD_FALLBACK_LIBRARY_PATH"]

        args = [
            settings.AVD_EMULATOR,
            "-avd",
            settings.AVD_NAME,
            "-writable-system",
            "-snapshot",
            settings.AVD_SNAPSHOT,
            "-netspeed",
            "full",
            "-netdelay",
            "none",
            "-port",
            str(settings.AVD_ADB_PORT),
        ]
        subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        # Give a few seconds and check if the snapshot load succeed
        time.sleep(5)
        result = adb_command(["getprop", "init.svc.bootanim"], True)

        if result:
            if result.strip() == b"stopped":
                return True

        # Snapshot failed, stop the avd and return an error
        adb_command(["emu", "kill"])
        return False
    except:
        PrintException("Starting MobSF Emulator")
        return False