You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by "paulirwin (via GitHub)" <gi...@apache.org> on 2026/06/17 21:26:22 UTC

Re: [I] Search crash [lucenenet]

paulirwin commented on issue #1013:
URL: https://github.com/apache/lucenenet/issues/1013#issuecomment-4735696330

   An update on this: I have definitively found the cause of the originally reported crash. And it's fairly interesting, at least to me.
   
   ### Workaround
   
   First, a workaround (which could suffice as a TL;DR). This is a MMapDirectory issue only (as indicated by the "UnmanagedMemoryAccessor" in the stack trace, which is an inlined call from memory-mapped file code). You can work around this issue in the meantime by switching to a different directory implementation, like SimpleFSDirectory or NIOFSDirectory.
   
   ### Detailed Explanation
   
   To understand this, it's worth looking at upstream Lucene for historical context. MMapDirectory has this thing called the ["unmap hack"](https://github.com/apache/lucene/blob/f01152a5909fa6059f4f1d4aeb4e14968ef1d8c2/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java#L154-L170) to work around a missing feature in the JDK. This defaults to being enabled if [it is supported in your Java runtime](https://github.com/apache/lucene/blob/f01152a5909fa6059f4f1d4aeb4e14968ef1d8c2/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java#L137-L152), but can be manually turned on or off. 
   
   The unmap hack will deterministically try to unmap the mapped file when the IndexInput is closed, except it has a gnarly flaw: if this is done by another thread and a different thread still has it mapped and tries to read, it will crash the JVM with a SIGSEGV (basically, an AccessViolationException - sound familiar?). (And FYI: this is called a "hack" because it pokes at internals via reflection to do this unmap, which is indeed a hack.)
   
   If you _don't_ have the unmap hack turned on, then it will fall back to the JVM's [MappedByteBuffer](https://docs.oracle.com/javase/8/docs/api/java/nio/MappedByteBuffer.html) default behavior, which will "remain valid until the buffer itself is garbage-collected." That prevents the SIGSEGV, but that means that file handles are not deterministically cleaned up until GC time. This aligns with the [docs](https://github.com/apache/lucene/blob/f01152a5909fa6059f4f1d4aeb4e14968ef1d8c2/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java#L50-L54): "Only when GC finally collects the underlying objects, which could be quite some time later, will the file handle be closed."
   
   (Aside: this comment also blames this on a Sun JVM "bug," which I assume they meant "bug" in the sense of "bug tracker" since it's more of a feature request for a missing API. The link in the docs has since bit-rotted, but is available now here: https://bugs.openjdk.org/browse/JDK-4724038 - the core request was to add an unmap method. I'm not sure this would have definitively solved it alone; it would have also required reworking the Lucene code. Regardless, this was closed in favor of the FFM API that was added in Java 22, which Lucene 10.x has since adopted.)
   
   So Lucene as of 4.x requires you to choose from two not-great options with MMapDirectory: either non-deterministically clean up the file handles potentially "quite some time later" via GC, or risk a SIGSEGV with concurrent closes. We currently take the latter approach, accidentally. In other words, we have the "unmap hack" effectively applied in our code all of the time.
   
   While we _could_ make our users have this same dilemma, I think we can reach a middle-ground, which I'm attempting to do now with the new PR #1361: deterministically clean up when called by the same thread, or non-deterministically clean up at GC time when called by a different thread. This demonstrably avoids an AVE in the concurrent case (and falls back to the MappedByteBuffer-like behavior), and in the same-thread case is able to safely, deterministically clean up at that moment.
   
   Also note that we can't take clever approaches like per-clone independent mappings, thread-local storage, etc. to try to avoid this dilemma, because we could exhaust address space with multiple redundant mappings. Synchronizing around the state fixes the AVE and allows us to deterministically clean up, but at the expense of performance; and if MMapDirectory is as slow as SimpleFSDirectory, then what even is the point of its existence, other than possibly reducing some buffer memory churn?
   
   I attempted analyzing what it would take to backport the Lucene 10.x approach, but the FFM API is quite big and complex, and doesn't have a direct .NET equivalent as far as I can tell from a first pass at research. It'll require more analysis later when we get to porting the 10.x code, or if this PR is unacceptable for some reason.
   
   Regardless of how this solution and PR plays out, I think we should strongly consider removing it as a default directory implementation from `FSDirectory.Open` in all cases, and allow people to opt-in to using this if they determine that it works reliably for their use cases. It's perhaps a good thing that it was the default, because otherwise we wouldn't know about these bugs. But, given the tradeoffs, I think it's best to default to a stable and safe option instead of this potentially-non-deterministic behavior. (Related: this may influence #1301 and I'll certainly propose this over there.)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org