Skip to content

Fix corruption on restore - #11378

Merged
kryksyh merged 10 commits into
audacity:masterfrom
kryksyh:fix_corruption_on_restore
Jul 8, 2026
Merged

kryksyh merged 10 commits into
audacity:masterfrom
kryksyh:fix_corruption_on_restore

Conversation

@kryksyh

@kryksyh kryksyh commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

Resolves: #11359

0e223066 - fixes the corruption as described in #11359, the rest is additional protection and recoverability improvements

  • when closing a recovered unsaved project we should drop autosave
  • there was a tiny but possible corruption when the disk is full
  • there was a possible data loss on close if, for example, an external drive was unplugged immediately after closing the project
  • when disk went full, the checkpointing was stopped, so on close wal file was left unmerged

For better recoverability, was added project journal with up to 10 snapshots and a length field for each waveblock

  • I signed CLA
  • The title of the pull request describes an issue it addresses
  • If changes are extensive, then there is a sequence of easily reviewable commits
  • Each commit's message describes its purpose and effects
  • There are no behavior changes unnecessary for the stated purpose of the PR

Recommended:

  • Each commit compiles and runs on my machine without known undesirable changes of behavior

QA:

  • Testflow test cases have been run

@kryksyh
kryksyh force-pushed the fix_corruption_on_restore branch from 76f1208 to 96dc31a Compare July 7, 2026 17:41
@coderabbitai

coderabbitai Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 17e3dc3c-c27a-42d2-a847-3f7f477ca150

📥 Commits

Reviewing files that changed from the base of the PR and between 41d11d4e3394df04b1e476dc9fc97482fa76ede2 and 739a120.

📒 Files selected for processing (20)
  • au3/libraries/au3-project-file-io/DBConnection.cpp
  • au3/libraries/au3-project-file-io/DBConnection.h
  • au3/libraries/au3-project-file-io/ProjectFileIO.cpp
  • au3/libraries/au3-project-file-io/SqliteSampleBlock.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.h
  • au3/libraries/au3-wave-track/Sequence.cpp
  • src/au3wrap/internal/au3project.cpp
  • src/project/tests/audacityproject_tests.cpp
  • src/project/tests/data/empty.aup4
  • src/project/tests/data/legacy_schema.aup4
  • src/project/tests/data/test.aup4
  • src/project/tests/realtimeeffectspersistence_tests.cpp
  • src/project/tests/testtools.h
  • src/projectscene/tests/data/test.aup4
  • src/trackedit/tests/au3interactiontestbase.h
  • src/trackedit/tests/au3trackeditclipboard_tests.cpp
  • src/trackedit/tests/data/empty.aup4
  • src/trackedit/tests/data/test.aup4
  • src/trackedit/tests/data/testClipboard.aup4

📝 Walkthrough

Walkthrough

This PR replaces the project schema version constants with ProjectSchemaVersion, adds main.project_history with schema upgrade and retention logic, and updates project writing, copying, loading, and compacting to use the new schema behavior. It adds transaction mutex locking to write paths, changes checkpoint and close handling, writes waveblock length attributes, and updates project tests and fixtures to .aup4 working copies and schema migration checks.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ProjectFileIO
  participant DBConnection
  participant SQLite
  ProjectFileIO->>DBConnection: TransactionMutex()
  ProjectFileIO->>SQLite: CheckVersion() / UpgradeSchema()
  ProjectFileIO->>SQLite: WriteDoc() inserts project_history
  ProjectFileIO->>SQLite: LoadProject() validates read-only and decode state
  DBConnection->>SQLite: WAL checkpoint on close
Loading
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #11359 by improving restore/close handling and adding recoverability safeguards for reopened projects.
Out of Scope Changes check ✅ Passed The extra schema, journal, and test updates still support the stated corruption-recovery fix and do not look unrelated.
Title check ✅ Passed The title is concise and directly matches the main change: fixing restore-related corruption.
Description check ✅ Passed The description includes the required resolve link, summary, checklist, and QA section, with only minor incompleteness.

Warning

Review ran into problems

🔥 Problems

