# Buggy code
def connect():
    """Connect to VM/Device"""
    logger.info("Connecting to VM/Device")
    adb = getADB()
    subprocess.call([adb, "kill-server"])
    subprocess.call([adb, "start-server"])
    logger.info("ADB Started")
    wait(5)
    logger.info("Connecting to VM/Device")
    out = subprocess.check_output([adb, "connect", get_identifier()])
    if b"unable to connect" in out:
        raise ValueError(
            "ERROR Connecting to VM/Device. ", out.decode("utf-8").replace("\n", "")
        )
    try:
        subprocess.call([adb, "-s", get_identifier(), "wait-for-device"])
        logger.info("Mounting")
        if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_REAL_DEVICE":
            adb_command(["su", "-c", "mount", "-o", "rw,remount,rw", "/system"], True)
        else:
            adb_command(["su", "-c", "mount", "-o", "rw,remount,rw", "/system"], True)
            # This may not work for VMs other than the default MobSF VM
            adb_command(
                [
                    "mount",
                    "-o",
                    "rw,remount",
                    "-t",
                    "rfs",
                    "/dev/block/sda6",
                    "/system",
                ],
                True,
            )
    except:
        PrintException("[ERROR]  Connecting to VM/Device")


# 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 connect():
    """Connect to VM/Device"""
    logger.info("Connecting to VM/Device")
    adb = getADB()
    subprocess.call([adb, "kill-server"])
    subprocess.call([adb, "start-server"])
    logger.info("ADB Started")
    wait(5)
    logger.info("Connecting to VM/Device")
    out = subprocess.check_output([adb, "connect", get_identifier()])
    if b"unable to connect" in out:
        raise ValueError(
            "ERROR Connecting to VM/Device. ", out.decode("utf-8").replace("\n", "")
        )
    try:
        subprocess.call([adb, "-s", get_identifier(), "wait-for-device"])
        logger.info("Mounting")
        if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_REAL_DEVICE":
            adb_command(["su", "-c", "mount", "-o", "rw,remount,rw", "/system"], True)
        else:
            adb_command(["su", "-c", "mount", "-o", "rw,remount,rw", "/system"], True)
            # This may not work for VMs other than the default MobSF VM
            adb_command(
                [
                    "mount",
                    "-o",
                    "rw,remount",
                    "-t",
                    "rfs",
                    "/dev/block/sda6",
                    "/system",
                ],
                True,
            )
    except:
        PrintException("Connecting to VM/Device")