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

[7.6.1] RuntimeError: TorrentState cannot be stored in the database #5928

Closed
sentry-for-tribler bot opened this issue Jan 11, 2021 · 9 comments
Closed
Assignees

Comments

@sentry-for-tribler
Copy link

sentry-for-tribler bot commented Jan 11, 2021

Sentry Issue: TRIBLER-5M

RuntimeError: ('Object TorrentState[new:22] cannot be stored in the database. DatabaseError: database disk image is malformed
--LONG TEXT--
Traceback (most recent call last):
  File "tribler_core/modules/popularity/popularity_community.py", line 121, in process_torrents_health
  File "pony/orm/core.py", line 476, in __exit__
  File "pony/orm/core.py", line 490, in _commit_or_rollback
  File "pony/orm/core.py", line 381, in commit
  File "pony/orm/core.py", line 370, in rollback_and_reraise
  File...
@xoriole xoriole added this to the 7.8.0 January milestone Jan 11, 2021
@drew2a drew2a changed the title RuntimeError: TorrentState cannot be stored in the database [7.6.1] RuntimeError: TorrentState cannot be stored in the database Jan 11, 2021
@drew2a
Copy link
Contributor

drew2a commented Jan 11, 2021

This type of error can be reproduced by replacing normal metadata.db with corrupted metadata.db.
Corrupted metadata.db can be made just by adding some noise text (or binary) to it.

@drew2a
Copy link
Contributor

drew2a commented Jan 11, 2021

The root cause of the corrupted file still remains unknown.

One possible workaround in case of this error:

  1. Create a backup for metadata.db by renaming it to metadata_backup.db
  2. Create a new metadata.db

@drew2a
Copy link
Contributor

drew2a commented Jan 11, 2021

@kozlovsky what do you think, what is the best approach here?

@kozlovsky
Copy link
Contributor

The root cause is indeed unclear. According to some reports, corruption on OS X can be caused by using memory-mapped I/O in SQLite, and in that case, we can recompile SQLite to turn off using memory-mapped I/O.
couchbase/couchbase-lite-ios#1482 (comment)
http://sqlite.1065341.n5.nabble.com/Re-Database-corruption-and-PRAGMA-fullfsync-on-macOS-td95366.html#a95401
But the problem is not limited to the OS X system only, sot it requires further investigation.

One possible workaround in case of this error:

  1. Create a backup for metadata.db by renaming it to metadata_backup.db
  2. Create a new metadata.db

Looks like a correct approach. In principle, it is possible to save at least partial data from the corrupted database by performing backup and then restore. But this way we cannot be sure that the resulted database contains all data. So it is probably not worth it, and we just need to create afresh database so the user can start Tribler without experiencing the same error again.

It may be not easy to rename the database file correctly, as another thread may hold a second connection to the database. Because of this, renaming should be performed at the next startup. As a flag that the renaming is necessary, we can create a file, like metadata.db.corrupted-detected, and check the presence of it on startup.

Beside metadata.db file itself it is also necessary to rename other metadata.db-* files (when present), like metadata.db-journal, metadata.db-wal and metadata.db-shm.

@kozlovsky kozlovsky self-assigned this Jan 11, 2021
@drew2a
Copy link
Contributor

drew2a commented Jan 11, 2021

@kozlovsky yes... Why I offered "rename" — because it should work even there is no free disk space. But this is just a rough idea.

@ichorid
Copy link
Contributor

ichorid commented Jan 12, 2021

Indeed, we don't have to try to save the Channels DB data, because all the data is duplicated in the /channels folder in the form of serialized channel torrents. This includes the user's personal channel. Also, the entries for the user's personal channel torrents are serialized to disk in the same folder, named accordingly.

The solution

In case of DB crash, we can show the user a dialog window offering them to "drop the database and restore personal channel from disk". After that, we create the DB anew and read the user's channel from disk.

@ichorid
Copy link
Contributor

ichorid commented Jan 12, 2021

Actually, duplicate of #5252

@sentry-for-tribler
Copy link
Author

sentry-for-tribler bot commented Apr 15, 2021

Sentry issue: TRIBLER-17

A corrupted database seems like the most frequently reported issue now.

@drew2a drew2a modified the milestones: Next-next release, Backlog Sep 15, 2021
@ichorid
Copy link
Contributor

ichorid commented Oct 12, 2021

the Sentry issue is not there. Closing this one

@ichorid ichorid closed this as completed Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants