Skip to content

Commit 3acfdb4

Browse files
committed
* fix bugs
1 parent 2b442e9 commit 3acfdb4

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

exceptions.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,24 +232,13 @@ static void exception_coroutine_entry(void)
232232

233233
bool async_spawn_and_throw(zend_object *exception, zend_async_scope_t *scope, int32_t priority)
234234
{
235-
bool need_new_scope = false;
236-
237235
if (scope == NULL) {
238236
scope = ZEND_ASYNC_CURRENT_SCOPE;
239237
}
240238

239+
// If the current Scope can no longer create coroutines, we use a trick.
240+
// We create a child Scope with a single coroutine.
241241
if (ZEND_ASYNC_SCOPE_IS_CANCELLED(scope) || ZEND_ASYNC_SCOPE_IS_CLOSED(scope)) {
242-
scope = scope->parent_scope;
243-
need_new_scope = true;
244-
}
245-
246-
if (scope == NULL || ZEND_ASYNC_SCOPE_IS_CLOSED(scope)) {
247-
async_warning("To throw an exception in a coroutine, "
248-
"the Scope must be active or have a parent that is not closed.");
249-
return false;
250-
}
251-
252-
if (need_new_scope) {
253242
scope = ZEND_ASYNC_NEW_SCOPE(scope);
254243
if (UNEXPECTED(scope == NULL)) {
255244
async_warning("Failed to create a new Scope for throwing an exception in a coroutine.");

scope.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ static bool scope_catch_or_cancel(
881881
//
882882

883883
if (false == is_cancellation
884-
&& (async_scope->child_exception_fci != NULL || async_scope->child_exception_fcc != NULL)
884+
&& (async_scope->exception_fci != NULL || async_scope->child_exception_fci != NULL)
885885
&& try_to_handle_exception(
886886
async_scope, (async_scope_t *) from_scope, exception, (async_coroutine_t *) coroutine
887887
)) {
@@ -960,8 +960,6 @@ static bool scope_catch_or_cancel(
960960
);
961961
}
962962

963-
goto exit_true;
964-
965963
exit_false:
966964
result = false;
967965
exit_true:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Coroutine onFinally single exception handling
3+
--FILE--
4+
<?php
5+
6+
use function Async\spawn;
7+
use Async\CompositeException;
8+
use Async\Scope;
9+
10+
$scope = new Scope();
11+
12+
$scope->setExceptionHandler(function($scope, $coroutine, $exception) {
13+
echo "Caught single exception: " . $exception->getMessage() . "\n";
14+
});
15+
16+
$coro = $scope->spawn(function() {
17+
return "result";
18+
});
19+
20+
// Add single finally handler that will throw exception
21+
$coro->onFinally(function($coroutine) {
22+
throw new Exception("Single exception");
23+
});
24+
25+
?>
26+
--EXPECT--
27+
Caught single exception: Single exception

0 commit comments

Comments
 (0)