René's URL Explorer Experiment


Title: fix: serialise native handle lifetime against concurrent close by andygrove · Pull Request #115 · apache/datafusion-java · GitHub

Open Graph Title: fix: serialise native handle lifetime against concurrent close by andygrove · Pull Request #115 · apache/datafusion-java

X Title: fix: serialise native handle lifetime against concurrent close by andygrove · Pull Request #115 · apache/datafusion-java

Description: Which issue does this PR close? Closes #40. Rationale for this change SessionContext and DataFrame each held their native pointer in a plain long nativeHandle, and every public method followed the pattern if (nativeHandle == 0) throw new IllegalStateException(...); someNativeCall(nativeHandle, ...); The check and the use are separate operations on a non-volatile field, so a close() on another thread can free the native Box in between: thread A reads a live handle and enters JNI, thread B zeroes the field and Rust drops the Box, and thread A dereferences freed memory. The == 0 guard is a TOCTOU, not a fix. The docs said "not thread-safe", which made this defensible but not comfortable — the first multi-threaded user (a server, a Flink or Spark integration) would have hit it in production rather than in dev. It is also a shape that recurs every time a new long-lived handle is added. Reading the native side showed the problem is entirely Java-side. Every non-consuming JNI entry point already takes a shared reference — &*(handle as *const SessionContext) or &*(handle as *const DataFrame)}.clone() — and DataFusion's SessionContext and DataFrame are both Send + Sync. Exactly four entry points take ownership and free: collectDataFrame, executeStreamDataFrame, closeDataFrame, closeSessionContext. So only the handle's lifetime needed serialising; the operations themselves are already safe to overlap, and no Rust change is required. What changes are included in this PR? A new package-private NativeHandle owns the raw pointer and is the only thing that reads or writes it: acquire() / release() pin the handle for the duration of one native call. claim() / claimQuietly() take exclusive ownership, block until in-flight pins drain, and surrender the pointer for freeing or consumption. Every non-consuming method became acquire() / try / finally release(). DataFrame.collect and executeStream use claim(); both close() methods use claimQuietly(). Two properties drove the design: acquire() never blocks — it pins immediately or throws. That is what lets the eight set operations and join / joinOn pin two DataFrames at once without a lock-ordering hazard. A ReentrantReadWriteLock (suggested on the issue) would not: its readers queue behind a waiting writer, so a.union(b) and b.union(a) on two threads with close()s interleaved could deadlock. claim() is the only blocking operation, and it always makes progress — a thread holding a pin is inside a native call and never claims, so the pins it waits on are guaranteed to be released. The monitor is held only for bookkeeping, never across a JNI call, so independent operations on the same object still run concurrently. The Rust-side Arc refcounting listed as option 2 on the issue is not needed given the above, and would have meant touching every JNI entry point. Argument validation stays in its existing position relative to the closed check within each method, so no currently-observable exception changes. Are these changes tested? Yes, three new suites (18 tests): NativeHandleTest — pure JVM, exercises the lifetime state machine in isolation: pin counting, acquire() after claim(), claim() blocking until a latched pin releases, one winner among racing claimers, and interruption during the drain being absorbed with the flag restored. SessionContextConcurrencyTest — close() waiting on an in-flight call (held open by a TableProvider whose schema() parks on a latch), several threads querying while another closes, and concurrent close() idempotence. DataFrameConcurrencyTest — racing collect() resolving to exactly one winner, close() waiting on an in-flight execution (latched scan), concurrent non-consuming reads all succeeding, and opposing union orders across threads not deadlocking. I verified the tests actually detect the bug by temporarily removing the drain loop from NativeHandle: exactly the four drain-dependent tests fail, including both integration-level close() tests, and pass again once restored. The full suite is green (360 tests), as are spotless:check and cargo fmt. No Rust files are touched. Are there any user-facing changes? No API changes, but the documented contract is stronger, and the Javadoc plus README.md, docs/source/user-guide/quickstart.md and docs/source/user-guide/sessioncontext.md are updated accordingly: Both classes are now safe to share between threads. close() blocks until in-flight calls return, then releases the native object; it is a no-op after the first call, including concurrently. A call that loses the race to a close() throws IllegalStateException instead of producing a use-after-free. The docs are explicit that this covers the handle's lifetime and not the ordering of overlapping operations — registering a table concurrently with a query that reads it still races in the ordinary way.

Open Graph Description: Which issue does this PR close? Closes #40. Rationale for this change SessionContext and DataFrame each held their native pointer in a plain long nativeHandle, and every public method followed th...

X Description: Which issue does this PR close? Closes #40. Rationale for this change SessionContext and DataFrame each held their native pointer in a plain long nativeHandle, and every public method followed th...

