Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious cyclic dependency detection for federate in TypeScript #1175

Open
CloverCho opened this issue May 17, 2022 · 8 comments
Open

Spurious cyclic dependency detection for federate in TypeScript #1175

CloverCho opened this issue May 17, 2022 · 8 comments
Assignees
Labels
bug Something isn't working federated typescript Related to TypeScript target

Comments

@CloverCho
Copy link
Collaborator

CloverCho commented May 17, 2022

For the following LF code, the LF language server finds cyclic dependency, so it is not compiled.
However, there is no causality loop in the code actually.

Here are some explanations for this error.

  1. This detection is removed when we changed "federated reactor" to "main reactor",
    so the logic for detecting cycle among federates would have problem.
  2. This detection is also removed when we remove a connection between "Broker" federate and "Print" federate.
    (It can be done by removing "b.out -> p.inp" at the end of the federated reactor)
    Considering the fact that there is one-way connection between these two federates, it is certainly wrong.
/**
* Test of replacment for error of federate instance
*/
target TypeScript {
    timeout: 10 sec
};

reactor Count(error_time:number(-1)) {
    input inp:number;
    output out:number;
    state count:number(1);
    logical action a;
    logical action infLoop;
    reaction(inp) -> a {=
        if (inp) {
            count = inp;
            actions.a.schedule(0, null);
        }
    =}
    reaction(a) -> out, a, infLoop {=
        out = count++;
        if (count < error_time){
            actions.a.schedule(TimeValue.sec(1), null);
        } else {
            actions.infLoop.schedule(0, null);
        }
    =}
    reaction(infLoop) -> infLoop {=
        actions.infLoop.schedule(TimeValue.sec(1), null);
    =}
}

reactor Print {
    input inp:number;
    reaction(inp) {=
        if (inp){
            console.log(`Received: ${inp as number}`);
        }
    =}
}

reactor Start {
    output out:number;
    reaction(startup) -> out {=
        out = 1;
    =}
}

reactor Broker(timeout: time(1500 msec)) {
    input inp1:number;
    input inp2:number;
    output out:number;
    state time1:time;
    state time2:time;
    state alive1:boolean(true);
    state alive2:boolean(true);
    logical action a1;
    logical action a2;
    reaction(inp1, inp2) -> out, a1, a2 {=
        console.log(`Alive 1: ${alive1}, Alive 2: ${alive2}`);
        if (alive1) {
            if (inp1){
                out = inp1;
                console.log(`Input 1 Received: ${inp1}`);
                time1 = util.getCurrentPhysicalTime();
                actions.a1.schedule(timeout, null);
            }
        } else if (alive2) {
            if (inp2){
                out = inp2;
                console.log(`Input 2 Received: ${inp2}`);
                time2 = util.getCurrentPhysicalTime();
                actions.a2.schedule(timeout, null);
            }
        }
    =}
    reaction(a1) {=
        if (time1) {
            let elapsedPhysical = util.getCurrentPhysicalTime().subtract(time1);
            if (timeout.isEarlierThan(elapsedPhysical)){
                alive1 = false;
            }
        }
    =}
    reaction(a2) {=
        if (time2) {
            let elapsedPhysical = util.getCurrentPhysicalTime().subtract(time2);
            if (timeout.isEarlierThan(elapsedPhysical)){
                alive2 = false;
            }
        }
    =}
}

// Error: Spurious cyclic dependency detected
// If we changed federated to main, the dection is removed
federated reactor {
    b = new Broker();
    s = new Start();
    p = new Print();
    c1 = new Count(error_time = 5);
    c2 = new Count();
    
    s.out -> c1.inp;
    s.out -> c2.inp;
    c1.out -> b.inp1;
    c2.out -> b.inp2;

    // if comment out this code, the detection is removed
    b.out -> p.inp;    
}


@CloverCho CloverCho added bug Something isn't working typescript Related to TypeScript target federated labels May 17, 2022
@lhstrh
Copy link
Member

lhstrh commented May 17, 2022

Also note that the diagram doesn't report a cycle, but the validator does.

@Soroosh129
Copy link
Contributor

Could you please try and see if #1085 fixes this issue for you?

@lhstrh
Copy link
Member

lhstrh commented May 18, 2022

That PR seems stalled and is not passing CI...so that's not really a fix...

@CloverCho
Copy link
Collaborator Author

@Soroosh129
I tried on the PR again but it is not fixed

@Soroosh129
Copy link
Contributor

This looks like a TypeScript-specific problem.
When I switch the target to C, there is no cycle error in the federated-delay-fix branch.

@lhstrh
Copy link
Member

lhstrh commented May 19, 2022

There are peculiarities in our code base that lead to cycle detection happening multiple times. At least once in the validator, once in the diagram synthesis, and then once more in the code generator. The message @CloverCho describes must come from the code generator...

@lhstrh
Copy link
Member

lhstrh commented May 19, 2022

@CloverCho: what is the literal textual content of the error message you're seeing?

@CloverCho
Copy link
Collaborator Author

@lhstrh This is the error I got when I execute runLfc

lfc: error: Cyclic dependency due to succeeding reaction. Consider reordering reactions within reactor Count to avoid causality loop.

and

lfc: error: Reaction triggers involved in cyclic dependency in reactor Count: inp.

These errors occurred many times.

for about diagram synthesis, I found the following error message in the vscode.

Cyclic dependency due to preceding reaction. Consider reordering reactions within reactor Count to avoid causality loop

I'm attaching pictures of these errors.

  1. command line error

cmd error

  1. vscode error

vscode error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working federated typescript Related to TypeScript target
Projects
None yet
Development

No branches or pull requests

3 participants