Title: bug: SessionContext.close() / DataFrame.close() race with concurrent JNI calls (use-after-free) · Issue #40 · apache/datafusion-java · GitHub
Open Graph Title: bug: SessionContext.close() / DataFrame.close() race with concurrent JNI calls (use-after-free) · Issue #40 · apache/datafusion-java
X Title: bug: SessionContext.close() / DataFrame.close() race with concurrent JNI calls (use-after-free) · Issue #40 · apache/datafusion-java
Description: Describe the bug SessionContext and DataFrame hold their native pointer in a plain long nativeHandle. Every public method follows the pattern: if (nativeHandle == 0) throw new IllegalStateException(...); someNativeCall(nativeHandle, ...)...
Open Graph Description: Describe the bug SessionContext and DataFrame hold their native pointer in a plain long nativeHandle. Every public method follows the pattern: if (nativeHandle == 0) throw new IllegalStateException...
X Description: Describe the bug SessionContext and DataFrame hold their native pointer in a plain long nativeHandle. Every public method follows the pattern: if (nativeHandle == 0) throw new IllegalStateException...
Opengraph URL: https://github.com/apache/datafusion-java/issues/40
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"bug: SessionContext.close() / DataFrame.close() race with concurrent JNI calls (use-after-free)","articleBody":"### Describe the bug\n\n`SessionContext` and `DataFrame` hold their native pointer in a plain\n`long nativeHandle`. Every public method follows the pattern:\n\n```java\nif (nativeHandle == 0) throw new IllegalStateException(...);\nsomeNativeCall(nativeHandle, ...);\n```\n\n…and `close()` does:\n\n```java\nif (nativeHandle != 0) {\n closeSessionContext(nativeHandle);\n nativeHandle = 0;\n}\n```\n\nIf thread A is mid-method on a context and thread B calls `close()` on\nthe same context, the read in A and the write+free in B race:\n\n1. A reads `nativeHandle` (non-zero), passes it to JNI.\n2. B sets `nativeHandle = 0` and the Rust side drops the `Box`.\n3. A's JNI call dereferences a freed `*const SessionContext` → UAF.\n\nEven the `nativeHandle == 0` guard is not safe — it's a TOCTOU. The\nsame shape applies to `DataFrame` (each method reads `nativeHandle`,\nthen calls JNI).\n\n### To Reproduce\n\nNo reproducer exists yet. A two-thread test running a tight loop of\n`ctx.sql(...).count()` against `ctx.close()` on an ASan-instrumented\nnative build would surface it deterministically; can write one if the\nmaintainers want to see it first.\n\n### Expected behavior\n\nOne of:\n\n1. **Document the UB explicitly.** Today the Javadoc says contexts are\n \"not thread-safe\" and warns about concurrent `sql` / `register*` /\n `close`, but the consequence (\"can produce a use-after-free\") is\n already spelled out — so it's arguably already expected, and this\n issue is just a tracking marker so a future maintainer doesn't get\n surprised.\n2. **Atomic handle + reference count.** Use `AtomicLong` for the\n handle and reference-count on the Rust side so close() defers until\n in-flight calls drain. Closer to what JNA-style bindings do.\n3. **Per-instance lock.** Wrap every JNI call in a synchronized\n block. Simplest, but kills any potential concurrency on independent\n read-only operations.\n\n### Additional context\n\nNot a regression — the documented contract already excludes concurrent\nuse. Filing for visibility ahead of the first multi-threaded user (a\nserver, a Flink/Spark integration, etc.) hitting it in production\nrather than dev. Cross-references `SessionContext.java` and\n`DataFrame.java`; same shape will need attention any time a new\nlong-lived handle is added.","author":{"url":"https://github.com/andygrove","@type":"Person","name":"andygrove"},"datePublished":"2026-05-13T22:06:30Z","url":"https://github.com/apache/datafusion-java/issues/40"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:71116c2a-8dfd-13a5-a68d-7135b231eea2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 843A:29CA94:202084:2CC7A3:6B0467F4 |
| html-safe-nonce | 5aa315e4351e8013fd36052ee103afbe09af8d4d0c40952b6dd2ce133edbd2c9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDNBOjI5Q0E5NDoyMDIwODQ6MkNDN0EzOjZCMDQ2N0Y0IiwidmlzaXRvcl9pZCI6IjY1OTcxOTgwNjc3MTQ1MTY5ODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 350d764069cb8b1fc8776261f4c265a11970e12c5938d1ac57f66e8a574d8501 |
| hovercard-subject-tag | issue:4441504371 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/apache/datafusion-java/40/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ecec0cc68add52231674847875b173cf1e6ed507fd251aa22779c85c8d4b2f0e/apache/datafusion-java/issues/40 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ecec0cc68add52231674847875b173cf1e6ed507fd251aa22779c85c8d4b2f0e/apache/datafusion-java/issues/40 |
| og:image:alt | Describe the bug SessionContext and DataFrame hold their native pointer in a plain long nativeHandle. Every public method follows the pattern: if (nativeHandle == 0) throw new IllegalStateException... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | andygrove |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| turbo-cache-control | no-preview |
| go-import | github.com/apache/datafusion-java git https://github.com/apache/datafusion-java.git |
| octolytics-dimension-user_id | 47359 |
| octolytics-dimension-user_login | apache |
| octolytics-dimension-repository_id | 1237105117 |
| octolytics-dimension-repository_nwo | apache/datafusion-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1237105117 |
| octolytics-dimension-repository_network_root_nwo | apache/datafusion-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width