Opengraph URL: https://github.com/apache/datafusion-java/pull/115

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.112.3

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:f58426d5-621e-27fa-b8ff-c9093e2a290b
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id9B8A:15EB96:233C10:30DEB6:6B04682C
html-safe-noncef45328e7f2c9dea9eb18999c84f284d2d679cfead2e8b0db269cfedc05dbe774
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjhBOjE1RUI5NjoyMzNDMTA6MzBERUI2OjZCMDQ2ODJDIiwidmlzaXRvcl9pZCI6IjQ4ODc2NDg3MzI4MjY1OTMzMjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacfcedf8b3a8808653215a63dd500ec5024c8743da68eba76ffd5a11bdd79b399c
hovercard-subject-tagpull_request:4221957347
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/apache/datafusion-java/pull/115/files
twitter:imagehttps://avatars.githubusercontent.com/u/934084?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/934084?s=400&v=4
og:image:altWhich issue does this PR close? Closes #40. Rationale for this change SessionContext and DataFrame each held their native pointer in a plain long nativeHandle, and every public method followed th...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/apache/datafusion-java git https://github.com/apache/datafusion-java.git
octolytics-dimension-user_id47359
octolytics-dimension-user_loginapache
octolytics-dimension-repository_id1237105117
octolytics-dimension-repository_nwoapache/datafusion-java
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1237105117
octolytics-dimension-repository_network_root_nwoapache/datafusion-java
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseca4d07a7cef4e4b75f821dc25e7300d80c1450a9
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/apache/datafusion-java/pull/115/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fapache%2Fdatafusion-java%2Fpull%2F115%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fapache%2Fdatafusion-java%2Fpull%2F115%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=apache%2Fdatafusion-java
Reloadhttps://github.com/apache/datafusion-java/pull/115/files
Reloadhttps://github.com/apache/datafusion-java/pull/115/files
Reloadhttps://github.com/apache/datafusion-java/pull/115/files
apachehttps://github.com/apache
datafusion-javahttps://github.com/apache/datafusion-java
Codehttps://github.com/apache/datafusion-java
Issues10 (10)https://github.com/apache/datafusion-java/issues
Pull requests10 (10)https://github.com/apache/datafusion-java/pulls
Discussionshttps://github.com/apache/datafusion-java/discussions
Actionshttps://github.com/apache/datafusion-java/actions
Security and qualityhttps://github.com/apache/datafusion-java/security
Insightshttps://github.com/apache/datafusion-java/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fapache%2Fdatafusion-java%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fapache%2Fdatafusion-java%2Fissues%2Fnew%2Fchoose
andygrovehttps://github.com/andygrove
apache:mainhttps://github.com/apache/datafusion-java/tree/main
andygrove:fix/handle-close-racehttps://github.com/andygrove/datafusion-java/tree/fix/handle-close-race
Conversation 0 https://github.com/apache/datafusion-java/pull/115
Commits 1 https://github.com/apache/datafusion-java/pull/115/commits
Checks 7 https://github.com/apache/datafusion-java/pull/115/checks
Files changed https://github.com/apache/datafusion-java/pull/115/files
Please reload this pagehttps://github.com/apache/datafusion-java/pull/115/files
fix: serialise native handle lifetime against concurrent close https://github.com/apache/datafusion-java/pull/115/files#top
Show all changes 1 commit https://github.com/apache/datafusion-java/pull/115/files
f19a1fb fix: serialise native handle lifetime against concurrent close andygrove Aug 6, 2026 https://github.com/apache/datafusion-java/pull/115/commits/f19a1fbfa27800ca5fdff097282594eb42d9af6c
Clear filters https://github.com/apache/datafusion-java/pull/115/files
Please reload this pagehttps://github.com/apache/datafusion-java/pull/115/files
Please reload this pagehttps://github.com/apache/datafusion-java/pull/115/files
README.md https://github.com/apache/datafusion-java/pull/115/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5
DataFrame.java https://github.com/apache/datafusion-java/pull/115/files#diff-a044f0300400b70bd8ec9c4e4a27c17b89674595e0ae3f5a6ebf88e393ba479b
NativeHandle.java https://github.com/apache/datafusion-java/pull/115/files#diff-7077e65978115b38c77aa44b4781c8a88685e26c760b9f8b2b630edb3a355ee3
SessionContext.java https://github.com/apache/datafusion-java/pull/115/files#diff-9aa9338a7364e93d5e2e4547564e7eb08e65a70f6d51b6fa896325cd4d87451e
DataFrameConcurrencyTest.java https://github.com/apache/datafusion-java/pull/115/files#diff-9f986a11bd63d1b7124743772219a5b8b7f40a16f557a16a7bf18831343d62ac
NativeHandleTest.java https://github.com/apache/datafusion-java/pull/115/files#diff-486aa827108aea6dfc2c97ce4ead910061ae6f9c4d3cd2570275f0fc72130cc8
SessionContextConcurrencyTest.java https://github.com/apache/datafusion-java/pull/115/files#diff-f92d3f32c3348598a10898ae80f3f9fb7fbe9b167abbd7397919c89243b11b61
quickstart.md https://github.com/apache/datafusion-java/pull/115/files#diff-eb4fd9a5418a622440444fca4307085e419ebb732ab260841ce6813cf46e3360
sessioncontext.md https://github.com/apache/datafusion-java/pull/115/files#diff-3bce4be80b83ba41ff0e8ca04cc6f97e2f1027978802a157d8391bea68437543
README.mdhttps://github.com/apache/datafusion-java/pull/115/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5
View file https://github.com/apache/datafusion-java/blob/f19a1fbfa27800ca5fdff097282594eb42d9af6c/README.md
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/apache/datafusion-java/pull/115/{{ revealButtonHref }}
https://github.com/apache/datafusion-java/pull/115/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5
https://github.com/apache/datafusion-java/pull/115/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5
Please reload this pagehttps://github.com/apache/datafusion-java/pull/115/files
Please reload this pagehttps://github.com/apache/datafusion-java/pull/115/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlno-cache

Viewport: width=device-width


URLs of crawlers that visited me.