Skip to content

Guard rethrowIfBlockingException call against unexpected failures - #12240

Open
dougqh wants to merge 4 commits into
masterfrom
dougqh/fix-exception-handler-blocking-exception-escape
Open

Guard rethrowIfBlockingException call against unexpected failures#12240
dougqh wants to merge 4 commits into
masterfrom
dougqh/fix-exception-handler-blocking-exception-escape

Conversation

@dougqh

@dougqh dougqh commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

Moves the BlockingExceptionHandler.rethrowIfBlockingException(t) call generated by ExceptionHandlers inside the existing try/catch that guards the rest of the instrumentation exception handler, and has the catch handler instanceof-check the caught Throwable before rethrowing — only a genuine BlockingException propagates; everything else is swallowed like any other instrumentation error.

Motivation

rethrowIfBlockingException (added in #7516 so BlockingException could escape the swallow-and-log) was previously called outside the try/catch. That meant any Throwable from that call — not just a genuine BlockingException — would escape uncaught, replacing the original swallowed exception and potentially crashing the instrumented method's caller (e.g. a NoClassDefFoundError from a classloader that can't see the appsec module).

Found while investigating a top-volume error-tracking issue whose "Failed to handle exception in instrumentation for ..." log samples traced back to this handler.

Additional Notes

None.

Test plan

  • :dd-java-agent:agent-tooling:forkedTestExceptionHandlerForkedTest, ExceptionHandlerExitOnFailureForkedTest, AppSecDisabledExceptionHandlerForkedTest (covers the "blocking exception is rethrown" case) all pass
  • ./gradlew spotlessApply — no additional changes

🤖 Generated with Claude Code

BlockingExceptionHandler.rethrowIfBlockingException was invoked outside
the try/catch that guards the rest of the instrumentation exception
handler, so any unexpected Throwable from that call itself (e.g. a
NoClassDefFoundError from a classloader that can't see the appsec
module) would escape uncaught, replacing the original swallowed
exception and crashing the instrumented method's caller instead of
being logged. Move the call inside the try/catch and only rethrow when
the caught exception is actually a BlockingException.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@datadog-official

datadog-official Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
Patch Coverage: 0.00%
Overall Coverage: 62.66% (+3.99%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 2261d3f | Docs | View more details | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Aug 19, 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.00 s 13.93 s [-0.2%; +1.2%] (no difference)
startup:insecure-bank:tracing:Agent 12.92 s 12.97 s [-1.4%; +0.5%] (no difference)
startup:petclinic:appsec:Agent 17.63 s 17.38 s [+0.4%; +2.4%] (maybe worse)
startup:petclinic:iast:Agent 17.59 s 17.60 s [-0.7%; +0.6%] (no difference)
startup:petclinic:profiling:Agent 17.56 s 17.34 s [+0.1%; +2.4%] (maybe worse)
startup:petclinic:sca:Agent 17.54 s 17.41 s [-0.3%; +1.8%] (no difference)
startup:petclinic:tracing:Agent 16.65 s 16.63 s [-1.1%; +1.3%] (no difference)

Commit: 2261d3f8 · 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.

@dougqh
dougqh marked this pull request as ready for review August 21, 2026 13:29
@dougqh
dougqh requested a review from a team as a code owner August 21, 2026 13:29
@dougqh
dougqh requested review from mtoffl01 and removed request for a team August 21, 2026 13:29
@dd-octo-sts

dd-octo-sts Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Hi! 👋 Thanks for your pull request! 🎉

To help us review it, please make sure to:

  • Add at least one type, and one component or instrumentation label to the pull request

If you need help, please check our contributing guidelines.

@dd-octo-sts dd-octo-sts Bot added the tag: ai generated Largely based on code generated by an AI or LLM label Aug 21, 2026

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 209973c00a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@dougqh
dougqh marked this pull request as draft August 21, 2026 14:09

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Datadog Autotest: FAIL

The new catch handler loads BlockingException after it catches a linkage failure. An isolated class loader can throw another NoClassDefFoundError from this check, so the instrumentation error can still reach application code.

Open Bits AI session

🤖 Datadog Autotest · Commit 209973c · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

dougqh and others added 3 commits August 21, 2026 10:24
…ception handler

Documents the gap flagged by codex on PR #12240: the eatException catch
block's own `instanceof BlockingException` check resolves the class via
the instrumented class's own classloader, so a classloader that can't
see the appsec module gets a NoClassDefFoundError instead of the
original exception. Left failing intentionally; not pushed until a fix
is decided.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Replace the INSTANCEOF check with a class-name comparison so the
catch handler no longer forces resolution of BlockingException via
the instrumented class's own classloader - the same
NoClassDefFoundError risk the try/catch exists to guard against.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dougqh
dougqh marked this pull request as ready for review August 21, 2026 16:13

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2261d3f847

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

false);
mv.visitMethodInsn(
Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
mv.visitLdcInsn("datadog.appsec.api.blocking.BlockingException");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve blocking for BlockingException subclasses

When advice throws a subclass of the public, non-final BlockingException, BlockingExceptionHandler.rethrowIfBlockingException recognizes it via instanceof and throws the same object, but this exact runtime-name comparison returns false and the subsequent POP swallows it. The instrumented operation then continues instead of enforcing the AppSec block; preserve the helper's subtype semantics without resolving BlockingException through the instrumented classloader, for example by inspecting superclass names.

Useful? React with 👍 / 👎.

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Datadog Autotest: FAIL

The new exact class-name check swallows a BlockingException subclass. The existing helper rethrows this subclass.

Open Bits AI session

🤖 Datadog Autotest · Commit 2261d3f · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

false);
mv.visitMethodInsn(
Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
mv.visitLdcInsn("datadog.appsec.api.blocking.BlockingException");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Preserve BlockingException subclass propagation

An AppSec or RASP block that uses a BlockingException subclass can fail, so the request can continue.

Assertion details
  • Input: Enabled AppSec instrumentation advice throws a subclass of the public, non-final BlockingException class.
  • Expected: The handler must rethrow BlockingException and its subclasses, as rethrowIfBlockingException does with an instanceof check.
  • Actual: The catch handler compares the thrown object's exact class name with BlockingException. It discards a subclass because its class name differs.

Was this helpful? React 👍 or 👎
🤖 Datadog Autotest · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@dougqh dougqh added comp: tooling Build & Tooling type: bug fix Bug fix labels Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: tooling Build & Tooling tag: ai generated Largely based on code generated by an AI or LLM type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant