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

Fix issue creating many snapshots #8392

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,8 @@
Changelog
=========

* :bug:`-` Fix an error introduced in 1.34.2 that was creating snapshots more frequently than expected.

* :release:`1.34.2 <2024-08-09>`
* :bug:`-` Users will be able to filter by event subtype in the history events view.
* :feature:`-` New event type/subtype combinations added. Receive/payment to receive a payment for something, Spend/payment to pay for something, Receive/Grant to receive a grant. Accounting wise they are treated like normal spend/receive and receive donation respectively but it helps with filtering and categorization during history searching.
Expand Down
5 changes: 3 additions & 2 deletions rotkehlchen/tasks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 538,10 @@ def _maybe_update_snapshot_balances(self) -> Optional[list[gevent.Greenlet]]:
and the current time exceeds the `balance_save_frequency`.
"""
with self.database.conn.read_ctx() as read_cursor:
if self.database.should_save_balances(read_cursor):
maybe_detect_new_tokens(self.database)
if not self.database.should_save_balances(read_cursor):
return None

maybe_detect_new_tokens(self.database)
task_name = 'Periodically update snapshot balances'
log.debug(f'Scheduling task to {task_name}')
return [self.greenlet_manager.spawn_and_track(
Expand Down
36 changes: 36 additions & 0 deletions rotkehlchen/tests/unit/test_tasks_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,3 1078,39 @@ def maybe_task():
# shouldn't raise any exception because we released it
GlobalDBHandler().packaged_db_lock.acquire()
assert len(task_manager.running_greenlets) == 0


@pytest.mark.parametrize('max_tasks_num', [5])
@pytest.mark.parametrize('number_of_eth_accounts', [0])
def test_snapshots_dont_happen_always(rotkehlchen_api_server: 'APIServer') -> None:
"""Regression test for an issue we had where the task for snapshots was
creating one for each run of the background task.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
creating one for each run of the background task.
creating a snapshot for each run of the background task.

"""
rotki = rotkehlchen_api_server.rest_api.rotkehlchen
task_manager = cast(TaskManager, rotki.task_manager)

task_manager.potential_tasks = [task_manager._maybe_update_snapshot_balances]
task_manager.should_schedule = True

query = 'SELECT COUNT(*) FROM timed_location_data'
with rotki.data.db.conn.read_ctx() as cursor:
assert cursor.execute(query).fetchone()[0] == 0

# Schedule the task and check that we got one snapshot.
task_manager.schedule()
gevent.joinall(rotki.greenlet_manager.greenlets)
assert cursor.execute(query).fetchone()[0] == 1

# Schedule again. The job shouldn't run.
task_manager.schedule()
gevent.joinall(rotki.greenlet_manager.greenlets)
assert cursor.execute(query).fetchone()[0] == 1

with patch( # Force a new snapshot.
'rotkehlchen.db.dbhandler.DBHandler.get_last_balance_save_time',
return_value=Timestamp(0),
):
task_manager.schedule()
gevent.joinall(rotki.greenlet_manager.greenlets)

assert cursor.execute(query).fetchone()[0] == 2
Loading