Skip to content

Fix GH-10437: Segfault on suspending a dead fiber #10438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Zend/tests/fibers/gh10437.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
GH-10437 (Segfault on suspending a dead fiber)
--FILE--
<?php

set_time_limit(1);

register_shutdown_function(function () {
Fiber::getCurrent()->suspend();
Copy link
Member

@kelunik kelunik Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find my previous comment anymore, so it might have gone lost... found it...

IMO, Fiber::getCurrent() should return null here, as I expect the stack to be unwound and shutdown functions being called from main then.

/cc @trowski

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Fiber::getCurrent() should be returning null. Fix in #10443.

});

$fiber = new Fiber(function () {
while (1);
});
$fiber->start();
--EXPECTF--
Fatal error: Maximum execution time of 1 second exceeded in %s

Fatal error: Uncaught FiberError: Cannot suspend a dead fiber in %s
Stack trace:
#0 %s: Fiber::suspend()
#1 [internal function]: {closure}()
#2 {main}
thrown in %s
29 changes: 28 additions & 1 deletion Zend/zend_fibers.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,26 @@ static HashTable *zend_fiber_object_gc(zend_object *object, zval **table, int *n
return lastSymTable;
}

static const char* zend_fiber_status_to_string(zend_fiber *fiber)
{
switch (fiber->context.status) {
case ZEND_FIBER_STATUS_INIT:
return "initializing";
break;
case ZEND_FIBER_STATUS_RUNNING:
return "running";
break;
case ZEND_FIBER_STATUS_SUSPENDED:
return "suspended";
break;
case ZEND_FIBER_STATUS_DEAD:
return "dead";
break;
}

return "(unknown status)";
}

ZEND_METHOD(Fiber, __construct)
{
zend_fcall_info fci;
Expand Down Expand Up @@ -846,7 +866,14 @@ ZEND_METHOD(Fiber, suspend)
RETURN_THROWS();
}

ZEND_ASSERT(fiber->context.status == ZEND_FIBER_STATUS_RUNNING || fiber->context.status == ZEND_FIBER_STATUS_SUSPENDED);
if (UNEXPECTED(fiber->context.status != ZEND_FIBER_STATUS_RUNNING && fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED)) {
zend_throw_error(zend_ce_fiber_error,
"Cannot suspend %s %s fiber",
((fiber->context.status == ZEND_FIBER_STATUS_INIT) ? "an" : "a"),
zend_fiber_status_to_string(fiber)
);
RETURN_THROWS();
}

fiber->execute_data = EG(current_execute_data);
fiber->stack_bottom->prev_execute_data = NULL;
Expand Down