ftctechnh / ftc_app

FTC Android Studio project to create FTC Robot Controller app.
761 stars 3.16k forks source link

Threads that throw unhandled exceptions that are not the main opmode thread irrecoverably crash the RC app #680

Open guineawheek opened 5 years ago

guineawheek commented 5 years ago

This has been a fairly longstanding issue and it can be triggered from both internal SDK code (under rare circumstances) and team code. Generally, if a thread that is not the main OpMode or LinearOpMode thread throws an exception, the entire app will crash to the homescreen and the team will be unable to recover for the rest of the match. This thread can either be started by code written by the team, or an internally such as used in Vuforia or TFLite processing through bugs in the SDK.

One can reproduce this issue with a simple OpMode that looks something like this:

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;

@TeleOp(name="crash", group="crash")
public class KillApp extends OpMode {
    @Override
    public void init() {}

    @Override
    public void start() {
        new Thread() {
            public void run() {
                throw new RuntimeException("Robot controller crash!");
            }
        }.start();
    }

    @Override
    public void loop() {}
}

One solution to this issue is simply setting a default exception handler for all threads. [This gist will restart the robot controller on a crash] (https://gist.github.com/guineawheek/ea7390854c3f9ecaca56d6ad1ad630fe), although for different threads different responses may be more appropriate to reduce downtime.