[PP-1] Ensure asynchronous tasks that run beyond the length of a request have the chance to complete before process exit

Created on 3 March 2024, 9 months ago

Problem/Motivation

With πŸ“Œ Add revoltphp/event-loop dependency to core Active we enable core and contrib developers to easily kick-off asynchronous tasks. In a lot of use-cases (e.g. BigPipe) where the request code waits for the tasks that were kicked-off to be completed this doesn't matter because the tasks are shorter than the request. However it is also possible to kick-off tasks (e.g. search engine indexing) where the result is not important for the current request.

In the case that the tasks take longer to complete than it takes to send the response for a request and to terminate the kernel, in the current Drupal implementation, the tasks may get discarded as the request process exits.

To demonstrate this and other Revolt related things I've created a Revolt Playground repository on GitHub. For this problem compare the two examples in the Bootstrap demo.

Steps to reproduce

Proposed resolution

Add EventLoop::run() to the end of the index.php file so that async tasks that were started but not yet completed before we get to that line have the opportunity to complete.

This placement is under the assumption that $kernel->terminate($request, $response); does not throw away any data that these asynchronous tasks may need.

Remaining tasks

This is postponed on πŸ“Œ Add revoltphp/event-loop dependency to core Active

  • Agree on the placement of EventLoop::run or optionally provide a way for async tasks to signal they need to complete before $kernel->terminate($request, $response); and provide a synchronisation point in Drupal core
  • Create the merge request
  • Write the release notes

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated 35 minutes ago

Created by

πŸ‡³πŸ‡±Netherlands kingdutch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kingdutch
  • πŸ‡³πŸ‡±Netherlands kingdutch

    πŸ“Œ Add revoltphp/event-loop dependency to core Active has been merged so this is no longer blocked.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Briefly discussed this with @Kingdutch in slack. I think the last point in the request where something can or should run is at the very end of DrupalKernel::terminate().

    e.g. a needs termination service like automated cron could (and probably should) start off an async task - like sending an email from a queue could be async and interleave with other things, or cron might call out to something that doesn't know it's running in cron but makes use of async things. So it makes sense to make the point of no return after that rather than before.

Production build 0.71.5 2024