Skip to contents

rush is equipped with an advanced error-handling mechanism designed to manage and mitigate errors encountered during the execution of tasks. It adeptly handles a range of error scenarios, from standard R errors to more complex issues such as segmentation faults and network errors.t If all of this fails, the user can manually debug the worker loop.

Simple R Errors

To illustrate the error-handling mechanism in rush, we employ the random search example from the main vignette. This time we introduce a random error with a 50% probability. Within the worker loop, users are responsible for catching errors and marking the corresponding task as "failed" using the $push_failed() method.

library(rush)

branin = function(x1, x2) {
  (x2 - 5.1 / (4 * pi^2) * x1^2 + 5 / pi * x1 - 6)^2 + 10 * (1 - 1 / (8 * pi)) * cos(x1) + 10
}

wl_random_search = function(rush) {

  while(rush$n_finished_tasks < 100) {

    xs = list(x1 = runif(1, -5, 10), x2 = runif(1, 0, 15))
    key = rush$push_running_tasks(xss = list(xs))

    tryCatch({
      if (runif(1) < 0.5) stop("Random Error")
      ys = list(y = branin(xs$x1, xs$x2))
      rush$push_results(key, yss = list(ys))
    }, error = function(e) {
      condition = list(message = e$message)
      rush$push_failed(key, conditions = list(condition))
    })

    ys = list(y = branin(xs$x1, xs$x2))
    rush$push_results(key, yss = list(ys))
  }
}

We start the workers.

rush = rsh(
  network = "test-simply-error",
  config = redux::redis_config())

rush$start_local_workers(
  worker_loop = wl_random_search,
  n_workers = 4,
  globals = "branin")

When an error occurs, the task is marked as "failed", and the error message is stored in the "message" column. This approach ensures that errors do not interrupt the overall execution process. It allows for subsequent inspection of errors and the reevaluation of failed tasks as necessary.

rush$fetch_failed_tasks()
            x1         x2   pid     worker_id       message          keys
         <num>      <num> <int>        <char>        <char>        <char>
 1:  5.4758210  6.1312465  9947 thickset_p... Random Err... df538103-7...
 2:  8.1801648  3.0763633  9918    flamy_hawk Random Err... 2cf7ab60-7...
 3: -1.3035261 14.5096460  9918    flamy_hawk Random Err... e8566011-1...
 4:  3.1608951 10.7203509  9918    flamy_hawk Random Err... ce807ad0-a...
 5:  5.1675697  1.4647186  9918    flamy_hawk Random Err... d987b865-a...
 6:  8.0785098  7.0533492  9918    flamy_hawk Random Err... c44ec005-f...
 7:  0.5794969  6.0515197  9918    flamy_hawk Random Err... 72e69c8c-d...
 8:  3.9601362  2.8631579  9929 disqualifi... Random Err... c6d853c5-c...
 9:  1.9074286  3.6094046  9929 disqualifi... Random Err... 063a3946-4...
10: -2.7644742  0.8188894  9918    flamy_hawk Random Err... 98cbda72-6...
11:  1.9587818  9.0358721  9934 establishe... Random Err... 31eb7ae9-0...
12: -2.0443164  7.2135418  9929 disqualifi... Random Err... 05aa7b28-6...
13:  6.5172560  5.1668265  9934 establishe... Random Err... a9209144-3...
14:  9.5058586  2.0026094  9929 disqualifi... Random Err... 0ddc3c4a-6...
15:  7.7213604 13.4179402  9934 establishe... Random Err... 2fd95b1f-8...
16:  7.7628397 14.3170554  9918    flamy_hawk Random Err... 56be902a-6...
17:  5.5830607  3.6164194  9929 disqualifi... Random Err... 6fcc1c81-b...
18:  4.5936473  6.0226033  9934 establishe... Random Err... 07ce3d6d-5...
19: -1.8965850 13.0895400  9934 establishe... Random Err... 645b4426-4...
20:  0.5178674  1.5951151  9918    flamy_hawk Random Err... 6df94fbf-3...
21: -4.8397220  9.7600857  9929 disqualifi... Random Err... d208bc05-b...
22:  1.5303450  0.1202721  9934 establishe... Random Err... 673cd969-9...
23:  3.8242110  0.8025768  9918    flamy_hawk Random Err... 3c77beea-c...
24:  8.1618700  3.1589894  9918    flamy_hawk Random Err... 828b171e-7...
25:  8.8285438  3.9941390  9929 disqualifi... Random Err... 507c5f2a-8...
26:  5.9867483  6.8433706  9934 establishe... Random Err... b05bc1ff-7...
27: -4.2804643  8.0838336  9947 thickset_p... Random Err... d990e0b4-f...
28:  8.9502935  1.8684014  9929 disqualifi... Random Err... aa9f6b5f-7...
29:  1.7482980  7.2825245  9918    flamy_hawk Random Err... cfb80ec1-2...
30:  6.4714533  3.2544829  9918    flamy_hawk Random Err... 6739ea3c-5...
31:  7.1809456  1.8086234  9918    flamy_hawk Random Err... 9a7197d6-c...
32:  0.4681932 11.7588821  9947 thickset_p... Random Err... 0f9410ae-3...
33:  2.0873590  2.7434855  9934 establishe... Random Err... a68327c7-1...
            x1         x2   pid     worker_id       message          keys

