Skip to content

Fix/autosave background write#32700

Open
CubikingChill wants to merge 1 commit intomusescore:masterfrom
CubikingChill:fix/autosave-background-write
Open

Fix/autosave background write#32700
CubikingChill wants to merge 1 commit intomusescore:masterfrom
CubikingChill:fix/autosave-background-write

Conversation

@CubikingChill
Copy link
Contributor

Resolves: #31596 (Maybe)

Move autosave file I/O to background thread to reduce UI stutter

Problem

Autosave runs entirely on the main thread via a QTimer. The file I/O steps — writeFile, copy, and remove — are blocking operations that stutter the UI mid-editing, which is disruptive to workflow.

Solution

After serialization completes on the main thread (unchanged), move the file I/O steps to a background thread using Concurrent::run when MUSE_THREADS_SUPPORT is available.

The serialization itself stays on the main thread because:

  • It's read-only on the score data
  • It writes to an in-memory Buffer, not disk
  • The buffer is fully complete before the background thread starts, so no locking is needed

When threads are not supported, autosave falls back to the existing synchronous behavior.

What is not changed

  • Manual save, SaveAs, SaveCopy — all unchanged
  • Serialization logic — unchanged
  • Backup generation — not applicable for autosave
  • Corruption check — not applicable for autosave

How to test

  1. Open a large score with multiple parts
  2. Edit the score and wait for autosave to trigger
  3. Verify no UI stutter occurs during autosave
  4. Verify the autosave file is written correctly and can be recovered
  • I signed the CLA
  • The title of the PR describes the problem it addresses
  • Each commit's message describes its purpose and effects, and references the issue it resolves
  • If changes are extensive, there is a sequence of easily reviewable commits
  • The code in the PR follows the coding rules
  • There are no unnecessary changes
  • The code compiles and runs on my machine, preferably after each commit individually
  • I created a unit test or vtest to verify the changes I made (if applicable)

@CubikingChill CubikingChill force-pushed the fix/autosave-background-write branch 2 times, most recently from 0534f37 to 857263f Compare March 19, 2026 17:43
QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther);
LOGD() << "Autosave: background write complete: " << targetContainerPathCopy;
});
return make_ret(Ret::Code::Ok);
Copy link
Contributor

Choose a reason for hiding this comment

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

The idea is very interesting.

But it's confusing that we have duplicate logic for both MUSE_THREADS_SUPPORT and non-MUSE_THREADS_SUPPORT, and it's currently written in a way that could be different.

How can we do this better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. Let me know your opinion please.

@CubikingChill CubikingChill force-pushed the fix/autosave-background-write branch 3 times, most recently from 0b12ad5 to 971f1e8 Compare March 24, 2026 11:42
@CubikingChill CubikingChill force-pushed the fix/autosave-background-write branch from 971f1e8 to ae52e5d Compare March 24, 2026 11:42
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.

Always freezes when working on a score for a short time

2 participants