-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(db): resolve resource leakage in database close()
#6688
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
Open
warku123
wants to merge
14
commits into
tronprotocol:develop
Choose a base branch
from
warku123:481_database_leakage_fix
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cea8bd9
Fix resource leakage
warku123 75af270
Merge branch 'tronprotocol:develop' into 481_database_leakage_fix
warku123 ab078f6
fix(db): resolve resource leakage in CheckPointV2Store.close()
warku123 57949ca
Fix review issues: deduplicate logs and simplify exception handling
warku123 7142e2e
fix(db): split try-catch in TronDatabase.close() and improve tests
warku123 bcb7c0b
fix(db): restore finally block for close log in TronDatabase
warku123 70add8b
ci: retry
warku123 6bf1267
test(db): add real-resource close test to restore coverage
warku123 39cddb5
fix(db): extract doClose() hook to keep CheckPointV2Store logs at deb…
warku123 a099357
test(db): cover dbSource exception path in doClose()
warku123 22dd4a1
ci: retry
warku123 10bd532
test(db): add doClose() test to verify dbSource closes despite writeO…
warku123 267e2ab
fix(style): expand anonymous subclass methods to pass checkstyle
warku123 12ab2c2
test(db): cover stub methods in CheckPointV2Store to improve changed-…
warku123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
104 changes: 104 additions & 0 deletions
104
framework/src/test/java/org/tron/core/db/CheckPointV2StoreTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package org.tron.core.db; | ||
|
|
||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.Field; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
| import org.rocksdb.RocksDB; | ||
| import org.tron.common.TestConstants; | ||
| import org.tron.common.storage.WriteOptionsWrapper; | ||
| import org.tron.core.config.args.Args; | ||
| import org.tron.core.db.common.DbSourceInter; | ||
| import org.tron.core.store.CheckPointV2Store; | ||
|
|
||
| public class CheckPointV2StoreTest { | ||
|
|
||
| @ClassRule | ||
| public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
|
||
| static { | ||
| RocksDB.loadLibrary(); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void initArgs() throws IOException { | ||
| Args.setParam( | ||
| new String[]{"-d", temporaryFolder.newFolder().toString()}, | ||
| TestConstants.TEST_CONF | ||
| ); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void destroy() { | ||
| Args.clearParam(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCloseReleasesAllResources() throws Exception { | ||
| CheckPointV2Store store = new CheckPointV2Store("test-close"); | ||
|
|
||
| // Replace dbSource with a mock so we can verify closeDB() | ||
| Field dbSourceField = TronDatabase.class.getDeclaredField("dbSource"); | ||
| dbSourceField.setAccessible(true); | ||
| DbSourceInter<byte[]> originalDbSource = (DbSourceInter<byte[]>) dbSourceField.get(store); | ||
| DbSourceInter<byte[]> mockDbSource = mock(DbSourceInter.class); | ||
| dbSourceField.set(store, mockDbSource); | ||
|
|
||
| try { | ||
| store.close(); | ||
|
|
||
| verify(mockDbSource).closeDB(); | ||
| } finally { | ||
| originalDbSource.closeDB(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCloseDbSourceWhenWriteOptionsThrows() throws Exception { | ||
| CheckPointV2Store store = new CheckPointV2Store("test-close-exception"); | ||
|
|
||
| // Replace child writeOptions with a spy that throws on close | ||
| Field childWriteOptionsField = CheckPointV2Store.class.getDeclaredField("writeOptions"); | ||
| childWriteOptionsField.setAccessible(true); | ||
| WriteOptionsWrapper childWriteOptions = | ||
| (WriteOptionsWrapper) childWriteOptionsField.get(store); | ||
| WriteOptionsWrapper spyChildWriteOptions = spy(childWriteOptions); | ||
| doThrow(new RuntimeException("simulated writeOptions failure")) | ||
| .when(spyChildWriteOptions).close(); | ||
| childWriteOptionsField.set(store, spyChildWriteOptions); | ||
|
|
||
| // Replace parent writeOptions with a spy that throws on close | ||
| Field parentWriteOptionsField = TronDatabase.class.getDeclaredField("writeOptions"); | ||
| parentWriteOptionsField.setAccessible(true); | ||
| WriteOptionsWrapper parentWriteOptions = | ||
| (WriteOptionsWrapper) parentWriteOptionsField.get(store); | ||
| WriteOptionsWrapper spyParentWriteOptions = spy(parentWriteOptions); | ||
| doThrow(new RuntimeException("simulated parent writeOptions failure")) | ||
| .when(spyParentWriteOptions).close(); | ||
| parentWriteOptionsField.set(store, spyParentWriteOptions); | ||
|
|
||
| // Replace dbSource with a mock | ||
| Field dbSourceField = TronDatabase.class.getDeclaredField("dbSource"); | ||
| dbSourceField.setAccessible(true); | ||
| DbSourceInter<byte[]> originalDbSource = (DbSourceInter<byte[]>) dbSourceField.get(store); | ||
| DbSourceInter<byte[]> mockDbSource = mock(DbSourceInter.class); | ||
| dbSourceField.set(store, mockDbSource); | ||
|
|
||
| try { | ||
| store.close(); | ||
|
|
||
| // dbSource.closeDB() must be called even though both writeOptions threw | ||
| verify(mockDbSource).closeDB(); | ||
| } finally { | ||
| originalDbSource.closeDB(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[[SHOULD]]
CheckPointV2Store.close()previously logged atdebugon purpose —SnapshotManager.createCheckpoint()opens/closes one on every checkpoint flush. Delegating tosuper.close()promotes those lines toinfoand will spam logs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Implemented the doClose() hook in 39cddb5 — CheckPointV2Store.close() now logs at debug level and calls doClose() directly, so high-frequency checkpoint flushes won't spam the info log.