Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:

- name: Remove OpenTelemetry dependencies on unsupported PHP versions
if: ${{ matrix.php.version == '7.2' || matrix.php.version == '7.3' || matrix.php.version == '7.4' || matrix.php.version == '8.0' }}
run: composer remove open-telemetry/api open-telemetry/exporter-otlp open-telemetry/sdk --dev --no-interaction --no-update
run: composer remove open-telemetry/api open-telemetry/exporter-otlp open-telemetry/sem-conv open-telemetry/sdk --dev --no-interaction --no-update

- name: Set phpunit/phpunit version constraint
run: composer require phpunit/phpunit:'${{ matrix.php.phpunit }}' --dev --no-interaction --no-update
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"nyholm/psr7": "^1.8",
"open-telemetry/api": "^1.0",
"open-telemetry/exporter-otlp": "^1.0",
"open-telemetry/sem-conv": "^1.27",
"open-telemetry/sdk": "^1.0",
"phpstan/phpstan": "^1.3",
"phpunit/phpunit": "^8.5.52|^9.6.34",
Expand Down
19 changes: 19 additions & 0 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ final class ErrorHandler
*/
private static $reservedMemory;

/**
* @var int The amount of memory to reserve for the fatal error handler
*/
private static $reservedMemorySize = self::DEFAULT_RESERVED_MEMORY_SIZE;

/**
* @var bool Whether the fatal error handler should be disabled
*/
Expand Down Expand Up @@ -214,6 +219,7 @@ public static function registerOnceFatalErrorHandler(int $reservedMemorySize = s
}

self::$handlerInstance->isFatalErrorHandlerRegistered = true;
self::$reservedMemorySize = $reservedMemorySize;
self::$reservedMemory = str_repeat('x', $reservedMemorySize);

register_shutdown_function(\Closure::fromCallable([self::$handlerInstance, 'handleFatalError']));
Expand Down Expand Up @@ -301,6 +307,19 @@ public function setMemoryLimitIncreaseOnOutOfMemoryErrorInBytes(?int $valueInByt
$this->memoryLimitIncreaseOnOutOfMemoryErrorValue = $valueInBytes;
}

/**
* @internal
*/
public static function resetFatalErrorHandlerState(): void
{
self::$disableFatalErrorHandler = false;
self::$didIncreaseMemoryLimit = false;

if (self::$handlerInstance !== null && self::$handlerInstance->isFatalErrorHandlerRegistered) {
self::$reservedMemory = str_repeat('x', self::$reservedMemorySize);
}
}
Comment thread
sentry[bot] marked this conversation as resolved.

/**
* Handles errors by capturing them through the client according to the
* configured bit field.
Expand Down
3 changes: 3 additions & 0 deletions src/State/RuntimeContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Sentry\ErrorHandler;
use Sentry\Tracing\PropagationContext;

/**
Expand Down Expand Up @@ -113,6 +114,8 @@ public function startContext(): void
return;
}

ErrorHandler::resetFatalErrorHandlerState();

$this->createContextForExecutionContextKey($executionContextKey);
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Fixtures/OpenTelemetry/TestDiscoveryStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sentry\Tests\Fixtures\OpenTelemetry;

use GuzzleHttp\Psr7\HttpFactory;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
Expand All @@ -21,7 +22,10 @@ public static function getCandidates(string $type): array
}

if (is_a(RequestFactoryInterface::class, $type, true) || is_a(StreamFactoryInterface::class, $type, true)) {
return [['class' => Psr17Factory::class, 'condition' => Psr17Factory::class]];
return [
['class' => HttpFactory::class, 'condition' => HttpFactory::class],
['class' => Psr17Factory::class, 'condition' => Psr17Factory::class],
];
}

return [];
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/OTLPIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ private function useCapturingHttpClient(): void

if (method_exists(HttpClientDiscovery::class, 'setDiscoverers')) {
HttpClientDiscovery::setDiscoverers([new TestClientDiscoverer()]);
} else {
ClassDiscovery::prependStrategy(TestDiscoveryStrategy::class);
}

ClassDiscovery::prependStrategy(TestDiscoveryStrategy::class);

StubOtelHttpClient::reset();
}

Expand Down
56 changes: 56 additions & 0 deletions tests/phpt/error_handler_reset_fatal_error_handler_state.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
Test that resetting the fatal error handler state re-arms OOM handling
--FILE--
<?php

declare(strict_types=1);

namespace Sentry\Tests;

use Sentry\ErrorHandler;

$vendor = __DIR__;

while (!file_exists($vendor . '/vendor')) {
$vendor = \dirname($vendor);
}

require $vendor . '/vendor/autoload.php';

error_reporting(\E_ALL & ~\E_DEPRECATED & ~\E_USER_DEPRECATED);

function getErrorHandlerProperty(string $name): \ReflectionProperty
{
$property = new \ReflectionProperty(ErrorHandler::class, $name);
$property->setAccessible(true);

return $property;
}

function setErrorHandlerStaticProperty(string $name, $value): void
{
getErrorHandlerProperty($name)->setValue(null, $value);
}

function getErrorHandlerStaticProperty(string $name)
{
return getErrorHandlerProperty($name)->getValue();
}

ErrorHandler::registerOnceFatalErrorHandler(1234);

setErrorHandlerStaticProperty('disableFatalErrorHandler', true);
setErrorHandlerStaticProperty('didIncreaseMemoryLimit', true);
setErrorHandlerStaticProperty('reservedMemory', null);

ErrorHandler::resetFatalErrorHandlerState();

var_dump(getErrorHandlerStaticProperty('disableFatalErrorHandler'));
var_dump(getErrorHandlerStaticProperty('didIncreaseMemoryLimit'));
var_dump(\strlen(getErrorHandlerStaticProperty('reservedMemory')));

?>
--EXPECT--
bool(false)
bool(false)
int(1234)
Loading