Linked repositories: Public OSS repositories can only analyze public repositories installed in this organization. No linked repositories were analyzed; skipped musescore/musescore.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kryksyh
kryksyh force-pushed the fix_corruption_on_restore branch from 96dc31a to 64073d5 Compare July 7, 2026 17:44

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: bff679ec-a149-44a2-85f2-eaa88bebcad1

📥 Commits

Reviewing files that changed from the base of the PR and between 655cbaa and 96dc31a1ab4e726d1c441474524ff33d89d3964b.

📒 Files selected for processing (8)
  • au3/libraries/au3-project-file-io/DBConnection.cpp
  • au3/libraries/au3-project-file-io/DBConnection.h
  • au3/libraries/au3-project-file-io/ProjectFileIO.cpp
  • au3/libraries/au3-project-file-io/SqliteSampleBlock.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.h
  • au3/libraries/au3-wave-track/Sequence.cpp
  • src/au3wrap/internal/au3project.cpp

@kryksyh
kryksyh force-pushed the fix_corruption_on_restore branch from 64073d5 to c7745cb Compare July 8, 2026 08:03

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: e2880b8e-4a5c-4135-be3b-970ed40e561a

📥 Commits

Reviewing files that changed from the base of the PR and between 64073d5c59b7a95c214ff9a75a692fb4fd60bf11 and c7745cb2276a1b150aec220c74662014507d585a.

📒 Files selected for processing (20)
  • au3/libraries/au3-project-file-io/DBConnection.cpp
  • au3/libraries/au3-project-file-io/DBConnection.h
  • au3/libraries/au3-project-file-io/ProjectFileIO.cpp
  • au3/libraries/au3-project-file-io/SqliteSampleBlock.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.h
  • au3/libraries/au3-wave-track/Sequence.cpp
  • src/au3wrap/internal/au3project.cpp
  • src/project/tests/audacityproject_tests.cpp
  • src/project/tests/data/empty.aup4
  • src/project/tests/data/legacy_schema.aup4
  • src/project/tests/data/test.aup4
  • src/project/tests/realtimeeffectspersistence_tests.cpp
  • src/project/tests/testtools.h
  • src/projectscene/tests/data/test.aup4
  • src/trackedit/tests/au3interactiontestbase.h
  • src/trackedit/tests/au3trackeditclipboard_tests.cpp
  • src/trackedit/tests/data/empty.aup4
  • src/trackedit/tests/data/test.aup4
  • src/trackedit/tests/data/testClipboard.aup4
✅ Files skipped from review due to trivial changes (2)
  • src/project/tests/testtools.h
  • src/project/tests/realtimeeffectspersistence_tests.cpp
🚧 Files skipped from review as they are similar to previous changes (8)
  • au3/libraries/au3-project-file-io/DBConnection.h
  • au3/libraries/au3-project-file-io/SqliteSampleBlock.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.h
  • au3/libraries/au3-project-file-io/DBConnection.cpp
  • src/au3wrap/internal/au3project.cpp
  • au3/libraries/au3-project/ProjectFormatVersion.cpp
  • au3/libraries/au3-wave-track/Sequence.cpp
  • au3/libraries/au3-project-file-io/ProjectFileIO.cpp

@kryksyh
kryksyh force-pushed the fix_corruption_on_restore branch 2 times, most recently from 4e611c8 to 41d11d4 Compare July 8, 2026 08:27
kryksyh added 10 commits July 8, 2026 16:19
When opening a dirty project the loaded doc is an "autosave" that the user could reject and rollback to the last saved one.
If we delete orphans now we may delete blocks that are referenced by the saved project, and this will cause a corruption: audacity#11359

Orphans will be removed on close anyway
NOTE: This is a speculation, and might be not the case. A scenario:

- AudioThread/: User is recording an audio, new Block audacity#500 is created
- MainThread: starts autosaving, updates project doc and opens TransactionScope
- AudioThread: New audio block 501 gets into block list, it gets into the current transaction, and AudioThread thinks it is saved
- MainThread: gets an error (out of disk?), and Transaction is rolled back, including block 501
- Sequence still holds reference to block 501, even though it was never stored