Handling Failing Workers

The rush package provides mechanisms to address situations in which workers fail due to crashes or lost connections. Such failures may result in tasks remaining in the “running” state indefinitely. To illustrate this, we define a function that simulates a segmentation fault by terminating the worker process.

wl_failed_worker = function(rush) {
  xs = list(x1 = runif(1, -5, 10), x2 = runif(1, 0, 15))
  key = rush$push_running_tasks(xss = list(xs))

  tools::pskill(Sys.getpid(), tools::SIGKILL)
}

rush = rsh(network = "test-failed-workers")

worker_ids =  rush$start_local_workers(
  worker_loop = wl_failed_worker,
  n_workers = 2)

The package offers the $detect_lost_workers() method, which is designed to identify and manage these occurrences.

rush$detect_lost_workers()

This method works for workers started with $start_local_workers() and $start_remote_workers(). Workers started with $worker_script() must be started with a heartbeat mechanism (see vignette).

The $detect_lost_workers() method also supports automatic restarting of lost workers when the option restart_workers = TRUE is specified. Alternatively, lost workers may be restarted manually using the $restart_workers() method. Automatic restarting is only available for local workers. When a worker fails, the status of the task that caused the failure is set to "failed".

rush$fetch_failed_tasks()
          x1       x2   pid     worker_id       message          keys
       <num>    <num> <int>        <char>        <char>        <char>
1:  6.939323 7.259324 10067 untrigonom... Worker has... 9c1937f5-2...
2: -2.616989 2.563020 10069 undeliciou... Worker has... de931a42-3...

Debugging

When the worker loop fails unexpectedly due to an uncaught error, it is necessary to debug the worker loop. Consider the following example, in which the worker loop randomly generates an error.

wl_error = function(rush) {

  repeat {
    x1 = runif(1)
    x2 = runif(1)

    xss = list(list(x1 = x1, x2 = x2))

    key = rush$push_running_tasks(xss = xss)

    if (x1 > 0.90) {
      stop("Unexpected error")
    }

    rush$push_results(key, yss = list(list(y = x1 + x2)))
  }
}

To begin debugging, the worker loop is executed locally. This requires the initialization of a RushWorker instance. Although the rush worker is typically created during worker initialization, it can also be instantiated manually. The worker instance is then passed as an argument to the worker loop.

rush_worker = RushWorker$new("test", remote = FALSE)

wl_error(rush_worker)
Error in wl_error(rush_worker): Unexpected error

When an error is raised in the main process, the traceback() function can be invoked to examine the stack trace. Breakpoints may also be set within the worker loop to inspect the program state. This approach provides substantial control over the debugging process. Certain errors, such as missing packages or undefined global variables, may not be encountered when running locally. However, such issues can be readily identified using the $detect_lost_workers() method.

rush = rsh("test-error")

rush$start_local_workers(
  worker_loop = wl_error,
  n_workers = 1
)

The $detect_lost_workers() method can be used to identify lost workers.

rush$detect_lost_workers()

Output and message logs can be written to files by specifying the message_log and output_log arguments.

rush = rsh("test-error")

message_log = tempdir()
output_log = tempdir()

worker_ids = rush$start_local_workers(
  worker_loop = wl_error,
  n_workers = 1,
  message_log = message_log,
  output_log = output_log
)

Sys.sleep(5)

readLines(file.path(message_log, sprintf("message_%s.log", worker_ids[1])))
[1] "Debug message logging on worker individual_andeancat started"
[2] "Error in start_args$worker_loop(rush = rush) : Unexpected error"
[3] "Calls: <Anonymous> ... <Anonymous> -> eval.parent -> eval -> eval -> <Anonymous>"
[4] "Execution halted"                                                                
readLines(file.path(output_log, sprintf("output_%s.log", worker_ids[1])))
[1] "[1] \"Debug output logging on worker individual_andeancat started\""