lf-lang / reactor-c

A reactor runtime written in C
Other
12 stars 24 forks source link

Stopping problems with decentralized coordination #473

Open edwardalee opened 3 months ago

edwardalee commented 3 months ago

Decentralized coordination is having difficulty coordinating the shutdown of a federation when a federate calls lf_request_stop(). The following program executes at tags (0,0) through (0,3) (four cycles), at which point the Gather reactor calls lf_request_stop() and schedules one more cycle to occur at (0,4). This will likely be the stop tag that the RTI selects, although in theory it is possible for the RTI to select a later stop tag. However, this fifth cycle is usually not completing correctly. The sockets between federates appear to get closed prematurely from the sending end.

target C {
  coordination: decentralized,
}
reactor Pi {
  input trigger: bool
  output out: int
  reaction(trigger) -> out {=
    tag_t now = lf_tag();
    lf_print("***** at tag " PRINTF_TAG, now.time - lf_time_start(), now.microstep);
    lf_set(out, 42);
  =} STP (30 s) {=
    tag_t now = lf_tag();
    lf_print_error_and_exit("STP violation at Pi at tag " PRINTF_TAG, now.time - lf_time_start(), now.microstep);
  =}
}
reactor Gather {
  input[4] in: int
  output next: bool
  logical action a
  state count: int = 0
  reaction(startup, a) -> next {=
    lf_set(next, true);
  =}
  reaction(in) -> a {=
    tag_t now = lf_tag();
    for (int i = 0; i < 4; i++) {
      if (!in[i]->is_present) {
        lf_print_error_and_exit("Missing input %d in Gather at tag " PRINTF_TAG,
            i, now.time - lf_time_start(), now.microstep);
      }
    }
    lf_print("%d: at tag " PRINTF_TAG, self->count, now.time - lf_time_start(), now.microstep);
    self->count++;
    if (self->count == 4) {
      lf_request_stop();
    }
    lf_schedule(a, 0);
  =} STP (30 s) {=
    tag_t now = lf_tag();
    lf_print_error_and_exit("STP violation at Gather at tag " PRINTF_TAG, now.time - lf_time_start(), now.microstep);
  =}
  reaction(shutdown) {=
    if (self->count < 5) {
      lf_print_error_and_exit("Gather received only %d inputs. Expected at least 5.", self->count);
    } 
  =}
}
federated reactor {
  pi = new[4] Pi()
  g = new Gather()
  pi.out -> g.in
  (g.next)+ -> pi.trigger
}