Gh 279 env close protection by at055612 · Pull Request #293 · lmdbjava/lmdbjava
Repository navigation
Gh 279 env close protection - #293
Open
at055612 wants to merge 62 commits into master from gh-279-env-close-protection
Open
Gh 279 env close protection #293 at055612 wants to merge 62 commits into master from gh-279-env-close-protection
Conversation
at055612 commented Aug 10, 2026 · edited Loading
Copy link Copy Markdown
Collaborator
Addresses #279
The aim of this PR is to add optional protection to prevent the Env from being closed if there are transactions or cursors open. #279 discussed some options for implementing this protection using reference counting including using a simple AtomicInteger and LongAdder. LongAdder is no-go as it is not thread safe in the way we need to use it.
This PR uses a reference counter that uses an array of AtomicIntegers for improved multi threaded performance. This is similar to what LongAdder does, but adds in concurrency controls to allow a count to happen in a thread safe way. I have various tests to check this and AI has checked over it, but would welcome anyone picking holes in it in case I have missed some race condition.
Rather than conflate this protection with the existing SHOULD_CHECK logic, it is enabled via these four new Env.Builder methods:
Env.create() .setSafeClose() // default is NO close protection if not called .setSafeClose(true) // default is NO close protection if not called .setSingleThreaded() // default is multithreaded if not called .setSingleThreaded(true) // default is multithreaded if not called
It may be beneficial to add a new system property to turn on/off safeClose to allow it to be turned on in dev, however users of the library can create their own system property to control the value for setSafeClose(enabled).
If safeClose and singleThreaded are set, a non-thread-safe reference counter is used that relies on a simple int and no concurrency protection. This will be faster and use less memory.
IfsafeClose and singleThreaded are both un-set, the Env will use a no-op implementation of RefCounter. I would hope that the JVM will optimise these no-op method calls out to remove the tiny overhead that they add.
If only safeClose is set, the striped thread safe reference counter implementation will be used.
If only singleThreaded is set, this has no impact on the Env, as this flag currently only impacts the choice of reference counter impl. It is possible that the singleThreaded flag on the env could also be used in the future to control how LMDBJava implements other things when used in a strictly single threaded way.
When safeClose is set, calling Env.close() will result in a EnvInUseException being thrown if there are open cursors/txns. It will not block or wait.
I started this PR quite a while ago, but have changed it to adopt some of the builder changes from #289 . I disagree with the close(timeout) in #289 on the basis that it should be the responsibility of the calling code to determine when to close the env, rather than LMDBJava's.
I have added a tryClose() method which returns true if the close could be performed or false otherwise. With safeClose enabled, this means tryClose() will return false if the env is in use rather than throwing.
Add RefCounter to Env to prevent closure while in use WIP
5609b3d
Tidy code
2a0a6c0
Change checkNotClosed to allow things to close down in CLOSING state
81e8ae7
Fix acquire behaviour in Txn/Cursor ctors
5a4585e
Add onClose to NoOpRefCounter
a7a99c9
Tweak perf test
d02f01b
Improve stripe hashing with golden ratio
7b83648
Add StampedLockRefCounterImpl WIP
301df13
Add more ref counters, improve tests
e40ee22
Refactor ref counter classes
91df336
Tidy code, add tests
9f7043d
Fix bug in StripedRefCounter, improve tests
e48f1c7
Rename class
a6080b3
Refactor test classes
f9138df
Improve perfTest
1b84247
Fix NoOpRefCounter close method
49438e9
Return Cursor.close() to original behavior
36019a3
Fix logic in SimpleRefCounter
3ce28de
Improve close() method
8d170c7
Remove redundant method & ctor from SimpleRefCounter
4b84cf6
Improve SimpleRefCounter
a6eb02b
Remove redundant EnvState
ded736e
Fix SimpleRefCounter.release()
cb890be
Change (Striped|Simple)RefCounter to not throw in lambda
b61dbf9
Make perf test runs more consistent in terms of work done
Add JMH benchmark for RefCounter
e2e7d5b
gh-279 Fix concurrency issues in StripedRefCounter
5a5b8bf
gh-279 Tidy code, add tests
37dfbdb
gh-279 Add Env.Builder.safeClose()
519479f
gh-279 Fix tests
8c9c544
gh-279 Run mvn build
3c2f181
at055612
added 6 commits
August 10, 2026 14:12
Merge branch 'master' into gh-279-env-close-protection
ed0df73
gh-279 Address GH code fix suggestions
8c001e6
gh-279 Add coverage
290522d
gh-279 Format
cccf9dd
gh-279 Adding in tests written by @bernardladenthin
819881a
gh-279 Add test coverage
6ceea10
github-advanced-security
Bot
found potential problems Aug 10, 2026
View reviewed changes
at055612 added 18 commits August 10, 2026 15:19
gh-279 Add test coverage
fae66ee
gh-279 Fix GH suggestions
04043b5
gh-279 Add test coverage
58f77e1
gh-279 Fix failing test
0729728
gh-279 Fix catch in Cursor ctor
b083c29
gh-279 Tidy asserts in TxnTest
859c0db
gh-279 Add Env.tryClose, add more tests
3c2ad81
gh-279 Add test for Cursor ctor exception
a9228db
gh-279 Remove dead code, add test for code coverage
2ce1c76
gh-279 Improve Env.copy tests
8aa99a1
Add test for KeyVal.close
ecf7251
gh-279 Add RefCounter.run test
7565c94
gh-279 Fix AbstractFlagSetTest
b486b76
gh-279 Changes to AbstractFlagSetTest for codecov
c3a05d7
gh-279 Format
9c7f97c
gh-279 Add tests to TargetNameTest
bfdefef
gh-279 Change safeClose to only track RW cursors
eefc87f
Add more cursor tests, improve javadoc
gh-279 Add more tests, improve javadoc
017ef34
github-advanced-security
Bot
found potential problems Aug 14, 2026
View reviewed changes
at055612 added 2 commits August 14, 2026 12:31
gh-279 Fix codeQL suggestion
ab0fc6a
gh-279 Tweak javadoc
e8fdd2d
at055612
mentioned this pull request
Aug 14, 2026
Make Env.closed volatile and document the close() lifecycle contract
#288
Closed
bernardladenthin
mentioned this pull request
Nov 14, 2026
Profile: tighter page, upstream work as one table
bernardladenthin/bernardladenthin#9
Merged
Sign up for free
to join this conversation on GitHub .
Already have an account?
Sign in to comment