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

bugfix: fix initialization order bugs #550

Merged
merged 4 commits into from
Apr 11, 2023
Merged

bugfix: fix initialization order bugs #550

merged 4 commits into from
Apr 11, 2023

Conversation

glevco
Copy link
Contributor

@glevco glevco commented Apr 3, 2023

Acceptance Criteria:

  • Fix bug that was preventing node initialization due to incorrect import order
    • The Event Simulator indirectly imported code that used get_settings before it was called by run_node with the correct configuration, therefore generating a loading config twice with a different file error.
  • Fix bug that was preventing node initialization with the events feature due to incorrect start order
    • The HathorManager was sending events relevant to the EventManager before starting it.

@glevco glevco requested a review from jansegre as a code owner April 3, 2023 16:48
@glevco glevco self-assigned this Apr 3, 2023
@glevco glevco requested a review from msbrogli as a code owner April 3, 2023 16:48
@codecov
Copy link

codecov bot commented Apr 3, 2023

Codecov Report

Merging #550 (551182f) into dev (551182f) will not change coverage.
The diff coverage is n/a.

❗ Current head 551182f differs from pull request most recent head 406eb93. Consider uploading reports for the commit 406eb93 to get more accurate results

@@           Coverage Diff           @@
##              dev     #550   +/-   ##
=======================================
  Coverage   83.19%   83.19%           
=======================================
  Files         211      211           
  Lines       18665    18665           
  Branches     2593     2593           
=======================================
  Hits        15528    15528           
  Misses       2565     2565           
  Partials      572      572           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@glevco glevco force-pushed the bugfix/initialization branch from ccdefd5 to 405bb58 Compare April 3, 2023 17:45
Copy link
Member

@jansegre jansegre left a comment

Choose a reason for hiding this comment

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

The cli import fix is good as is, about the other one I wonder if we should make a test for it.

@glevco
Copy link
Contributor Author

glevco commented Apr 4, 2023

The cli import fix is good as is, about the other one I wonder if we should make a test for it.

@jansegre I was investigating this and I found out that existing tests in test_event_manager.py were supposed to have caught this. The code in PubSub for publishing events is:

    def publish(self, key: HathorEvents, **kwargs: Any) -> None:
        reactor_thread = ReactorThread.get_current_thread(self.reactor)

        args = EventArguments(**kwargs)
        for fn in self._subscribers[key]:
            if reactor_thread == ReactorThread.NOT_RUNNING:
                fn(key, args)
            else:
                is_empty = bool(not self.queue)
                self.queue.append((fn, key, args))
                if is_empty:
                    self._schedule_call_next()

The reason the tests passed is because tests use the Clock class instead of the reactor, so incidentally things are called in the correct order: the else branch is called and the event handler function call is delayed to after the EventManager is started. In the real case, when running the full node, the if reactor_thread == ReactorThread.NOT_RUNNING: branch is called, and therefore the event handler function is called before the EventManager is started.

Also, ReactorThread is where the MAIN_THREAD value is returned for Clock (for testing):

    def get_current_thread(cls, reactor: Reactor) -> 'ReactorThread':
        """ Returns if the code is being run on the reactor thread, if it's running already.
        """
        running = getattr(reactor, 'running', None)
        if running is not None:
            if running:
                return cls.MAIN_THREAD if isInIOThread() else cls.NOT_MAIN_THREAD
            else:
                # if reactor is not running yet, there's no threading
                return cls.NOT_RUNNING
        else:
            # on tests, we use Clock instead of a real Reactor, so there's
            # no threading. We consider that the reactor is running
            return cls.MAIN_THREAD

I feel that we should somehow either change ReactorThread or PubSub themselves, to prevent this from happening again in the future. Code logic is getting executed differently in production and in tests for this reason.

For example, changing the above last line from return cls.MAIN_THREAD to return cls.NOT_RUNNING would fix the problem, but I'm not sure of other unintended consequences. Edit: I have tested this, and it breaks a lot of other tests. Which may be a bit weird because maybe it indicates that there's code that relies on the specific implementation of PubSub and the order it is going to call message handlers (either at the time of a publish, or the postponed call)...

What do you think?

@jansegre
Copy link
Member

jansegre commented Apr 4, 2023

What do you think?

I think we should open an issue and investigate this separately and independently from this fix. I'd say this PR can me merged as is.

@glevco
Copy link
Contributor Author

glevco commented Apr 4, 2023

I think we should open an issue and investigate this separately and independently from this fix

I have created an issue: #552

@glevco glevco force-pushed the bugfix/initialization branch from 405bb58 to aa37ec3 Compare April 9, 2023 19:38
hathor/event/event_manager.py Show resolved Hide resolved
hathor/event/event_manager.py Show resolved Hide resolved
@glevco glevco force-pushed the bugfix/initialization branch from a5ae79a to 80df581 Compare April 11, 2023 01:59
@glevco glevco force-pushed the bugfix/initialization branch from 80df581 to 406eb93 Compare April 11, 2023 15:35
@glevco glevco merged commit 41670f7 into dev Apr 11, 2023
@glevco glevco deleted the bugfix/initialization branch April 11, 2023 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants