Skip to content

[Bug]: Exceptions thrown in prehook callbacks are suppressed by trace_method() #3461

@Devdits

Description

@Devdits

Bug report

The trace_method() function suppresses exceptions that are thrown inside a prehook callback.
This leads to inconsistent behavior between environments with and without dd-trace (e.g., local development vs. production).

Problem was discovered in DrupalIntegration.php, an exception thrown from $themeRegistry->getRuntime() inside a traced Drupal\Core\Theme\ThemeManager::render method is silently swallowed when dd-trace is active.

This makes debugging and consistent behavior between environments difficult.

Code to reproduce the problem:

<?php

use DDTrace\SpanData;
use function DDTrace\trace_method;

class testClass {
  private string $message = 'default';

  public function print(): void {
    echo $this->message;
  }

  public function throwException(): void {
    $this->message = 'Exception was thrown';
    throw new RuntimeException('Hello there!');
  }
}

trace_method(
  'testClass',
  'print',
  [
    'prehook' => function (SpanData $span, $args) {
      $test_class = $this;
      assert($test_class instanceof testClass);
      $test_class->throwException();
    },
  ]
);

$test_class = new testClass();
$test_class->print();

PHP version

8.3.21

Tracer or profiler version

1.13.0

Installed extensions

No response

Output of phpinfo()

No response

Upgrading from

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions