juju / charm-helpers

Apache License 2.0
18 stars 127 forks source link

`hookenv.config` never calls `_run_atexit()` so previous values are never saved #772

Closed mthaddon closed 1 year ago

mthaddon commented 1 year ago

I've been trying to debug a problem in a charm where the charm code does the following:

cfg = hookenv.config()
if cfg.previous("some-key"):
    do_something()

However, it looks like cfg.previous("some-key") is always None. From what I can tell this is because in the __init__ method for the Config class we have atexit(self._implicit_save). However, because we're not calling _run_atexit() all this does is append the function to a list that might be called, but never is.

mthaddon commented 1 year ago

I should mention that this is happening within an Operator Framework charm, and the code in question is within a function that's called as part of an action event handler. It's possible this isn't how this is intended to be used, would appreciate confirmation either way.

ajkavanagh commented 1 year ago

Hi @mthaddon

Yes, some parts of hookenv (the submodule) expects the using-code to be using the Hooks class which automatically does-the-right-thing by calling _run_atstart() and _run_atexit(), and Config is one of those that expect to work this way. Curiously, the Services framework also uses it, but then I know of only a couple of charms that still use that framework!

charms.reactive also automatically calls the hookenv atexit code handlers in the right places too. I'm guessing for an ops framework charm you'll need to do this manually somewhere in the framework.

I'm going to close this as I don't think it's a bug, but please do re-open it if you get further information. Thanks.