abhigyanpatwari/GitNexus · error · OSError

AssignProcessToJobObject failed

Error message

AssignProcessToJobObject failed

What it means

kernel32.AssignProcessToJobObject(job, process_handle) returned FALSE. On modern Windows a process can only be in one job unless nested jobs are enabled; the harness spawns the child CREATE_SUSPENDED precisely so a failed assignment can be cleaned up (process.kill() + job close) before re-raising.

Source

Thrown at eval/workflow_bench/process_control.py:266

        kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
        kernel32.CloseHandle.restype = wintypes.BOOL
        ntdll.NtResumeProcess.argtypes = [wintypes.HANDLE]
        ntdll.NtResumeProcess.restype = wintypes.LONG

        handle = kernel32.CreateJobObjectW(None, None)
        if not handle:
            raise OSError(ctypes.get_last_error(), "CreateJobObjectW failed")
        self._kernel32 = kernel32
        self._handle = handle
        ownership_slot[-1] = (process, self, None)
        try:
            limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION()
            limits.BasicLimitInformation.LimitFlags = 0x00002000  # KILL_ON_JOB_CLOSE
            if not kernel32.SetInformationJobObject(handle, 9, ctypes.byref(limits), ctypes.sizeof(limits)):
                raise OSError(ctypes.get_last_error(), "SetInformationJobObject failed")
            process_handle = wintypes.HANDLE(int(process._handle))  # type: ignore[attr-defined]
            if not kernel32.AssignProcessToJobObject(handle, process_handle):
                raise OSError(ctypes.get_last_error(), "AssignProcessToJobObject failed")
            status = int(ntdll.NtResumeProcess(process_handle))
            if status != 0:
                raise OSError(status, "NtResumeProcess failed")
        except BaseException:
            # The child is still suspended when assignment fails. Kill it
            # before releasing any handle; never retry with job breakaway.
            process.kill()
            process.wait()
            self.close()
            raise

    def terminate(self) -> None:
        import ctypes

        if self._handle and not self._kernel32.TerminateJobObject(self._handle, 1):
            raise OSError(ctypes.get_last_error(), "TerminateJobObject failed")

    def active_processes(self) -> int:

View on GitHub (pinned to d540b00184)

Solutions

  1. Check OSError.errno — ERROR_ACCESS_DENIED (5) or ERROR_NOT_SUPPORTED (50) -> parent is in another non-nestable job.
  2. Launch the benchmark from a process that is NOT already inside a Job (plain cmd.exe or PowerShell outside containers/IDE).
  3. Upgrade to Windows 8+ where nested jobs are default (silicon-safe default).
  4. Run the workflow bench on Linux instead, where ownership uses POSIX process groups.
Defensive patterns

Strategy: validation

Validate before calling

import sys, ctypes
if sys.platform == 'win32':
    # Detect whether the parent process is already inside a non-nestable job.
    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
    # IsProcessInJob exists on Vista+; if True and nested jobs are off, expect this error.
    in_job = ctypes.wintypes.BOOL()
    kernel32.IsProcessInJob(kernel32.GetCurrentProcess(), None, ctypes.byref(in_job))
    if in_job.value:
        log.warning('parent already in a job; AssignProcessToJobObject may fail')

Try / catch

try:
    run_managed(cmd, timeout=60)
except OSError as exc:
    if sys.platform == 'win32' and 'AssignProcessToJobObject' in str(exc) and exc.winerror in (5, 50):
        raise RuntimeError('launch the benchmark outside any container/IDE job (nested jobs unsupported)')
    raise

Prevention

When it happens

Trigger: On Windows when the child is already a member of another job that does not allow nesting, or when access is denied to the job handle. Common when the parent Python process itself is already inside a Job (e.g. launched from a container, CI runner, terminal that uses jobs).

Common situations: Running under Docker Desktop for Windows, WSL2 broker, Visual Studio Code task, or any CI runner that wraps processes in a non-nested job; older Windows 7 / Server 2008 R2 without nested jobs enabled.

Related errors


AI-assisted analysis of abhigyanpatwari/GitNexus@d540b00184 (2026-08-12). Data as JSON: /api/errors/b15f8ec0d51ba527. Report an issue: GitHub.