Repository navigation
GH-51663: [C++][Gandiva] Race condition with Gandiva expressions - #51665
Merged
Merged
Conversation
) and fixed it on the arrow-java side with a coarse grained lock on the Projector and Filter Make methods. That fixed the issue but adds some undesirable overhead in certain multi threaded workflows. This issue captures the deeper problem that is occurring in the C++ side of Gandiva and was not touched by my previous fix though it is prevented from happening. I have a proposed solution for this and believe it is a better overall solution. Projector::Make() and Filter::Make() read the shared expression cache once to decide the is_cached status and then call SetLLVMObjectCache(). That performs its own second, unsynchronized read of the same key before pre-loading a cached object into the LLJIT. Those two reads could disagree. If another thread compiling the identical (schema, expressions, selection vector mode, configuration) tuple inserted between them, the first thread would: take the is_cached == false path, generating expr_0_0 into its IR module, and also see a hit on the second read and addObjectFile() a cached object that defines expr_0_0 as well. Both then call JITDylib::define for the same symbol in the same JITDylib, and ORC's duplicate-symbol detection fires: CodeGenError in Gandiva: Failed to add IR module to LLJIT: In gdv_module_..., duplicate definition of symbol 'expr_0_0' C++, Gandiva Thread the single cache-lookup result (prev_cached_obj) that already determined is_cached directly into SetLLVMObjectCache(), instead of letting it perform an independent second lookup. Engine::SetLLVMObjectCache and LLVMGenerator::SetLLVMObjectCache now take the resolved shared_ptr<llvm::MemoryBuffer> rather than a GandivaObjectCache&. No new locking — this removes the window rather than serializing around it. New cpp/src/gandiva/tests/concurrent_make_test.cc. Verified tests fail with the duplicate-symbol error without this fix. Verified internally with our product stress tests after removing the arrow-java side of the fix. Once this change is in C++ I can make the java change to remove the synchronization.
dmitry-chirkov-dremio
suggested changes
Oct 8, 2026
dmitry-chirkov-dremio
left a comment
Contributor
There was a problem hiding this comment.
Change looks good and I’ll approve once my test-related comments are addressed.
dmitry-chirkov-dremio
approved these changes
Oct 8, 2026
dmitry-chirkov-dremio
left a comment
Contributor
There was a problem hiding this comment.
LGTM
Nit: GH-601 reference is from arrow-java - not sure what are the policies on cross-repo references
dmitry-chirkov-dremio
approved these changes
Oct 8, 2026
Contributor
|
@kou this is good to go. |
Member
|
Could you update the PR description before we merge this? |
Contributor
Author
Sorry, what update? If it's about the GH issues, the link in the description goes to arrow-java correctly. Although at this moment apache/arrow-java#601 gives an error for some reason, though its the right URL. |
Member
|
It seems that the latest changes aren't reflected to the description. I've updated the description. I'll merge this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Rationale for this change
I originally identified this issue in arrow-java and fixed it on the arrow-java side with a coarse grained lock on the Projector and Filter Make methods. That fixed the issue but adds some undesirable overhead in certain multi threaded workflows.
This issue captures the deeper problem that is occurring in the C++ side of Gandiva and was not touched by my previous fix though it is prevented from happening. I have a proposed solution for this and believe it is a better overall solution.
Projector::Make()andFilter::Make()read the shared expression cache once to decide theis_cachedstatus and then callSetLLVMObjectCache(). That performs its own second, unsynchronized read of the same key before pre-loading a cached object into the LLJIT.Those two reads could disagree. If another thread compiling the identical (schema, expressions, selection vector mode, configuration) tuple inserted between them, the first thread would:
is_cached == falsepath, generatingexpr_0_0into its IR module, andaddObjectFile()a cached object that definesexpr_0_0as well.Both then call
JITDylib::definefor the same symbol in the sameJITDylib, and ORC's duplicate-symbol detection fires:What changes are included in this PR?
Thread the single cache-lookup result (
prev_cached_obj) that already determinedis_cacheddirectly intoSetLLVMObjectCache(), instead of letting it perform an independent second lookup.Engine::SetLLVMObjectCache()andLLVMGenerator::SetLLVMObjectCache()now take the resolvedstd::shared_ptr<llvm::MemoryBuffer>rather than aGandivaObjectCache&.Engine::SetLLVMObjectCache()passes a copy of the buffer toaddObjectFile(), as the oldGandivaObjectCache::getObject()did, because the shared cache still owns the original buffer.No new locking — this removes the window rather than serializing around it.
Are these changes tested?
This adds a new deterministic regression test,
cpp/src/gandiva/tests/cache_race_test.cc, which is part ofgandiva-projector-test.The test doesn't depend on thread timing. It registers an
ExportedFuncsBasehook that inserts an entry for the key being built into the shared cache duringEngine::Init(). That is afterMake()'s first cache lookup and before the (pre-fix) second lookup inSetLLVMObjectCache(), so the race window is reproduced on every run. It covers bothProjector::Make()andFilter::Make(), and checks that the built projector/filter evaluates correctly.Verified the tests fail with the duplicate-symbol error without this fix.
Verified internally with our product stress tests after removing the arrow-java side of the fix. Once this change is in C++ I can make the java change to remove the synchronization.
Are there any user-facing changes?
This PR includes breaking changes to public APIs.
Engine::SetLLVMObjectCache and LLVMGenerator::SetLLVMObjectCache are GANDIVA_EXPORT so this changes an exported signature. In-tree there are no other callers, and the Gandiva Java side JNI only uses Projector::Make/Filter::Make, which are unchanged.
Was AI used for this PR?
AI was used to diagnose the problem, write code and tests over several iterations before being reviewed by two human developers. A human ran several manual and regression tests against the change. One human reviewed and heavily edited the PR description and also wrote some himself.
PR code and description written by:
Reviewed before submission by: