Skip to content
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

How to setup execution timeout for synchronous tasks with blocking IO? #209

Open
3DFace opened this issue Jan 3, 2025 · 0 comments
Open

Comments

@3DFace
Copy link

3DFace commented Jan 3, 2025

Hello!

It seems like ContextWorker relies on task-runner process/thread to react on cancellation.
But in a case of blocking IO task-runner can't do that.

<?php
use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Amp\TimeoutCancellation;
use function Amp\Parallel\Worker\submit;

class SleepTask implements Task
{
    public function __construct(private int $seconds) {}
	
    public function run(Channel $channel, Cancellation $cancellation) : int
    {
        \sleep($this->seconds); // to emulate blocking IO
        return $this->seconds;
    }
}

public function test() : int
{
    $exec_timeout = new TimeoutCancellation(2);
    $task = new SleepTask(10);
    return submit($task, $exec_timeout)->await();
}

echo test().PHP_EOL;

I want to make SleepTask get failed after 2 seconds. But it runs 10 seconds instead.

If I replace in ContextWorker this:

$cancellation->subscribe(static fn () => async(
    $context->send(...),
    new Internal\JobCancellation($jobId),
)->ignore());

with:

$cancellation->subscribe(fn () => async(
    $this->kill(...)
)->ignore());

it works as I want.

Is there any correct way to achieve such behavior (kill on timeout)?

Probably having $context reference in Amp\Parallel\Worker\Execution would be helpful.
It would be possible to call $context->kill() from user-space code.

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant