Skip to content

Commit dbd941a

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

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
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.4",
22+
"nette/tester": "~1.6",
2323
"nette/di": "~2.3",
2424
"latte/latte": "~2.3.0"
2525
},

src/Caching/Cache.php

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

126126
if ($data instanceof Nette\Callback || $data instanceof \Closure) {
127127
$this->storage->lock($key);
128-
$data = call_user_func_array($data, [& $dependencies]);
128+
try {
129+
$data = call_user_func_array($data, [& $dependencies]);
130+
} catch (\Exception $e) {
131+
$this->storage->remove($key);
132+
throw $e;
133+
}
129134
}
130135

131136
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+
});

tests/Storages/IJournalTestCase.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Test: Common tests for all IJournal implementations.
55
*/
66

7-
use Nette\Caching\Cache;
7+
use Nette\Caching\Cache;
88
use Nette\Caching\Storages\IJournal;
99
use Tester\Assert;
1010

0 commit comments

Comments
 (0)