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

event context is unset when it probably should be #1378

Open
PietroPasotti opened this issue Sep 17, 2024 · 1 comment
Open

event context is unset when it probably should be #1378

PietroPasotti opened this issue Sep 17, 2024 · 1 comment
Labels
25.04 bug Something isn't working small item

Comments

@PietroPasotti
Copy link
Contributor

PietroPasotti commented Sep 17, 2024

the Framework._event_context var is used by the framework to determine whether it should be validating relation data access and for example raise if a nonleader unit is trying to read data only a leader can access.
_event_context is only set during _reemit, that is, when an event is actually being processed by the charm.
This causes the following confusing situation during testing:

(supposing we don't have leadership)

class MyCharm:
    def __init__():
        relation.data[self.app]  # no problem: false positive
        ...
    def _on_foo_event(self, _):
        relation.data[self.app]  # problem

Note that this issue also (very probably) causes inconsistent tracebacks in production.

class MyCharm:
    def __init__():
        relation.data[self.app]  # ModelError
        ...
    def _on_foo_event(self, _):
        relation.data[self.app]  # RelationDataAccessError

A possible solution could be to enter the event context before the charm is initialized if we are testing (and set it to some "not-a-real-event" string as, at the point in time when we initialize the charm, we don't yet necessarily know what event we'll be emitting on it)

@benhoyt
Copy link
Contributor

benhoyt commented Sep 18, 2024

Yes, seems best to have RelationDataAccessError in both cases (init and otherwise). We'll look at this at some point.

@benhoyt benhoyt added bug Something isn't working 25.04 labels Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
25.04 bug Something isn't working small item
Projects
None yet
Development

No branches or pull requests

2 participants