Skip to content

ENT-14194 2. Reset classes and variables between "then" bundle runs in cf-reactor - #6375

Merged
larsewi merged 2 commits into
cfengine:masterfrom
victormlg:cf-reactor.ResetVarClasses
Oct 9, 2026
Merged

larsewi merged 2 commits into
cfengine:masterfrom
victormlg:cf-reactor.ResetVarClasses

Conversation

@victormlg

@victormlg victormlg commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Classes that are defined inside a then bundle can stay defined between runs, even though the condition is not true anymore:

Exemple:

body when file_deleted(filename)
{
  file_deleted => "$(filename)";
}

bundle agent check_marker(file)
{
  classes:
    "marker_exists" expression => fileexists("/tmp/marker"), scope => "namespace";

  files:
    "$(file)" create => "true";

  reports:
    marker_exists::
      "marker exists";
    !marker_exists::
      "marker does not exist";
}

bundle reactor event_handler
{
  vars:
    "Z" string => "/tmp/a";

  events:
    "Watch /tmp/a"
      when => file_deleted("$(Z)"),
      then => check_marker("$(Z)");
}
  • 1st event: /tmp/marker exists, marker_exists is defined
  • delete /tmp/marker
  • 2nd event: /tmp/marker doesn't exists, marker_exists is still defined

Persistent classes should not be deleted

Comment thread cf-agent/agent_operations.c Fixed
Comment thread cf-reactor/reactor_transform.c Fixed
@victormlg
victormlg marked this pull request as draft October 5, 2026 12:07
@victormlg
victormlg force-pushed the cf-reactor.ResetVarClasses branch from 67bd500 to 323b383 Compare October 7, 2026 14:37
@victormlg
victormlg marked this pull request as ready for review October 7, 2026 14:44

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GJ 🚀 ClassTableCopy, VariableTableCopy could be good candidates for unit tests. E.g. define a class and a variable, restore, assert both are gone, but a persistent class is kept.

Comment thread libpromises/eval_context.c Outdated
The issue is that classes and variables declared once will stay declared until the next policy
reload. So a class can stay defined between runs, when the state it checks changed.
Instead, the state of all vars and classes is saved in a snapshot before every event, and restored
once the event is handled. Persistent classes are left out and loaded from the state database at
every event, so they stay defined across runs until they expire, as in cf-agent.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Adds tests for ClassTableCopy(), VariableTableCopy() and snapshot take/restore,
and corrects a comment that wrongly said bundle variables aren't in the global variable table.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
@victormlg
victormlg force-pushed the cf-reactor.ResetVarClasses branch from 323b383 to a715a16 Compare October 8, 2026 12:02
@victormlg

Copy link
Copy Markdown
Contributor Author

@cf-bottom jenkins, please :)

@cf-bottom

Copy link
Copy Markdown

@larsewi
larsewi merged commit 27ad73d into cfengine:master Oct 9, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants