Skip to content

Fix Java HTTP async continuation lifecycle - #12498

Open
amarziali wants to merge 3 commits into
masterfrom
andrea.marziali/diag-fix-java-http-async
Open

amarziali wants to merge 3 commits into
masterfrom
andrea.marziali/diag-fix-java-http-async

Conversation

@amarziali

Copy link
Copy Markdown
Contributor

What Does This Do

Keeps the Java HTTP client context available across all asynchronous response-body callbacks.

The body subscriber previously used a one-shot continuation. The first callback resolved it, so later callbacks could run without the expected context and the trace could be reported before response processing
completed.

The continuation is now held until a terminal event:

  • onComplete
  • onError
  • subscription cancellation

A shared terminal guard ensures concurrent or repeated terminal signals release it exactly once.

sequenceDiagram
    participant Client as HTTP client
    participant Wrapper as BodySubscriber wrapper
    participant Context as Trace continuation
    participant Delegate as Body subscriber

    Client->>Wrapper: onSubscribe(subscription)
    Wrapper->>Delegate: onSubscribe(wrapped subscription)

    loop Response chunks
        Client->>Wrapper: onNext(chunk)
        Wrapper->>Context: resume()
        Wrapper->>Delegate: onNext(chunk)
        Wrapper->>Context: close resumed scope
    end

    alt Response completes
        Client->>Wrapper: onComplete()
        Wrapper->>Context: resume()
        Wrapper->>Delegate: onComplete()
        Wrapper->>Context: release once
    else Response fails
        Client->>Wrapper: onError(error)
        Wrapper->>Context: resume()
        Wrapper->>Delegate: onError(error)
        Wrapper->>Context: release once
    else Subscription is cancelled
        Delegate->>Wrapper: cancel()
        Wrapper->>Client: delegate cancel()
        Wrapper->>Context: release once
    end
Loading

Motivation

Java HTTP response bodies can invoke onNext multiple times. Their tracing context must remain available until body processing actually terminates, including cancellation.

Additional Notes

Contributor Checklist

Jira ticket: [PROJ-IDENT]

@amarziali amarziali added type: bug fix Bug fix inst: java Core Java language instrumentation labels Sep 15, 2026
@amarziali
amarziali requested review from a team as code owners September 15, 2026 10:14
@amarziali amarziali added the tag: ai generated Largely based on code generated by an AI or LLM label Sep 15, 2026
@amarziali
amarziali requested review from jordan-wong and mhdatie and removed request for a team September 15, 2026 10:14
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-15T11:01:04.951589Z 72169ba Manual request
🔒 Security Review Completed 2026-09-15T10:57:05.564390Z 72169ba Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-prod-us1-6 datadog-prod-us1-6 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: FAIL

A custom body subscriber can throw from onSubscribe or onNext. The held continuation then has no release path, so it can keep trace context and stop trace reporting.

Open Bits AI session

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

@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: 257fe750c3

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@datadog-prod-us1-6

datadog-prod-us1-6 Bot commented Sep 15, 2026

Copy link
Copy Markdown

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 68.82% (+9.77%)

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

@dd-octo-sts

dd-octo-sts Bot commented Sep 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.05 s 13.88 s [+0.1%; +2.2%] (maybe worse)
startup:insecure-bank:tracing:Agent 12.91 s 12.97 s [-1.3%; +0.2%] (no difference)
startup:petclinic:appsec:Agent 17.16 s 16.99 s [+0.1%; +1.8%] (maybe worse)
startup:petclinic:iast:Agent 17.06 s 17.06 s [-0.9%; +0.9%] (no difference)
startup:petclinic:profiling:Agent 16.81 s 16.69 s [-0.2%; +1.6%] (no difference)
startup:petclinic:sca:Agent 17.00 s 16.87 s [-0.1%; +1.7%] (no difference)
startup:petclinic:tracing:Agent 16.12 s 16.15 s [-1.2%; +0.8%] (no difference)

Commit: d6630735 · 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

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-prod-us1-6 datadog-prod-us1-6 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 held continuation stays available for each response-body callback. Each terminal path releases it one time.

Was this helpful? React 👍 or 👎

Open Bits AI session

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

@amarziali

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 72169baf12

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 72169baf12

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@jordan-wong jordan-wong 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.

LGTM, left one non-blocking comment about potential test coverage

private int released;

@Override
public ContextContinuation hold() {

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.

This RecordingContinuation test class for ContextContinuation doesn't track the core behavior that was changed with .hold() in this PR - may be worth looking into adding modeling for hold and release behavior in tests?

Claude suggests this for the test class:

 private static final class RecordingContinuation implements ContextContinuation {
    private boolean held;
    private int released;

    @Override
    public ContextContinuation hold() {
      held = true;
      return this;
    }

    @Override
    public Context context() {
      return null;
    }

    @Override
    public ContextScope resume() {
      // Real contract: without hold(), the resumed scope's close() implicitly releases once.
      // With hold(), close() does NOT release — release() must be called explicitly.
      return new ContextScope() {
        private boolean closed;

        @Override
        public Context context() {
          return null;
        }

        @Override
        public void close() {
          if (!closed && !held) {
            closed = true;
            released++;
          }
        }
      };
    }

    @Override
    public void release() {
      released++;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: java Core Java language instrumentation 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.

3 participants