amphp / amphp/parallel

Can't get CancelledException ends the task properly without killing the child process

Open
#169 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation question
Dominant language
PHP
Stars
859
Forks
69
PR merge metrics
No merged PRs in 30d

Description

Hello,

We would like to be able to get a task killed if a timeout is reached. As such, we thought that the correct approach was to use a TimeoutCancellation.

According to the documentation :

A Cancellation provided to Worker::submit() may be used to request cancellation of the task in the worker. When cancellation is requested in the parent, the Cancellation provided to Task::run() is cancelled. The task may choose to ignore this cancellation request or act accordingly and throw a CancelledException from Task::run(). If the cancellation request is ignored, the task may continue and return a value which will be returned to the parent as though cancellation had not been requested.

In view of these explanation, we have tried to make the task subscribe to the Cancellation of the Task and just throw it, thinking that the Context will be ok with that, and just wait for the next task to run. It must not be the correct approach as the exception seems to kill the child process and make the whole thing crash.

We do not find an example in the repository to fit our need.

Does someone can help us ?

Thank you

Here is the simple code to illustrate what we have tried :

  • main.php :
<?php

use Amp\Future;
use Amp\Parallel\Worker;
use Amp\TimeoutCancellation;

use PhpWorker\Job;

$workerPool = Amp\Parallel\Worker\workerPool();

$execution = Worker\submit(new Job(), new TimeoutCancellation(1));
$future = $execution->getFuture();

$future->finally(function () {
 echo "JOB_END" . PHP_EOL;
});

Future\await([$future]);

echo "MAIN_END"  . PHP_EOL;
  • Job.php :
<?php

namespace PhpWorker;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use function Amp\delay;

class Job implements Task {
 public function run(Channel $channel, Cancellation $cancellation): string {
  $cancellation->subscribe(fn($e) => throw $e);
  delay(2); // fake work, longer than the timeout
  return true;
 }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied main.php and Job.php examples, then read the Worker::submit(), Task::run(), Cancellation, and CancelledException documentation referenced in the report. Reproduce the one-second timeout around the two-second delay and determine whether cancellation should end the task while leaving the child worker usable for later work.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.