Skip to content

Commit 340f1a9

Browse files
committed
Fix removing temp IPC sock
Fixes #183.
1 parent 223c47c commit 340f1a9

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/Ipc/LocalIpcHub.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Amp\ForbidSerialization;
88
use Amp\Socket;
99
use Amp\Socket\ResourceSocket;
10+
use Revolt\EventLoop;
1011
use const Amp\Process\IS_WINDOWS;
1112

1213
final class LocalIpcHub implements IpcHub
@@ -40,6 +41,12 @@ public function __construct(
4041
$this->delegate = new SocketIpcHub(Socket\listen($address), $keyReceiveTimeout, $keyLength);
4142
}
4243

44+
public function __destruct()
45+
{
46+
EventLoop::queue($this->delegate->close(...));
47+
$this->unlink();
48+
}
49+
4350
public function accept(string $key, ?Cancellation $cancellation = null): ResourceSocket
4451
{
4552
return $this->delegate->accept($key, $cancellation);
@@ -53,22 +60,30 @@ public function isClosed(): bool
5360
public function close(): void
5461
{
5562
$this->delegate->close();
56-
if ($this->toUnlink !== null) {
57-
// Ignore errors when unlinking temp socket.
58-
\set_error_handler(static fn () => true);
59-
try {
60-
\unlink($this->toUnlink);
61-
} finally {
62-
\restore_error_handler();
63-
}
64-
}
63+
$this->unlink();
6564
}
6665

6766
public function onClose(\Closure $onClose): void
6867
{
6968
$this->delegate->onClose($onClose);
7069
}
7170

71+
private function unlink(): void
72+
{
73+
if ($this->toUnlink === null) {
74+
return;
75+
}
76+
77+
// Ignore errors when unlinking temp socket.
78+
\set_error_handler(static fn () => true);
79+
try {
80+
\unlink($this->toUnlink);
81+
} finally {
82+
\restore_error_handler();
83+
$this->toUnlink = null;
84+
}
85+
}
86+
7287
public function getUri(): string
7388
{
7489
return $this->delegate->getUri();

0 commit comments

Comments
 (0)