Skip to content

Fix TOCTOU race causing NPE in Hibernate closeScope#11955

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit into
masterfrom
andrea.marziali/hibernate
Jul 16, 2026
Merged

Fix TOCTOU race causing NPE in Hibernate closeScope#11955
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit into
masterfrom
andrea.marziali/hibernate

Conversation

@amarziali

Copy link
Copy Markdown
Contributor

What Does This Do

SessionMethodUtils.closeScope read SessionState.methodScope twice (once to check null, once to use) with no lock between the reads. A concurrent call could null it out in between, causing

Error : Failed to handle exception in instrumentation for org.hibernate.internal.CriteriaImpl - datadog.trace.instrumentation.hibernate.core.v4_0.CriteriaInstrumentation$CriteriaMethodAdvice
java.lang.NullPointerException
  at datadog.trace.instrumentation.hibernate.SessionMethodUtils.closeScope(SessionMethodUtils.java:73)
  at (redacted: 29 frames)
  at sun.reflect.GeneratedMethodAccessor5142.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at (redacted: 16 frames)
  at sun.reflect.GeneratedMethodAccessor5141.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at (redacted: 11 frames)
  at sun.reflect.GeneratedMethodAccessor5731.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMet

Motivation

Additional Notes

Contributor Checklist

Jira ticket: [PROJ-IDENT]

@amarziali amarziali requested a review from a team as a code owner July 15, 2026 10:22
@amarziali amarziali requested review from vandonr and removed request for a team July 15, 2026 10:22
@amarziali amarziali added type: bug fix Bug fix inst: others All other instrumentations tag: concurrency Virtual Threads, Coroutines, Async, RX, Executors labels Jul 15, 2026
@amarziali amarziali requested a review from mcculls July 15, 2026 10:23
@amarziali amarziali enabled auto-merge July 15, 2026 10:32
@amarziali amarziali added the tag: telemetry error reported Reported by error telemetry label Jul 15, 2026

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

The single-read fix correctly eliminates the NPE: capturing methodScope once into a local variable makes it immune to concurrent nullification by another thread. The reordering of setMethodScope(null) to before scope.close() is a strict improvement — it prevents a stale scope reference if close() throws and reduces a pre-existing window where same-thread re-entry during span finishing could leak a newly-created scope. Zero NPEs across 1,000 concurrent-nullification iterations.

Was this helpful? React 👍 or 👎

📊 Validated against 6 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit fe6db89 · What is Autotest? · Any feedback? Reach out in #autotest

@datadog-prod-us1-3

Copy link
Copy Markdown

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 69.73% (+12.56%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: fe6db89 | Docs | Datadog PR Page | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.01 s 14.03 s [-0.8%; +0.5%] (no difference)
startup:insecure-bank:tracing:Agent 12.96 s 13.06 s [-1.5%; +0.1%] (no difference)
startup:petclinic:appsec:Agent 16.99 s 16.72 s [+0.5%; +2.8%] (maybe worse)
startup:petclinic:iast:Agent 16.92 s 16.86 s [-0.5%; +1.2%] (no difference)
startup:petclinic:profiling:Agent 16.67 s 16.80 s [-1.9%; +0.4%] (no difference)
startup:petclinic:sca:Agent 16.87 s 16.72 s [-0.4%; +2.2%] (no difference)
startup:petclinic:tracing:Agent 16.04 s 16.01 s [-0.7%; +1.1%] (no difference)

Commit: fe6db895 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@amarziali amarziali added this pull request to the merge queue Jul 15, 2026
@dd-octo-sts

dd-octo-sts Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Jul 15, 2026

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-07-15 10:56:31 UTC ℹ️ Start processing command /merge


2026-07-15 10:56:36 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in master is approximately 2h (p90).


2026-07-15 12:57:21 UTCMergeQueue: The build pipeline has timeout

The merge request has been interrupted because the build 3377249345649551824 took longer than expected. The current limit for the base branch 'master' is 120 minutes.

Possible reasons:

  • some mandatory checkruns are failing:
    • Check pull request labels
    • Check pull requests
    • Enforce Groovy migration

@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 15, 2026
@amarziali

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Jul 16, 2026

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-07-16 10:24:18 UTC ℹ️ Start processing command /merge


2026-07-16 10:24:22 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in master is approximately 2h (p90).


2026-07-16 11:30:07 UTC ℹ️ MergeQueue: This merge request was merged

@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot merged commit 92779f6 into master Jul 16, 2026
591 of 595 checks passed
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot deleted the andrea.marziali/hibernate branch July 16, 2026 11:30
@github-actions github-actions Bot added this to the 1.65.0 milestone Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: others All other instrumentations tag: concurrency Virtual Threads, Coroutines, Async, RX, Executors tag: telemetry error reported Reported by error telemetry type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants