Skip to content

Commit

Permalink
[wpilibj] Call abort() on Rio on caught Java exception (#6420)
Browse files Browse the repository at this point in the history
On Rio, we simply want to restart the robot program as quickly as possible,
and don't want to risk a hang somewhere that will keep that from happening.

The main downside of this is it won't wait for threads to finish (e.g. data logs won't get a final flush).
  • Loading branch information
PeterJohnson authored Mar 9, 2024
1 parent ccb4cbe commit 18e57f7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hal/src/main/java/edu/wpi/first/hal/HAL.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public final class HAL extends JNIWrapper {
*/
public static native void exitMain();

/** Terminates the executable (at the native level). Does nothing in simulation. */
public static native void terminate();

private static native void simPeriodicBeforeNative();

private static final List<Runnable> s_simPeriodicBefore = new ArrayList<>();
Expand Down
15 changes: 15 additions & 0 deletions hal/src/main/native/cpp/jni/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <jni.h>

#include <cassert>
#include <cstdlib>
#include <cstring>

#include <fmt/format.h>
Expand Down Expand Up @@ -82,6 +83,20 @@ Java_edu_wpi_first_hal_HAL_exitMain
HAL_ExitMain();
}

/*
* Class: edu_wpi_first_hal_HAL
* Method: terminate
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_HAL_terminate
(JNIEnv*, jclass)
{
#ifdef __FRC_ROBORIO__
std::abort();
#endif
}

/*
* Class: edu_wpi_first_hal_HAL
* Method: simPeriodicBeforeNative
Expand Down
5 changes: 5 additions & 0 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ public static <T extends RobotBase> void startRobot(Supplier<T> robotSupplier) {
runRobot(robotSupplier);
}

// On RIO, this will just terminate rather than shutting down cleanly (it's a no-op in sim).
// It's not worth the risk of hanging on shutdown when we want the code to restart as quickly
// as possible.
HAL.terminate();

HAL.shutdown();

System.exit(0);
Expand Down

0 comments on commit 18e57f7

Please sign in to comment.