Skip to content

Commit a88432b

Browse files
committed
Cache: fixed deadlock when exception is thrown in fallback [Closes #36]
1 parent aef120d commit a88432b

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"nette/utils": "~2.2"
2020
},
2121
"require-dev": {
22-
"nette/tester": "~1.0",
22+
"nette/tester": "~1.6",
2323
"latte/latte": "~2.3.0"
2424
},
2525
"conflict": {

src/Caching/Cache.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ public function save($key, $data, array $dependencies = NULL)
130130

131131
if ($data instanceof Nette\Callback || $data instanceof \Closure) {
132132
$this->storage->lock($key);
133-
$data = call_user_func_array($data, array(& $dependencies));
133+
try {
134+
$data = call_user_func_array($data, array(& $dependencies));
135+
} catch (\Exception $e) {
136+
$this->storage->remove($key);
137+
throw $e;
138+
}
134139
}
135140

136141
if ($data === NULL) {
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Caching\Cache dead lock & exception test.
5+
*/
6+
7+
use Nette\Caching\Cache;
8+
use Nette\Caching\Storages\FileStorage;
9+
use Tester\Assert;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
$storage = new FileStorage(TEMP_DIR);
16+
$cache = new Cache($storage);
17+
18+
try {
19+
$cache->load('key', function () {
20+
throw new Exception;
21+
});
22+
} catch (Exception $e) {
23+
}
24+
25+
Assert::noError(function () use ($cache) {
26+
$cache->load('key', function () {});
27+
});

0 commit comments

Comments
 (0)