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
4 changes: 2 additions & 2 deletions Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
frankenphp {
max_threads auto
max_requests 100
php_ini memory_limit 512M

worker {
num 2
Expand All @@ -19,7 +20,7 @@
watch core/**/*.php
watch 3rdparty/**/*.php
watch *.php

match /index.php/*
max_consecutive_failures -1
}
Expand Down Expand Up @@ -55,7 +56,6 @@

{$SERVER_NAME:localhost} {
php_server {

}

log {
Expand Down
20 changes: 12 additions & 8 deletions lib/OC.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,14 @@ public static function initForRequest(): void {
self::handleAuthHeaders();

// setup the basic server
self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
/** @psalm-suppress RedundantPropertyInitializationCheck The server is first initialized here */
if (!isset(self::$server)) {
self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
} else {
set_exception_handler(null);
set_error_handler(null);
self::$server->reset();
}
self::$server->boot();

self::oneTimeChecks();
Expand Down Expand Up @@ -825,7 +832,7 @@ public static function initForRequest(): void {
} else {
set_error_handler([$errorHandler, 'onError']);
}
register_shutdown_function([$errorHandler, 'onShutdown']);
self::$server->registerShutdownFunction($errorHandler->onShutdown(...));
set_exception_handler($exceptionHandler);
}

Expand Down Expand Up @@ -924,11 +931,8 @@ public static function initForRequest(): void {
$appManager->loadApp('settings');
}

//make sure temporary files are cleaned up
$tmpManager = Server::get(\OCP\ITempManager::class);
register_shutdown_function([$tmpManager, 'clean']);
$lockProvider = Server::get(\OCP\Lock\ILockingProvider::class);
register_shutdown_function([$lockProvider, 'releaseAll']);
self::$server->registerShutdownFunction($lockProvider->releaseAll(...));

$host = $request->getInsecureServerHost();
/**
Expand Down Expand Up @@ -974,11 +978,11 @@ public static function initForRequest(): void {
$eventLogger->log('init', 'OC::init', $loaderStart, microtime(true));
$eventLogger->start('runtime', 'Runtime');
$eventLogger->start('request', 'Full request after boot');
register_shutdown_function(function () use ($eventLogger) {
self::$server->registerShutdownFunction(static function () use ($eventLogger) {
$eventLogger->end('request');
});

register_shutdown_function(function () use ($config) {
self::$server->registerShutdownFunction(static function () use ($config) {
$memoryPeak = memory_get_peak_usage();
$debugModeEnabled = $config->getSystemValueBool('debug', false);
$memoryLimit = null;
Expand Down
31 changes: 29 additions & 2 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,28 @@
* TODO: hookup all manager classes
*/
class Server extends ServerContainer {
private array $shutdownFunctions = [];

public function __construct(
private string $webRoot,
Config $config,
private Config $config,
) {
parent::__construct();
$this->runInitialRegistrations();

register_shutdown_function($this->shutdown(...));
}

/**
* Empty container and re-run initial registration
*/
public function reset(): void {
$this->shutdown();
parent::__construct();
$this->runInitialRegistrations();
}

public function runInitialRegistrations(): void {
// To find out if we are running from CLI or not
$this->registerParameter('isCLI', \OC::$CLI);
$this->registerParameter('serverRoot', \OC::$SERVERROOT);
Expand Down Expand Up @@ -472,7 +488,7 @@ public function __construct(

$this->registerAlias(IConfig::class, AllConfig::class);

$this->registerService(SystemConfig::class, static fn ($c) => new SystemConfig($config));
$this->registerService(SystemConfig::class, fn ($c) => new SystemConfig($this->config));

$this->registerAlias(IAppConfig::class, \OC\AppConfig::class);
$this->registerAlias(IUserConfig::class, UserConfig::class);
Expand Down Expand Up @@ -1145,4 +1161,15 @@ public function setSession(ISession $session): void {
public function getWebRoot(): string {
return $this->webRoot;
}

public function registerShutdownFunction(callable $function): void {
$this->shutdownFunctions[] = $function;
}

public function shutdown(): void {
foreach ($this->shutdownFunctions as $shutdownFunction) {
$shutdownFunction();
}
$this->shutdownFunctions = [];
}
}
5 changes: 5 additions & 0 deletions lib/private/TempManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function __construct(
$this->tmpBaseDir = $this->getTempBaseDir();
}

public function __destruct() {
$this->clean();
}

private function generateTemporaryPath(string $postFix): string {
$secureRandom = Server::get(ISecureRandom::class);
$absolutePath = $this->tmpBaseDir . '/' . self::TMP_PREFIX . $secureRandom->generate(32, ISecureRandom::CHAR_ALPHANUMERIC);
Expand Down Expand Up @@ -93,6 +97,7 @@ public function getTemporaryFolder($postFix = ''): string|false {
#[\Override]
public function clean() {
$this->cleanFiles($this->current);
$this->current = [];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/TempManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ public function testGenerateTemporaryPathTraversal(): void {
}

public function testGetTempBaseDirFromConfig(): void {
$dir = $this->getManager()->getTemporaryFolder();
$manager1 = $this->getManager();
$dir = $manager1->getTemporaryFolder();

$config = $this->createMock(IConfig::class);
$config->expects($this->once())
Expand Down
Loading