{"record":{"id":"b75d4dc7e531bd40","repo":"abhigyanpatwari/GitNexus","slug":"terminatejobobject-failed","errorCode":null,"errorMessage":"TerminateJobObject failed","messagePattern":"TerminateJobObject failed","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"eval/workflow_bench/process_control.py","lineNumber":282,"sourceCode":"            process_handle = wintypes.HANDLE(int(process._handle))  # type: ignore[attr-defined]\n            if not kernel32.AssignProcessToJobObject(handle, process_handle):\n                raise OSError(ctypes.get_last_error(), \"AssignProcessToJobObject failed\")\n            status = int(ntdll.NtResumeProcess(process_handle))\n            if status != 0:\n                raise OSError(status, \"NtResumeProcess failed\")\n        except BaseException:\n            # The child is still suspended when assignment fails. Kill it\n            # before releasing any handle; never retry with job breakaway.\n            process.kill()\n            process.wait()\n            self.close()\n            raise\n\n    def terminate(self) -> None:\n        import ctypes\n\n        if self._handle and not self._kernel32.TerminateJobObject(self._handle, 1):\n            raise OSError(ctypes.get_last_error(), \"TerminateJobObject failed\")\n\n    def active_processes(self) -> int:\n        \"\"\"Return live members so closing the job cannot hide forced cleanup.\"\"\"\n\n        import ctypes\n        from ctypes import wintypes\n\n        class JOBOBJECT_BASIC_ACCOUNTING_INFORMATION(ctypes.Structure):\n            _fields_ = [\n                (\"TotalUserTime\", ctypes.c_longlong),\n                (\"TotalKernelTime\", ctypes.c_longlong),\n                (\"ThisPeriodTotalUserTime\", ctypes.c_longlong),\n                (\"ThisPeriodTotalKernelTime\", ctypes.c_longlong),\n                (\"TotalPageFaultCount\", wintypes.DWORD),\n                (\"TotalProcesses\", wintypes.DWORD),\n                (\"ActiveProcesses\", wintypes.DWORD),\n                (\"TotalTerminatedProcesses\", wintypes.DWORD),\n            ]","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/abhigyanpatwari/GitNexus/blob/d540b00184d71a896261ee02670da9a92d59d8f7/eval/workflow_bench/process_control.py#L264-L300","documentation":"_WindowsJob.terminate() calls TerminateJobObject(handle, exit_code=1); on FALSE it raises OSError(get_last_error(), ...). Termination is best-effort cleanup during abort/timeout, so failure here means the job handle is stale or the kernel rejected the call.","triggerScenarios":"On Windows during timeout/cleanup when TerminateJobObject returns FALSE: typically self._handle was already closed by a prior close(), or the OS denied termination (rare).","commonSituations":"Double-terminate (close() then terminate()) due to concurrent timeout and ownership-slot abort; handle reuse race; security software blocking termination.","solutions":["Inspect OSError.errno — ERROR_INVALID_HANDLE (6) means the handle was already closed; guard terminate() with `if self._handle`.","Avoid calling both run_managed's ownership abort and an explicit terminate on the same job.","Update process_control.py to make terminate() tolerant of an already-closed handle (return rather than raise on ERROR_INVALID_HANDLE) — open an issue.","Run on Linux to bypass the Windows job path."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    job.terminate()\nexcept OSError as exc:\n    if sys.platform == 'win32' and exc.winerror == 6:  # ERROR_INVALID_HANDLE\n        log.debug('job already closed; nothing to terminate')\n    else:\n        raise","preventionTips":["Do not call terminate() after close().","Avoid overlapping timeout-abort and explicit terminate() on the same job.","Track job lifecycle in the caller so cleanup runs once."],"tags":["windows","win32","job-object","cleanup","process-control"],"backgroundTag":null,"analyzedSha":"d540b00184d71a896261ee02670da9a92d59d8f7","analyzedAt":"2026-08-12T19:50:25.132Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}