Fix VirtualThread support on multiple unmount/remount cycles by PerfectSlayer · Pull Request #10931 · DataDog/dd-trace-java

What Does This Do

This PR supersedes #10887 to fix context tracking on multiple virtual thread unmount / remount cycles.

Motivation

Previously, only one mount was supported, leading to context break.

Additional Notes

This PR is a re-implementation and adds the following changes compared to the original PR report:

  • Updates the Javadoc to clarify the expected lifecycle and context related operations
  • Distinguishes afterTerminate / afterDone hooks
  • Uses typed ContextScope to reduce unnecessary type casts
  • Avoid unnecessary null checks for null-safe API calls
  • Does not test remount cycle from the tests dedicated to VirtualThread and its builder API but creates a dedicated test suite
  • Does not limit to test the missing behavior but tests other cases too like:
    • multiple unmount/remount cycles
    • implicit (tracing) context restored
    • multiple virtual threads unmount/remount cycles
    • sanity check without context
  • Tries to trigger unmount more reliably - no dedicated API for so we can expect the test to flake
  • Removes the logging correlation concern from the test cases to focus only on context tracking
  • Adds thread join timeout

Contributor Checklist

Jira ticket: [PROJ-IDENT]

Note: Once your PR is ready to merge, add it to the merge queue by commenting /merge. /merge -c cancels the queue request. /merge -f --reason "reason" skips all merge queue checks; please use this judiciously, as some checks do not run at the PR-level. For more information, see this doc.