Skip to content

Commit c8d4e99

Browse files
committed
explicitly release all semaphores
1 parent 50ea2d6 commit c8d4e99

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/Module/Control/GameController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function __construct(
5959
private readonly GameStateInterface $gameState,
6060
private readonly JavascriptExecutionInterface $javascriptExecution,
6161
private readonly SessionStringFactoryInterface $sessionStringFactory,
62+
private readonly SemaphoreUtilInterface $semaphoreUtil,
6263
private readonly BenchmarkResultInterface $benchmarkResult
6364
) {
6465
$this->gameData = new GameData();
@@ -285,6 +286,8 @@ public function main(
285286
$this->fallbackRouter->showFallbackSite($e, $this);
286287
}
287288

289+
$this->semaphoreUtil->releaseAllSemaphores();
290+
288291
$isTemplateSet = $this->twigPage->isTemplateSet();
289292

290293
if (!$isTemplateSet) {

src/Module/Control/SemaphoreUtil.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66
use Stu\Component\Game\SemaphoreConstants;
77
use Stu\Exception\SemaphoreException;
88
use Stu\Module\Config\StuConfigInterface;
9-
use Stu\Module\Logging\LogLevelEnum;
10-
use Stu\Module\Logging\LoggerUtilFactoryInterface;
11-
use Stu\Module\Logging\LoggerUtilInterface;
9+
use Stu\Module\Logging\LogTypeEnum;
10+
use Stu\Module\Logging\StuLogger;
1211
use SysvSemaphore;
1312

1413
final class SemaphoreUtil implements SemaphoreUtilInterface
1514
{
1615
/** @var array<int, SysvSemaphore> */
1716
public static array $semaphores = [];
1817

19-
private LoggerUtilInterface $loggerUtil;
20-
21-
public function __construct(
22-
private readonly StuConfigInterface $stuConfig,
23-
LoggerUtilFactoryInterface $loggerUtilFactory
24-
) {
25-
$this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
26-
}
18+
public function __construct(private readonly StuConfigInterface $stuConfig) {}
2719

2820
#[Override]
2921
public function isSemaphoreAlreadyAcquired(int $key): bool
@@ -83,18 +75,36 @@ public function releaseSemaphore(null|int|SysvSemaphore $semaphore, bool $doRemo
8375
$this->release($semaphore, $doRemove);
8476
}
8577

78+
#[Override]
79+
public function releaseAllSemaphores(bool $doRemove = false): void
80+
{
81+
if (!$this->isSemaphoreUsageActive()) {
82+
return;
83+
}
84+
85+
foreach (self::$semaphores as $semaphore) {
86+
$this->release($semaphore, $doRemove);
87+
}
88+
}
89+
8690
private function release(SysvSemaphore $semaphore, bool $doRemove): void
8791
{
92+
$key = array_search($semaphore, self::$semaphores, true);
93+
if ($key === false) {
94+
return;
95+
}
96+
unset(self::$semaphores[$key]);
97+
8898
if (!sem_release($semaphore)) {
89-
$this->loggerUtil->init('semaphores', LogLevelEnum::ERROR);
90-
$this->loggerUtil->log("Error releasing Semaphore!");
99+
StuLogger::log(sprintf("Error releasing Semaphore with key %d!", $key), LogTypeEnum::SEMAPHORE);
91100
return;
92101
//throw new SemaphoreException("Error releasing Semaphore!");
102+
} else {
103+
StuLogger::log(sprintf(' Released semaphore %d', $key), LogTypeEnum::SEMAPHORE);
93104
}
94105

95106
if ($doRemove && !sem_remove($semaphore)) {
96-
$this->loggerUtil->init('semaphores', LogLevelEnum::ERROR);
97-
$this->loggerUtil->log("Error removing Semaphore!");
107+
StuLogger::log(sprintf("Error removing Semaphore with key %d!", $key), LogTypeEnum::SEMAPHORE);
98108
//throw new SemaphoreException("Error removing Semaphore!");
99109
}
100110
}

src/Module/Control/SemaphoreUtilInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public function isSemaphoreAlreadyAcquired(int $key): bool;
1111
public function acquireSemaphore(int $key): null|int|SysvSemaphore;
1212

1313
public function releaseSemaphore(null|int|SysvSemaphore $semaphore, bool $doRemove = false): void;
14+
15+
public function releaseAllSemaphores(bool $doRemove = false): void;
1416
}

src/Module/Spacecraft/Lib/SpacecraftLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ private function acquireSemaphoreForSpacecraft(Spacecraft|int $spacecraft): ?Spa
247247
}
248248

249249
$key = $spacecraft->getUser()->getId();
250+
StuLogger::log(sprintf(
251+
'spacecraft %d with key %d',
252+
$spacecraft->getId(),
253+
$key
254+
), LogTypeEnum::SEMAPHORE);
250255
$this->semaphoreUtil->acquireSemaphore($key);
251256

252257
return $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft);

0 commit comments

Comments
 (0)