fix: high CPU usage when deleting collection#498
Open
fishyy119 wants to merge 1 commit intoximu3:mainfrom
Open
fix: high CPU usage when deleting collection#498fishyy119 wants to merge 1 commit intoximu3:mainfrom
fishyy119 wants to merge 1 commit intoximu3:mainfrom
Conversation
Previously, deleting a document by setting its value to '_delete'
inside db.upsert caused repeated retries, leading to excessive CPU usage.
This occurred because upsert internally retries on conflicts, and returning
the {_deleted: true} flag triggered repeated conflict resolution loops.
This commit moves deletion logic out of upsert into a dedicated removeDoc
method, bypassing the retry loop and eliminating unnecessary CPU load.
The behavior for full-document replacements (#all) and partial updates
remains unchanged.
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical high CPU usage issue that occurs when deleting collections. The problem was caused by using PouchDB's upsert method with {_deleted: true} to delete documents, which triggered internal conflicts in PouchDB leading to infinite retry loops and sustained high CPU usage. The fix replaces this approach with a proper call to the removeDoc method that uses PouchDB's native db.remove() API.
Changes:
- Refactored document deletion logic to use
removeDoc()method instead ofupsertwith_deleted: true - Reorganized imports alphabetically for better code organization
- Removed problematic deletion logic from within the upsert callback
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
在删除某个收藏后,CPU会出现持续的高占用。这主要是因为收藏的删除逻辑使用 upsert 方法将文档值设为 {_deleta: true} 来删除文档,这会触发 PouchDB 内部的冲突,产生无限重试,进而导致持续的高 CPU 占用。
本提交将删除逻辑替换为使用remove方法,以解决该问题。