This mutex prevents simultaneous doc and sample write to the database
Also explicitly checkpoint on close
Length value is used during project recovery only, not used during normal operations
@kryksyh
kryksyh force-pushed the fix_corruption_on_restore branch from 41d11d4 to 739a120 Compare July 8, 2026 13:20
@kryksyh
kryksyh merged commit 3fa8c13 into audacity:master Jul 8, 2026
3 of 10 checks passed
@kryksyh
kryksyh deleted the fix_corruption_on_restore branch July 8, 2026 13:20
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 18, 2026
When opening a dirty project the loaded doc is an "autosave" that the user could reject and rollback to the last saved one.
If we delete orphans now we may delete blocks that are referenced by the saved project, and this will cause a corruption: audacity#11359

Orphans will be removed on close anyway

Backport of ac65627 (audacity#11378)
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 18, 2026
NOTE: This is a speculation, and might be not the case. A scenario:

- AudioThread/: User is recording an audio, new Block audacity#500 is created
- MainThread: starts autosaving, updates project doc and opens TransactionScope
- AudioThread: New audio block 501 gets into block list, it gets into the current transaction, and AudioThread thinks it is saved
- MainThread: gets an error (out of disk?), and Transaction is rolled back, including block 501
- Sequence still holds reference to block 501, even though it was never stored

This mutex prevents simultaneous doc and sample write to the database

Backport of 8890bb5 (audacity#11378)
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 18, 2026
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 18, 2026
Also explicitly checkpoint on close

Backport of 00479c0 (audacity#11378)
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 18, 2026
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 18, 2026
Length value is used during project recovery only, not used during normal operations

Backport of aacedf2 (audacity#11378)
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 19, 2026
When opening a dirty project the loaded doc is an "autosave" that the user could reject and rollback to the last saved one.
If we delete orphans now we may delete blocks that are referenced by the saved project, and this will cause a corruption: audacity#11359

Orphans will be removed on close anyway

Backport of ac65627 (audacity#11378)
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 19, 2026
NOTE: This is a speculation, and might be not the case. A scenario:

- AudioThread/: User is recording an audio, new Block audacity#500 is created
- MainThread: starts autosaving, updates project doc and opens TransactionScope
- AudioThread: New audio block 501 gets into block list, it gets into the current transaction, and AudioThread thinks it is saved
- MainThread: gets an error (out of disk?), and Transaction is rolled back, including block 501
- Sequence still holds reference to block 501, even though it was never stored

This mutex prevents simultaneous doc and sample write to the database

Backport of 8890bb5 (audacity#11378)
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 19, 2026
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 19, 2026
Also explicitly checkpoint on close

Backport of 00479c0 (audacity#11378)
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 19, 2026
kryksyh added a commit to kryksyh/audacity that referenced this pull request Aug 19, 2026
Length value is used during project recovery only, not used during normal operations

Backport of aacedf2 (audacity#11378)
kryksyh added a commit that referenced this pull request Aug 19, 2026
This may resolve these issues, but we can't reproduce them. We will keep
the issues open for now to improve discoverability if more reports come
in:
- #1754
- #11359
- #11419

Might fix:  #8319, #11244

This pr is a backport of #11378

Changes: 

* when closing a recovered unsaved project we should drop autosave
* there was a tiny but possible corruption when the disk is full
* there was a possible data loss on close if, for example, an external
drive was unplugged immediately after closing the project
* when disk went full, the checkpointing was stopped, so on close wal
file was left unmerged
* For better recoverability, a length field for each waveblock was added

- [x] I signed [CLA](https://www.audacityteam.org/cla/)
- [x] The title of the pull request describes an issue it addresses
- [x] If changes are extensive, then there is a sequence of easily
reviewable commits
- [x] Each commit's message describes its purpose and effects
- [x] There are no behavior changes unnecessary for the stated purpose
of the PR

Recommended:
- [ ] Each commit compiles and runs on my machine without known
undesirable changes of behavior

QA:

- [ ] Testflow test cases have been run
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.

Project gets Broken after re-opening Project

2 participants