GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Gh 279 env close protection by at055612 · Pull Request #293 · lmdbjava/lmdbjava

Conversation

@at055612

@at055612 at055612 commented Aug 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
at055612 added 30 commits January 27, 2026 11:11
@at055612
Add RefCounter to Env to prevent closure while in use WIP
5609b3d
@at055612
Tidy code
2a0a6c0
@at055612
Change checkNotClosed to allow things to close down in CLOSING state
81e8ae7
@at055612
Fix acquire behaviour in Txn/Cursor ctors
5a4585e
@at055612
Add onClose to NoOpRefCounter
a7a99c9
@at055612
Tweak perf test
d02f01b
@at055612
Improve stripe hashing with golden ratio
7b83648
@at055612
Add StampedLockRefCounterImpl WIP
301df13
@at055612
Add more ref counters, improve tests
e40ee22
@at055612
Refactor ref counter classes
91df336
@at055612
Tidy code, add tests
9f7043d
@at055612
Fix bug in StripedRefCounter, improve tests
e48f1c7
@at055612
Rename class
a6080b3
@at055612
Refactor test classes
f9138df
@at055612
Improve perfTest
1b84247
@at055612
Fix NoOpRefCounter close method
49438e9
@at055612
Return Cursor.close() to original behavior
36019a3
@at055612
Fix logic in SimpleRefCounter
3ce28de
@at055612
Improve close() method
8d170c7
@at055612
Remove redundant method & ctor from SimpleRefCounter
4b84cf6
@at055612
Improve SimpleRefCounter
a6eb02b
@at055612
Remove redundant EnvState
ded736e
@at055612
Fix SimpleRefCounter.release()
cb890be
@at055612
Change (Striped|Simple)RefCounter to not throw in lambda
b61dbf9
Make perf test runs more consistent in terms of work done
@at055612
Add JMH benchmark for RefCounter
e2e7d5b
@at055612
gh-279 Fix concurrency issues in StripedRefCounter
5a5b8bf
@at055612
gh-279 Tidy code, add tests
37dfbdb
@at055612
gh-279 Add Env.Builder.safeClose()
519479f
@at055612
gh-279 Fix tests
8c9c544
@at055612
gh-279 Run mvn build
3c2f181
at055612 added 7 commits August 10, 2026 14:11
@at055612
gh-279 Add test
0831fc9
@at055612
Merge branch 'master' into gh-279-env-close-protection
ed0df73
@at055612
gh-279 Address GH code fix suggestions
8c001e6
@at055612
gh-279 Add coverage
290522d
@at055612
gh-279 Format
cccf9dd
@at055612
gh-279 Adding in tests written by @bernardladenthin
819881a
@at055612
gh-279 Add test coverage
6ceea10
github-advanced-security[bot]
github-advanced-security Bot found potential problems Aug 10, 2026
View reviewed changes
Comment thread src/test/java/org/lmdbjava/EnvTest.java Fixed

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/test/java/org/lmdbjava/EnvTest.java Fixed

Uh oh!

There was an error while loading. Please reload this page.

at055612 added 18 commits August 10, 2026 15:19
@at055612
gh-279 Add test coverage
fae66ee
@at055612
gh-279 Fix GH suggestions
04043b5
@at055612
gh-279 Add test coverage
58f77e1
@at055612
gh-279 Fix failing test
0729728
@at055612
gh-279 Fix catch in Cursor ctor
b083c29
@at055612
gh-279 Tidy asserts in TxnTest
859c0db
@at055612
gh-279 Add Env.tryClose, add more tests
3c2ad81
@at055612
gh-279 Add test for Cursor ctor exception
a9228db
@at055612
gh-279 Remove dead code, add test for code coverage
2ce1c76
@at055612
gh-279 Improve Env.copy tests
8aa99a1
@at055612
Add test for KeyVal.close
ecf7251
@at055612
gh-279 Add RefCounter.run test
7565c94
@at055612
gh-279 Fix AbstractFlagSetTest
b486b76
@at055612
gh-279 Changes to AbstractFlagSetTest for codecov
c3a05d7
@at055612
gh-279 Format
9c7f97c
@at055612
gh-279 Add tests to TargetNameTest
bfdefef
@at055612
gh-279 Change safeClose to only track RW cursors
eefc87f
Add more cursor tests, improve javadoc
@at055612
gh-279 Add more tests, improve javadoc
017ef34
github-advanced-security[bot]
github-advanced-security Bot found potential problems Aug 14, 2026
View reviewed changes
Comment thread src/test/java/org/lmdbjava/TestUtils.java Fixed

Uh oh!

There was an error while loading. Please reload this page.

at055612 added 2 commits August 14, 2026 12:31
@at055612
gh-279 Fix codeQL suggestion
ab0fc6a
@at055612
gh-279 Tweak javadoc
e8fdd2d
@at055612 at055612 mentioned this pull request Aug 14, 2026
Make Env.closed volatile and document the close() lifecycle contract #288
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@github-advanced-security github-advanced-security[bot] github-advanced-security[bot] left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@at055612 @github-advanced-security
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.