Skip to content

Commit

Permalink
[EXECUTOR] Fix executor not printing import error message to net hand…
Browse files Browse the repository at this point in the history
…ler (#217)

* [EXECUTOR] Set game state to inactive on idle

* [EXECUTOR] Swapped api and student code import for executor_init

Former-commit-id: 850cf6f
  • Loading branch information
AVSurfer123 authored Apr 24, 2021
1 parent 26174c4 commit 62f5064
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions executor/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "../runtime_util/runtime_util.h" //for runtime constants (TODO: consider removing relative pathname in include)
#include "../shm_wrapper/shm_wrapper.h" // Shared memory wrapper to get/send device data

#define DEBUG_SIGNAL 15 // Signal to log as DEBUG instead of ERROR

// Global variables to all functions and threads
const char* api_module = "studentapi";
Expand Down Expand Up @@ -101,6 +100,14 @@ static void executor_init(char* student_code) {
// Need this so that the Python interpreter sees the Python files in this directory
PyRun_SimpleString("import sys;sys.path.insert(0, '.')");

//imports the Cython student API
pAPI = PyImport_ImportModule(api_module);
if (pAPI == NULL) {
PyErr_Print();
log_printf(ERROR, "Could not import API module");
exit(1);
}

// imports the student code
module_name = student_code;
pModule = PyImport_ImportModule(module_name);
Expand All @@ -115,14 +122,6 @@ static void executor_init(char* student_code) {
return;
}

//imports the Cython student API
pAPI = PyImport_ImportModule(api_module);
if (pAPI == NULL) {
PyErr_Print();
log_printf(ERROR, "Could not import API module");
exit(1);
}

//checks to make sure there is a Robot class, then instantiates it
PyObject* robot_class = PyObject_GetAttrString(pAPI, "Robot");
if (robot_class == NULL) {
Expand Down Expand Up @@ -449,8 +448,8 @@ static void run_challenges() {
* Handler for killing the child mode subprocess
*/
static void python_exit_handler(int signum) {
// Cancel the Python thread by sending a TimeoutError
exit(0);
// Cancel the Python thread by sending a TimeoutError
// log_printf(DEBUG, "cancelling Python function");
// PyGILState_STATE gstate = PyGILState_Ensure();
// if (mode != CHALLENGE) {
Expand Down Expand Up @@ -485,6 +484,12 @@ static void kill_subprocess() {
if (waitpid(pid, &status, 0) == -1) {
log_printf(ERROR, "Wait failed for pid %d: %s", pid, strerror(errno));
}
if (!WIFEXITED(status)) {
log_printf(ERROR, "Error when shutting down execution of mode %d", mode);
}
if (WIFSIGNALED(status)) {
log_printf(ERROR, "killed by signal %d\n", WTERMSIG(status));
}
// Reset parameters if robot was in AUTO or TELEOP. Needed for safety
if (mode != CHALLENGE) {
reset_params();
Expand Down Expand Up @@ -513,7 +518,7 @@ static pid_t start_mode_subprocess(char* student_code, char* challenge_code) {
robot_desc_write(RUN_MODE, IDLE); // Will tell parent to call kill_subprocess
} else {
executor_init(student_code);
signal(SIGTERM, python_exit_handler); // kill subprocess regardless
signal(SIGTERM, python_exit_handler); // Set handler for killing subprocess
run_mode(mode);
}
exit(0);
Expand Down

0 comments on commit 62f5064

Please sign in to comment.