Skip to content

Commit 633c26f

Browse files
author
Edmond
committed
Delegate fiber GC to coroutine when fiber has coroutine
When fiber->coroutine is set, the fiber's GC handler now delegates to the coroutine's GC handler by adding the coroutine object to GC buffer. This ensures proper garbage collection of coroutine data including: - fcall parameters and function name - coroutine result - execution stack - all other coroutine-managed resources For non-coroutine fibers, the original GC logic remains unchanged.
1 parent 04f64a6 commit 633c26f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Zend/zend_fibers.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,18 +1375,26 @@ static HashTable *zend_fiber_object_gc(zend_object *object, zval **table, int *n
13751375
zend_get_gc_buffer *buf = zend_get_gc_buffer_create();
13761376

13771377
/*
1378-
* For coroutine path add fcall to GC (if ownership not taken).
1379-
* For non-coroutine path add fci and result to GC.
1378+
* For coroutine path: add coroutine object to GC.
1379+
* GC will call coroutine's get_gc handler which handles fcall, result, and execution stack.
13801380
*/
1381-
if (fiber->coroutine != NULL && fiber->fcall != NULL) {
1382-
zend_get_gc_buffer_add_zval(buf, &fiber->fcall->fci.function_name);
1383-
zend_get_gc_buffer_add_zval(buf, &fiber->coroutine->result);
1384-
/* result in coroutine->result - coroutine's GC handles it */
1385-
} else {
1386-
zend_get_gc_buffer_add_zval(buf, &fiber->fci.function_name);
1387-
zend_get_gc_buffer_add_zval(buf, &fiber->result);
1381+
if (fiber->coroutine != NULL) {
1382+
zend_object *coroutine_obj = ZEND_ASYNC_EVENT_TO_OBJECT(&fiber->coroutine->event);
1383+
zend_get_gc_buffer_add_obj(buf, coroutine_obj);
1384+
1385+
/* Add fcall if it's still owned by fiber (not yet transferred to coroutine) */
1386+
if (fiber->fcall != NULL) {
1387+
zend_get_gc_buffer_add_zval(buf, &fiber->fcall->fci.function_name);
1388+
}
1389+
1390+
zend_get_gc_buffer_use(buf, table, num);
1391+
return NULL;
13881392
}
13891393

1394+
/* Non-coroutine path: add fci and result to GC */
1395+
zend_get_gc_buffer_add_zval(buf, &fiber->fci.function_name);
1396+
zend_get_gc_buffer_add_zval(buf, &fiber->result);
1397+
13901398
if (fiber->context.status != ZEND_FIBER_STATUS_SUSPENDED || fiber->caller != NULL) {
13911399
zend_get_gc_buffer_use(buf, table, num);
13921400
return NULL;

0 commit comments

Comments
 (0)