Skip to content

Commit fde3c08

Browse files
committed
* Fixes in await methods.
Corrected the memory release model for the async_await_callback_t structure.
1 parent 81b8186 commit fde3c08

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

async.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,14 @@ PHP_FUNCTION(Async_awaitAny)
249249
Z_PARAM_OBJ_OF_CLASS_OR_NULL(cancellation, async_ce_awaitable);
250250
ZEND_PARSE_PARAMETERS_END();
251251

252+
SCHEDULER_LAUNCH;
253+
252254
HashTable * results = zend_new_array(8);
253255

254256
async_await_futures(futures,
255257
1,
256258
false,
257-
ZEND_ASYNC_OBJECT_TO_EVENT(cancellation),
259+
cancellation != NULL ? ZEND_ASYNC_OBJECT_TO_EVENT(cancellation) : NULL,
258260
0,
259261
0,
260262
results,
@@ -273,7 +275,11 @@ PHP_FUNCTION(Async_awaitAny)
273275
}
274276

275277
zval result;
276-
ZVAL_COPY(&result, zend_hash_index_find(results, 0));
278+
ZEND_HASH_FOREACH_VAL(results, zval *item) {
279+
ZVAL_COPY(&result, item);
280+
break;
281+
} ZEND_HASH_FOREACH_END();
282+
277283
zend_array_release(results);
278284

279285
RETURN_ZVAL(&result, 0, 0);
@@ -290,13 +296,15 @@ PHP_FUNCTION(Async_awaitFirstSuccess)
290296
Z_PARAM_OBJ_OF_CLASS_OR_NULL(cancellation, async_ce_awaitable);
291297
ZEND_PARSE_PARAMETERS_END();
292298

299+
SCHEDULER_LAUNCH;
300+
293301
HashTable * results = zend_new_array(8);
294302
HashTable * errors = zend_new_array(8);
295303

296304
async_await_futures(futures,
297305
1,
298306
true,
299-
ZEND_ASYNC_OBJECT_TO_EVENT(cancellation),
307+
cancellation != NULL ? ZEND_ASYNC_OBJECT_TO_EVENT(cancellation) : NULL,
300308
0,
301309
0,
302310
results,
@@ -339,12 +347,14 @@ PHP_FUNCTION(Async_awaitAll)
339347
Z_PARAM_OBJ_OF_CLASS_OR_NULL(cancellation, async_ce_awaitable);
340348
ZEND_PARSE_PARAMETERS_END();
341349

350+
SCHEDULER_LAUNCH;
351+
342352
HashTable * results = zend_new_array(8);
343353

344354
async_await_futures(futures,
345355
0,
346356
false,
347-
ZEND_ASYNC_OBJECT_TO_EVENT(cancellation),
357+
cancellation != NULL ? ZEND_ASYNC_OBJECT_TO_EVENT(cancellation) : NULL,
348358
0,
349359
0,
350360
results,
@@ -371,13 +381,15 @@ PHP_FUNCTION(Async_awaitAllWithErrors)
371381
Z_PARAM_OBJ_OF_CLASS_OR_NULL(cancellation, async_ce_awaitable);
372382
ZEND_PARSE_PARAMETERS_END();
373383

384+
SCHEDULER_LAUNCH;
385+
374386
HashTable * results = zend_new_array(8);
375387
HashTable * errors = zend_new_array(8);
376388

377389
async_await_futures(futures,
378390
0,
379391
true,
380-
ZEND_ASYNC_OBJECT_TO_EVENT(cancellation),
392+
cancellation != NULL ? ZEND_ASYNC_OBJECT_TO_EVENT(cancellation) : NULL,
381393
0,
382394
0,
383395
results,
@@ -416,12 +428,14 @@ PHP_FUNCTION(Async_awaitAnyOf)
416428
Z_PARAM_OBJ_OF_CLASS_OR_NULL(cancellation, async_ce_awaitable);
417429
ZEND_PARSE_PARAMETERS_END();
418430

431+
SCHEDULER_LAUNCH;
432+
419433
HashTable * results = zend_new_array(8);
420434

421435
async_await_futures(futures,
422436
(int)count,
423437
false,
424-
ZEND_ASYNC_OBJECT_TO_EVENT(cancellation),
438+
cancellation != NULL ? ZEND_ASYNC_OBJECT_TO_EVENT(cancellation) : NULL,
425439
0,
426440
0,
427441
results,
@@ -453,10 +467,12 @@ PHP_FUNCTION(Async_awaitAnyOfWithErrors)
453467
HashTable * results = zend_new_array(8);
454468
HashTable * errors = zend_new_array(8);
455469

470+
SCHEDULER_LAUNCH;
471+
456472
async_await_futures(futures,
457473
(int)count,
458474
true,
459-
ZEND_ASYNC_OBJECT_TO_EVENT(cancellation),
475+
cancellation != NULL ? ZEND_ASYNC_OBJECT_TO_EVENT(cancellation) : NULL,
460476
0,
461477
0,
462478
results,
@@ -490,6 +506,8 @@ PHP_FUNCTION(Async_delay)
490506
Z_PARAM_LONG(ms)
491507
ZEND_PARSE_PARAMETERS_END();
492508

509+
SCHEDULER_LAUNCH;
510+
493511
zend_coroutine_t *coroutine = ZEND_ASYNC_CURRENT_COROUTINE;
494512

495513
if (UNEXPECTED(coroutine == NULL)) {

async_API.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,21 @@ static zend_always_inline zend_async_event_t * zval_to_event(const zval * curren
362362
}
363363
}
364364

365+
void async_waiting_callback_dispose(zend_async_event_callback_t *callback, zend_async_event_t * event)
366+
{
367+
async_await_callback_t * await_callback = (async_await_callback_t *) callback;
368+
async_await_context_t * await_context = await_callback->await_context;
369+
370+
await_callback->await_context = NULL;
371+
372+
if (await_context == NULL) {
373+
return;
374+
}
375+
376+
await_context->dtor(await_context);
377+
await_callback->prev_dispose(callback, event);
378+
}
379+
365380
void async_waiting_callback(
366381
zend_async_event_t *event,
367382
zend_async_event_callback_t *callback,
@@ -708,6 +723,8 @@ void async_await_futures(
708723
return;
709724
}
710725

726+
callback->prev_dispose = callback->callback.base.dispose;
727+
callback->callback.base.dispose = async_waiting_callback_dispose;
711728
await_context->ref_count++;
712729

713730
} ZEND_HASH_FOREACH_END();

async_API.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct
4747
async_await_context_t *await_context;
4848
// The key index for the result
4949
zval key;
50+
zend_async_event_callback_dispose_fn prev_dispose;
5051
} async_await_callback_t;
5152

5253
typedef struct

0 commit comments

Comments
 (0)