-
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 5 commits
cea8bd9
75af270
ab078f6
57949ca
7142e2e
bcb7c0b
70add8b
6bf1267
39cddb5
a099357
22dd4a1
10bd532
267e2ab
12ab2c2
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 |
|---|---|---|
|
|
@@ -63,19 +63,18 @@ public void updateByBatch(Map<byte[], byte[]> rows) { | |
| } | ||
|
|
||
| /** | ||
| * close the database. | ||
| * Closes the database and releases all resources. | ||
| * This method ensures that subclass-specific resources are cleaned up | ||
| * before delegating to the parent class for complete resource cleanup. | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| logger.debug("******** Begin to close {}. ********", getName()); | ||
| try { | ||
| writeOptions.close(); | ||
| dbSource.closeDB(); | ||
| } catch (Exception e) { | ||
| logger.warn("Failed to close {}.", getName(), e); | ||
| } finally { | ||
| logger.debug("******** End to close {}. ********", getName()); | ||
| logger.warn("Failed to close writeOptions in {}.", getName(), e); | ||
| } | ||
| super.close(); | ||
|
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. [[SHOULD]]
// TronDatabase
@Override
public void close() {
logger.info("******** Begin to close {}. ********", getName());
doClose();
logger.info("******** End to close {}. ********", getName());
}
protected void doClose() {
try { writeOptions.close(); }
catch (Exception e) { logger.warn("Failed to close writeOptions in {}.", getName(), e); }
try { dbSource.closeDB(); }
catch (Exception e) { logger.warn("Failed to close dbSource in {}.", getName(), e); }
}// CheckPointV2Store
@Override
public void close() {
logger.debug("******** Begin to close {}. ********", getName());
try { writeOptions.close(); }
catch (Exception e) { logger.warn("Failed to close writeOptions in {}.", getName(), e); }
doClose();
logger.debug("******** End to close {}. ********", getName());
}
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. 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. |
||
| } | ||
|
|
||
| } | ||
| 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(); | ||
| } | ||
| } | ||
| } |
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.
The fix for splitting the try-catch blocks is clean and directly addresses the root cause — really nice work isolating each resource cleanup independently!
One small thing: the
"End to close"log moved from afinallyblock to a bare statement after both try-catch blocks. In the (admittedly rare) event that an uncheckedError(e.g.OutOfMemoryError) escapes fromdbSource.closeDB(), this line won't execute — whereas the originalfinallyguaranteed it would. Would it be worth wrapping both try-catch blocks in an outer try-finally?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 the suggestion! Good catch on the Error case. I've moved the log into a finally on the second try-catch block instead of adding an outer try-finally — same guarantee, slightly less nesting. Updated in bcb7c0b.
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.
Nice, putting the log in the second
finallyis a clean alternative — less nesting with the same guarantee. Looks good!