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

feat: support waiting for background tasks on the per_worker policy #451

Merged
merged 6 commits into from
Nov 28, 2024

Conversation

nyannyacha
Copy link
Collaborator

@nyannyacha nyannyacha commented Nov 27, 2024

What kind of change does this PR introduce?

Feature

Description

This PR prevents the supervisor from switching the user worker to the early drop state if there is a microtask (Promise) queued in the user worker.

Note that this does not track all promises, only those marked for tracking at runtime by extra typing, also, this support only works in the per_worker policy.

Type declaration

// its definition is found in `types/global.d.ts` in this PR.
declare function markAsBackgroundTask<T>(promise: Promise<T>): Promise<T>;

Usage

markAsBackgroundTask(new Promise(...));

Side note

This behavior will interact with the graceful shutdown.

In other words, even if runtime is during the graceful shutdown state, if there are background tasks left in the user worker, the supervisor will wait for them to be completed unless it is forced to terminate by the resource limit.

The timeout for the graceful shutdown specified by the flag in cli is respected, so the maximum time that the supervisor can wait for background tasks is limited to the graceful shutdown timeout. (For each user worker)

Future note

Today, I have made it so that this can only be done by extra typing, which is due to the limitation that we cannot get consistent promise counts that can determine whether all background tasks created by core scripts or our deno ext implementation have been resolved.

Most of these inconsistencies seem to occur in our HTTP request listener implementation (not sure), and if this is resolved, extra typing may not be necessary, so it should be reviewed later.

@nyannyacha nyannyacha force-pushed the feat-wait-background-tasks branch from 61c873a to d76b7f1 Compare November 27, 2024 02:07
@nyannyacha nyannyacha marked this pull request as ready for review November 27, 2024 21:47
@nyannyacha nyannyacha merged commit 3cddc61 into supabase:main Nov 28, 2024
3 checks passed
@nyannyacha nyannyacha deleted the feat-wait-background-tasks branch November 28, 2024 04:52
Copy link

🎉 This PR is included in version 1.65.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants