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

[PULP-218] Add timeout to immediate tasks #6155

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions pulpcore/tasking/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import contextvars
import importlib
import logging
import signal
import sys
import traceback
from datetime import timedelta
Expand All @@ -20,6 +21,7 @@
TASK_STATES,
TASK_DISPATCH_LOCK,
)
from pulpcore.exceptions import TimeoutException
from pulpcore.tasking.kafka import send_task_notification

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -47,10 +49,28 @@ def wakeup_worker():


def execute_task(task):
# This extra stack is needed to isolate the current_task ContextVar
contextvars.copy_context().run(_execute_task, task)


def execute_immediate_task(task):
# set alarm timeout
IMMEDIATE_TASK_TIMEOUT = 60 * 5

def timeout_handler(signum, frame):
raise TimeoutException(f"Immediate task time: {IMMEDIATE_TASK_TIMEOUT}")

signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(IMMEDIATE_TASK_TIMEOUT)
Comment on lines +62 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting.
This is quite close to low level process handling. I wonder if this might interfere with other uses of the signal handler.

Do you think we can instead require that all immediate tasks are async functions? We could then implement the timeout on the asyncio level with much more confidence.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can instead require that all immediate tasks are async functions? We could then implement the timeout on the asyncio level with much more confidence.

Yes. I dont have a good idea of how much friction this imposes, but that's definitely more safe.


try:
# This extra stack is needed to isolate the current_task ContextVar
contextvars.copy_context().run(_execute_task, task)
except TimeoutException:
raise
finally:
signal.alarm(0)


def _execute_task(task):
# Store the task id in the context for `Task.current()`.
current_task.set(task)
Expand Down Expand Up @@ -226,9 +246,13 @@ def dispatch(
).exists()
):
task.unblock()
execute_task(task)
if resources:
notify_workers = True
try:
execute_immediate_task(task)
except TimeoutException:
pass
finally:
if resources:
notify_workers = True
elif deferred:
notify_workers = True
else:
Expand Down
Loading