Skip to content

Commit

Permalink
Merge pull request #1151: fix boot resource leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Sep 30, 2024
2 parents 9aea4d8 + a9b7c3f commit f98452e
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/Boot/src/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,37 @@ public function __construct(
}

/**
* @param string $filename Cache filename.
* @param non-empty-string|null $filename Cache filename.
*/
public function loadData(string $section, string &$filename = null): mixed
public function loadData(string $section, ?string &$filename = null): mixed
{
$filename = $this->getFilename($section);

if (!\file_exists($filename)) {
return null;
}

$fp = false;
$lock = false;

try {
$fp = \fopen($filename, 'r');
if (!\flock($fp, \LOCK_SH | \LOCK_NB)) {
if ($fp === false) {
return null;
}

$lock = \flock($fp, \LOCK_SH | \LOCK_NB);

if ($lock === false) {
return null;
}
$data = include($filename);
\flock($fp, \LOCK_UN);
\fclose($fp);
return $data;

return include($filename);
} catch (\Throwable) {
return null;
} finally {
$lock === false or \flock($fp, \LOCK_UN);
$fp === false or \fclose($fp);
}
}

Expand Down

0 comments on commit f98452e

Please sign in to comment.