Skip to content

Commit 6fe562f

Browse files
committed
Fix resumed_coroutines assertion: add ZEND_ASYNC_REACTOR_TICK and remove io_close reactor tick
- Implement async_reactor_tick() in scheduler.c: sets scheduler context, executes reactor (no-wait), restores context, drains resumed_coroutines. - Register it as zend_async_reactor_tick_fn during scheduler startup. - Remove the manual reactor tick from libuv_io_close — the uv_close callback will be processed during the next regular reactor tick.
1 parent 565c95f commit 6fe562f

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

libuv_reactor.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3862,7 +3862,6 @@ static bool libuv_io_close(zend_async_io_t *io_base)
38623862
}
38633863

38643864
io->base.state |= ZEND_ASYNC_IO_CLOSED;
3865-
bool need_close_handle = false;
38663865

38673866
/* If the reactor is already shut down (e.g. bailout during memory
38683867
* exhaustion followed by executor_globals_dtor), skip libuv calls. */
@@ -3875,12 +3874,10 @@ static bool libuv_io_close(zend_async_io_t *io_base)
38753874
io->handle.stream.data = io;
38763875
ZEND_ASYNC_EVENT_ADD_REF(&io->base.event);
38773876
uv_close((uv_handle_t *) &io->handle.stream, io_close_cb);
3878-
need_close_handle = true;
38793877
} else if (io->base.type == ZEND_ASYNC_IO_TYPE_UDP) {
38803878
io->handle.udp.data = io;
38813879
ZEND_ASYNC_EVENT_ADD_REF(&io->base.event);
38823880
uv_close((uv_handle_t *) &io->handle.udp, io_close_cb);
3883-
need_close_handle = true;
38843881
}
38853882
/* FILE type: no uv handle to close. */
38863883

@@ -3896,12 +3893,6 @@ static bool libuv_io_close(zend_async_io_t *io_base)
38963893
}
38973894
io->orig_fd = -1;
38983895

3899-
if (need_close_handle && false == ZEND_ASYNC_IS_SCHEDULER_CONTEXT) {
3900-
ZEND_ASYNC_SCHEDULER_CONTEXT = true;
3901-
libuv_reactor_execute(true);
3902-
ZEND_ASYNC_SCHEDULER_CONTEXT = false;
3903-
}
3904-
39053896
return true;
39063897
}
39073898

scheduler.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,22 @@ static void fiber_context_cleanup(zend_fiber_context *context);
8686
/// MODULE INIT/SHUTDOWN
8787
///////////////////////////////////////////////////////////
8888

89+
static zend_always_inline void process_resumed_coroutines(void);
90+
91+
static void async_reactor_tick(void)
92+
{
93+
ZEND_ASYNC_SCHEDULER_CONTEXT = true;
94+
ZEND_ASYNC_REACTOR_EXECUTE(true);
95+
ZEND_ASYNC_SCHEDULER_CONTEXT = false;
96+
97+
if (circular_buffer_is_not_empty(&ASYNC_G(resumed_coroutines))) {
98+
process_resumed_coroutines();
99+
}
100+
}
101+
89102
void async_scheduler_startup(void)
90103
{
104+
zend_async_reactor_tick_fn = async_reactor_tick;
91105
}
92106

93107
void async_scheduler_shutdown(void)

0 commit comments

Comments
 (0)