Skip to content

Commit

Permalink
Make run_with_user_context crash if multiprocessing worker tries to…
Browse files Browse the repository at this point in the history
… import API
  • Loading branch information
themylogin committed Feb 6, 2025
1 parent 1f42eef commit 4732761
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from middlewared.schema import accepts, Bool, Dict, returns, Str
from middlewared.service import CallError, Service, private

from middlewared.utils.filesystem.access import check_access, check_acl_execute_impl
from middlewared.utils.user_context import run_with_user_context

Expand Down
10 changes: 8 additions & 2 deletions src/middlewared/middlewared/utils/user_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import logging
import os
import subprocess

from typing import Any, Callable, Optional

import middlewared.api

logger = logging.getLogger(__name__)

__all__ = ["run_command_with_user_context", "run_with_user_context", "set_user_context"]
Expand Down Expand Up @@ -46,11 +47,16 @@ def set_user_context(user_details: dict) -> None:
})


def run_with_user_context_initializer(user_details: dict):
middlewared.api.API_LOADING_FORBIDDEN = True
set_user_context(user_details)


def run_with_user_context(func: Callable, user_details: dict, func_args: Optional[list] = None) -> Any:
assert {'pw_uid', 'pw_gid', 'pw_dir', 'pw_name', 'grouplist'} - set(user_details) == set()

with concurrent.futures.ProcessPoolExecutor(
max_workers=1, initializer=functools.partial(set_user_context, user_details)
max_workers=1, initializer=functools.partial(run_with_user_context_initializer, user_details)
) as exc:
return exc.submit(func, *(func_args or [])).result()

Expand Down

0 comments on commit 4732761

Please sign in to comment.