|
13 | 13 | import datadog.environment.OperatingSystem; |
14 | 14 | import java.io.File; |
15 | 15 | import java.io.IOException; |
| 16 | +import java.net.ServerSocket; |
16 | 17 | import java.nio.charset.StandardCharsets; |
17 | 18 | import java.nio.file.FileSystems; |
18 | 19 | import java.nio.file.Files; |
@@ -319,6 +320,132 @@ void testCombineTracking() throws Exception { |
319 | 320 | assertOOMEvent(); |
320 | 321 | } |
321 | 322 |
|
| 323 | + /** |
| 324 | + * Verifies that the OOME notifier script correctly unsets inherited JVM environment variables. |
| 325 | + * Without the fix, the child JVM spawned by the script would inherit JDK_JAVA_OPTIONS containing |
| 326 | + * JMX port-binding flags, causing a BindException and losing the OOME event. |
| 327 | + * |
| 328 | + * @see <a href="https://github.com/DataDog/dd-trace-java/issues/10766">#10766</a> |
| 329 | + */ |
| 330 | + @Test |
| 331 | + void testOomeTrackingWithInheritedEnvVars() throws Exception { |
| 332 | + int jmxPort = findFreePort(); |
| 333 | + |
| 334 | + Path script = tempDir.resolve("dd_oome_notifier." + getExtension()); |
| 335 | + String onErrorValue = script + " %p"; |
| 336 | + String errorFile = tempDir.resolve("hs_err_pid%p.log").toString(); |
| 337 | + |
| 338 | + String onOOMEArg = |
| 339 | + !Platform.isLinux() |
| 340 | + ? "-XX:OnOutOfMemoryError=" + onErrorValue |
| 341 | + : "-Ddd.crashtracking.debug.autoconfig.enable=true"; |
| 342 | + |
| 343 | + List<String> processArgs = new ArrayList<>(); |
| 344 | + processArgs.add(javaPath()); |
| 345 | + processArgs.add("-javaagent:" + agentShadowJar()); |
| 346 | + processArgs.add("-Xmx96m"); |
| 347 | + processArgs.add("-Xms96m"); |
| 348 | + if (!onOOMEArg.isEmpty()) { |
| 349 | + processArgs.add(onOOMEArg); |
| 350 | + } |
| 351 | + processArgs.add("-XX:ErrorFile=" + errorFile); |
| 352 | + processArgs.add("-XX:+CrashOnOutOfMemoryError"); |
| 353 | + processArgs.add("-Ddd.dogstatsd.start-delay=0"); |
| 354 | + processArgs.add("-Ddd.trace.enabled=false"); |
| 355 | + processArgs.add("-jar"); |
| 356 | + processArgs.add(appShadowJar()); |
| 357 | + |
| 358 | + ProcessBuilder pb = new ProcessBuilder(processArgs); |
| 359 | + pb.environment().put("DD_DOGSTATSD_PORT", String.valueOf(udpServer.getPort())); |
| 360 | + // Simulate admission controller injecting JMX flags via JDK_JAVA_OPTIONS |
| 361 | + pb.environment() |
| 362 | + .put( |
| 363 | + "JDK_JAVA_OPTIONS", |
| 364 | + "-Dcom.sun.management.jmxremote" |
| 365 | + + " -Dcom.sun.management.jmxremote.port=" |
| 366 | + + jmxPort |
| 367 | + + " -Dcom.sun.management.jmxremote.rmi.port=" |
| 368 | + + jmxPort |
| 369 | + + " -Dcom.sun.management.jmxremote.authenticate=false" |
| 370 | + + " -Dcom.sun.management.jmxremote.ssl=false"); |
| 371 | + |
| 372 | + System.out.println("==> Process args: " + pb.command()); |
| 373 | + System.out.println("==> JMX port: " + jmxPort); |
| 374 | + |
| 375 | + Process p = pb.start(); |
| 376 | + OUTPUT.captureOutput( |
| 377 | + p, LOG_FILE_DIR.resolve("testProcess.testOomeTrackingWithInheritedEnvVars.log").toFile()); |
| 378 | + |
| 379 | + assertExpectedCrash(p); |
| 380 | + assertOOMEvent(); |
| 381 | + } |
| 382 | + |
| 383 | + /** |
| 384 | + * Verifies that the crash uploader script correctly unsets inherited JVM environment variables. |
| 385 | + * Without the fix, the child JVM spawned by the script would inherit JDK_JAVA_OPTIONS containing |
| 386 | + * JMX port-binding flags, causing a BindException and losing the crash data. |
| 387 | + * |
| 388 | + * @see <a href="https://github.com/DataDog/dd-trace-java/issues/10766">#10766</a> |
| 389 | + */ |
| 390 | + @Test |
| 391 | + void testCrashTrackingWithInheritedEnvVars() throws Exception { |
| 392 | + int jmxPort = findFreePort(); |
| 393 | + |
| 394 | + Path script = tempDir.resolve("dd_crash_uploader." + getExtension()); |
| 395 | + String onErrorValue = script + " %p"; |
| 396 | + String errorFile = tempDir.resolve("hs_err.log").toString(); |
| 397 | + |
| 398 | + String onErrorArg = |
| 399 | + !Platform.isLinux() |
| 400 | + ? "-XX:OnError=" + onErrorValue |
| 401 | + : "-Ddd.crashtracking.debug.autoconfig.enable=true"; |
| 402 | + |
| 403 | + List<String> processArgs = new ArrayList<>(); |
| 404 | + processArgs.add(javaPath()); |
| 405 | + processArgs.add("-javaagent:" + agentShadowJar()); |
| 406 | + processArgs.add("-Xmx96m"); |
| 407 | + processArgs.add("-Xms96m"); |
| 408 | + if (!onErrorArg.isEmpty()) { |
| 409 | + processArgs.add(onErrorArg); |
| 410 | + } |
| 411 | + processArgs.add("-XX:ErrorFile=" + errorFile); |
| 412 | + processArgs.add("-XX:+CrashOnOutOfMemoryError"); |
| 413 | + processArgs.add("-Ddd.dogstatsd.start-delay=0"); |
| 414 | + processArgs.add("-Ddd.trace.enabled=false"); |
| 415 | + processArgs.add("-jar"); |
| 416 | + processArgs.add(appShadowJar()); |
| 417 | + |
| 418 | + ProcessBuilder pb = new ProcessBuilder(processArgs); |
| 419 | + pb.environment().put("DD_TRACE_AGENT_PORT", String.valueOf(tracingServer.getPort())); |
| 420 | + // Simulate admission controller injecting JMX flags via JDK_JAVA_OPTIONS |
| 421 | + pb.environment() |
| 422 | + .put( |
| 423 | + "JDK_JAVA_OPTIONS", |
| 424 | + "-Dcom.sun.management.jmxremote" |
| 425 | + + " -Dcom.sun.management.jmxremote.port=" |
| 426 | + + jmxPort |
| 427 | + + " -Dcom.sun.management.jmxremote.rmi.port=" |
| 428 | + + jmxPort |
| 429 | + + " -Dcom.sun.management.jmxremote.authenticate=false" |
| 430 | + + " -Dcom.sun.management.jmxremote.ssl=false"); |
| 431 | + |
| 432 | + System.out.println("==> Process args: " + pb.command()); |
| 433 | + System.out.println("==> JMX port: " + jmxPort); |
| 434 | + |
| 435 | + Process p = pb.start(); |
| 436 | + OUTPUT.captureOutput( |
| 437 | + p, LOG_FILE_DIR.resolve("testProcess.testCrashTrackingWithInheritedEnvVars.log").toFile()); |
| 438 | + |
| 439 | + assertExpectedCrash(p); |
| 440 | + assertCrashData(assertCrashPing()); |
| 441 | + } |
| 442 | + |
| 443 | + private static int findFreePort() throws IOException { |
| 444 | + try (ServerSocket socket = new ServerSocket(0)) { |
| 445 | + return socket.getLocalPort(); |
| 446 | + } |
| 447 | + } |
| 448 | + |
322 | 449 | private static void assertExpectedCrash(Process p) throws InterruptedException { |
323 | 450 | // exit code -1 means the test application exited prematurely |
324 | 451 | // exit code > 0 means the test application crashed, as expected |
|
0 commit comments