How to setup execution timeout for synchronous tasks with blocking IO?
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 859
- Forks
- 69
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the SleepTask example with ContextWorker, TimeoutCancellation, and submit(), then inspect how task-runner handles cancellation and how Execution exposes worker control. Compare the existing ContextWorker cancellation subscription with the proposed kill behavior. Done means a blocking task times out near two seconds instead of running for ten, with its failure observable through await().
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, distributed-systems
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100