-
Notifications
You must be signed in to change notification settings - Fork 702
fix: Prevent premature EventManager shutdown when multiple crawlers share it
#1810
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,9 @@ def __init__( | |
| delay=self._persist_state_interval, | ||
| ) | ||
|
|
||
| # Reference count for active contexts. | ||
| self._ref_count = 0 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
|
|
||
| # Flag to indicate the context state. | ||
| self._active = False | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need |
||
|
|
||
|
|
@@ -102,13 +105,11 @@ def active(self) -> bool: | |
| return self._active | ||
|
|
||
| async def __aenter__(self) -> EventManager: | ||
| """Initialize the event manager upon entering the async context. | ||
| """Initialize the event manager upon entering the async context.""" | ||
| self._ref_count += 1 | ||
|
|
||
| Raises: | ||
| RuntimeError: If the context manager is already active. | ||
| """ | ||
| if self._active: | ||
| raise RuntimeError(f'The {self.__class__.__name__} is already active.') | ||
| if self._ref_count > 1: | ||
| return self | ||
|
|
||
| self._active = True | ||
| self._emit_persist_state_event_rec_task.start() | ||
|
|
@@ -130,9 +131,16 @@ async def __aexit__( | |
| if not self._active: | ||
| raise RuntimeError(f'The {self.__class__.__name__} is not active.') | ||
|
|
||
| self._ref_count -= 1 | ||
|
|
||
| # Emit persist state event to ensure the latest state is saved before closing the context. | ||
| await self._emit_persist_state_event() | ||
|
|
||
| if self._ref_count > 0: | ||
| return | ||
|
|
||
| # Stop persist state event periodic emission and manually emit last one to ensure latest state is saved. | ||
| await self._emit_persist_state_event_rec_task.stop() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having reduced the risk of a possible scenario of two persist state events very close to each other, I would prefer to keep it this way and just add |
||
| await self._emit_persist_state_event() | ||
| await self.wait_for_all_listeners_to_complete(timeout=self._close_timeout) | ||
| self._event_emitter.remove_all_listeners() | ||
| self._listener_tasks.clear() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be more readable to just do it on a standalone line. Something like
contexts_to_enter.append([local_event_manager, global_event_manager])