-
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
base: develop
Are you sure you want to change the base?
Changes from all commits
cea8bd9
75af270
ab078f6
57949ca
7142e2e
bcb7c0b
70add8b
6bf1267
39cddb5
a099357
22dd4a1
10bd532
267e2ab
12ab2c2
ab0db5f
cc57817
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.tron.common.logsfilter.capsule.ContractEventTriggerCapsule; | ||
|
|
@@ -28,6 +29,11 @@ | |
| @Slf4j | ||
| public class FilterQueryTest { | ||
|
|
||
| @After | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @warku123, [SHOULD] This
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The background and rationale are already written up in an earlier inline comment: #6688 (comment) TL;DR: the new tests in this PR shifted Gradle's So these That said, you're right that the root fix belongs outside this PR's scope. My plan is to open a follow-up issue after this merges, covering both:
I'll also update the PR description with a short pointer to the inline comment so reviewers don't have to dig for it. |
||
| public void tearDown() { | ||
| EventPluginLoader.getInstance().setFilterQuery(null); | ||
| } | ||
|
|
||
| @Test | ||
| public synchronized void testParseFilterQueryBlockNumber() { | ||
| assertEquals(LATEST_BLOCK_NUM, parseToBlockNumber(EMPTY)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| 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.Assert; | ||
| 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 testStubMethods() throws Exception { | ||
| CheckPointV2Store store = new CheckPointV2Store("test-stubs"); | ||
| try { | ||
| byte[] key = "key".getBytes(); | ||
|
|
||
| store.put(key, new byte[]{}); | ||
| Assert.assertNull(store.get(key)); | ||
| Assert.assertFalse(store.has(key)); | ||
| store.forEach(item -> { | ||
| }); | ||
| Assert.assertNull(store.spliterator()); | ||
|
|
||
| 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); | ||
| store.delete(key); | ||
| dbSourceField.set(store, originalDbSource); | ||
|
|
||
| java.lang.reflect.Method initMethod = | ||
| CheckPointV2Store.class.getDeclaredMethod("init"); | ||
| initMethod.setAccessible(true); | ||
| initMethod.invoke(store); | ||
| } finally { | ||
| store.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCloseWithRealResources() { | ||
| CheckPointV2Store store = new CheckPointV2Store("test-close-real"); | ||
| // Exercises the real writeOptions.close() and dbSource.closeDB() code paths | ||
| store.close(); | ||
| } | ||
|
|
||
| @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 testCloseWhenDbSourceThrows() throws Exception { | ||
| CheckPointV2Store store = new CheckPointV2Store("test-close-dbsource-throws"); | ||
|
|
||
| Field dbSourceField = TronDatabase.class.getDeclaredField("dbSource"); | ||
| dbSourceField.setAccessible(true); | ||
| DbSourceInter<byte[]> originalDbSource = (DbSourceInter<byte[]>) dbSourceField.get(store); | ||
| DbSourceInter<byte[]> mockDbSource = mock(DbSourceInter.class); | ||
| doThrow(new RuntimeException("simulated dbSource failure")).when(mockDbSource).closeDB(); | ||
| dbSourceField.set(store, mockDbSource); | ||
|
|
||
| try { | ||
| store.close(); | ||
| } 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(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,7 @@ public static void init() { | |
|
|
||
| @Before | ||
| public void before() throws IOException { | ||
| EventPluginLoader.getInstance().setFilterQuery(null); | ||
| Args.getInstance().setNodeListenPort(10000 + port.incrementAndGet()); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defensive reset unrelated to this PR's resource-leakage fix, but needed to stabilize the JaCoCo overall-delta gate. Root cause: Evidence:
Fix: Note: This singleton pollution pattern may exist elsewhere in the codebase — any test that writes to a static singleton (e.g. |
||
|
|
||
| dbManager = context.getBean(Manager.class); | ||
|
|
||
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.
@warku123, [SHOULD] The fact that
testCloseDbSourceWhenWriteOptionsThrowshas to reflectively set BOTHCheckPointV2Store.writeOptionsandTronDatabase.writeOptionsproves the subclass has its ownwriteOptionsfield shadowing the parent's — twoWriteOptionsWrapperinstances live side by side. This is pre-existing, but the newclose()structure makes the implicit contract "child closes its ownwriteOptionsfirst, then delegates to parent" very fragile. Consider either promoting the parent field toprotectedand removing the subclass field, or adding a class-level comment explaining why twowriteOptionsinstances are intentional.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.
Thanks for catching this. The two
WriteOptionsWrapperinstances are intentional rather than accidental shadowing:TronDatabase.writeOptionsis configured withisDbSync()CheckPointV2Store.writeOptionsis configured withisCheckpointSync()(a dedicated checkpoint-specific sync flag)Collapsing them into a single
protectedfield would change the sync semantics for either the parent or the checkpoint store, so it's not a zero-risk refactor. Since this shadowing pre-dates the PR and the scope here is the resource-leak fix, I'd prefer to leave the structure as-is and file a follow-up issue if you'd like the shadowing formally addressed. Happy to add a class-level comment documenting the intent if that would help.