Skip to content

Commit 2ce32d5

Browse files
committed
Return same promise on subsequent close calls
1 parent 20261f7 commit 2ce32d5

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

lib/EioHandle.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,36 @@
1010
use function Amp\call;
1111

1212
class EioHandle implements Handle {
13+
/** @var \Amp\File\Internal\EioPoll */
1314
private $poll;
15+
16+
/** @var resource eio file handle. */
1417
private $fh;
18+
19+
/** @var string */
1520
private $path;
21+
22+
/** @var string */
1623
private $mode;
24+
25+
/** @var int */
1726
private $size;
27+
28+
/** @var int */
1829
private $position;
30+
31+
/** @var \SplQueue */
1932
private $queue;
33+
34+
/** @var bool */
2035
private $isActive = false;
36+
37+
/** @var bool */
2138
private $writable = true;
2239

40+
/** @var \Amp\Promise|null */
41+
private $closing;
42+
2343
public function __construct(Internal\EioPoll $poll, $fh, string $path, string $mode, int $size) {
2444
$this->poll = $poll;
2545
$this->fh = $fh;
@@ -169,8 +189,12 @@ private function onWrite(Deferred $deferred, $result, $req) {
169189
* {@inheritdoc}
170190
*/
171191
public function close(): Promise {
192+
if ($this->closing) {
193+
return $this->closing;
194+
}
195+
172196
$deferred = new Deferred;
173-
$this->poll->listen($deferred->promise());
197+
$this->poll->listen($this->closing = $deferred->promise());
174198

175199
\eio_close($this->fh, \EIO_PRI_DEFAULT, [$this, "onClose"], $deferred);
176200

lib/Internal/FileTask.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function run(Environment $environment) {
112112
case "fclose":
113113
$file->close();
114114
$environment->delete($this->id);
115-
return true;
115+
return;
116116

117117
default:
118118
throw new \Error('Invalid operation');

lib/ParallelHandle.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class ParallelHandle implements Handle {
4040
/** @var bool */
4141
private $writable = true;
4242

43+
/** @var \Amp\Promise|null */
44+
private $closing;
45+
4346
/**
4447
* @param \Amp\Parallel\Worker\Worker $worker
4548
* @param int $id
@@ -73,20 +76,20 @@ public function path(): string {
7376
* {@inheritdoc}
7477
*/
7578
public function close(): Promise {
76-
if (!$this->writable) {
77-
return new Success;
79+
if ($this->closing) {
80+
return $this->closing;
7881
}
7982

8083
$this->writable = false;
8184

8285
if ($this->worker->isRunning()) {
83-
$promise = $this->worker->enqueue(new Internal\FileTask('fclose', [], $this->id));
86+
$this->closing = $this->worker->enqueue(new Internal\FileTask('fclose', [], $this->id));
8487
$this->id = null;
85-
return $promise;
88+
} else {
89+
$this->closing = new Success;
8690
}
8791

88-
// FIXME: Should that really return new Success instead of an exception?
89-
return new Success;
92+
return $this->closing;
9093
}
9194

9295
/**

lib/UvHandle.php

+28-4
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,39 @@
1212
use function Amp\call;
1313

1414
class UvHandle implements Handle {
15+
/** @var UvPoll */
1516
private $poll;
16-
private $driver;
17+
18+
/** @var \UVLoop */
19+
private $loop;
20+
21+
/** @var resource */
1722
private $fh;
23+
24+
/** @var string */
1825
private $path;
26+
27+
/** @var string */
1928
private $mode;
29+
30+
/** @var int */
2031
private $size;
21-
private $loop;
32+
33+
/** @var int */
2234
private $position;
35+
36+
/** @var \SplQueue */
2337
private $queue;
38+
39+
/** @var bool */
2440
private $isActive = false;
41+
42+
/** @var bool */
2543
private $writable = true;
2644

45+
/** @var \Amp\Promise|null */
46+
private $closing;
47+
2748
/**
2849
* @param \Amp\Loop\UvDriver $driver
2950
* @param UvPoll $poll Poll for keeping the loop active.
@@ -33,7 +54,6 @@ class UvHandle implements Handle {
3354
* @param int $size
3455
*/
3556
public function __construct(Loop\UvDriver $driver, UvPoll $poll, $fh, string $path, string $mode, int $size) {
36-
$this->driver = $driver;
3757
$this->poll = $poll;
3858
$this->fh = $fh;
3959
$this->path = $path;
@@ -219,8 +239,12 @@ public function mode(): string {
219239
* {@inheritdoc}
220240
*/
221241
public function close(): Promise {
242+
if ($this->closing) {
243+
return $this->closing;
244+
}
245+
222246
$deferred = new Deferred;
223-
$this->poll->listen($deferred->promise());
247+
$this->poll->listen($this->closing = $deferred->promise());
224248

225249
\uv_fs_close($this->loop, $this->fh, function ($fh) use ($deferred) {
226250
// FIXME: Check for errors

0 commit comments

Comments
 (0)