@@ -243,11 +243,12 @@ def _assistant_message_schema_wrapper() -> str:
243243
244244def _run_claude_print (* , system : str , prompt : str , model : str , tools : list [dict [str , Any ]] | None , tool_choice : str | dict [str , Any ] | None , return_message : bool , timeout : int | None , attachments : list [dict [str , Any ]] | None = None ) -> tuple [str , dict [str , Any ], dict [str , int ]]:
245245 effort = _normalize_reasoning_effort (REASONING_EFFORT )
246- # Use mkdtemp + manual rmtree to avoid WinError 32 on Windows
247- # where the spawned claude/node subprocess still holds a handle
248- # to the temp directory when the context manager tries to clean up.
249- temp_dir = tempfile .mkdtemp (prefix = "skillopt_claude_" )
250- try :
246+ # A lingering claude/node handle can make Windows cleanup raise WinError 32.
247+ # Python 3.10+ supports suppressing cleanup failures while retaining
248+ # TemporaryDirectory's normal lifecycle and best-effort removal behavior.
249+ with tempfile .TemporaryDirectory (
250+ prefix = "skillopt_claude_" , ignore_cleanup_errors = True ,
251+ ) as temp_dir :
251252 copied_attachments = _copy_attachments_to_temp (attachments or [], temp_dir )
252253 prompt_for_cli = _append_attachment_instructions (prompt , copied_attachments )
253254 cmd = [CLAUDE_BIN , "-p" , "--output-format" , "json" , "--permission-mode" , CLAUDE_PERMISSION_MODE , "--add-dir" , temp_dir ]
@@ -291,8 +292,6 @@ def _run_claude_print(*, system: str, prompt: str, model: str, tools: list[dict[
291292 raw_text , result_event = _extract_result (stream )
292293 usage_info = _usage_from_result (result_event )
293294 return raw_text , result_event or {}, usage_info
294- finally :
295- shutil .rmtree (temp_dir , ignore_errors = True )
296295
297296
298297def _compat_message_from_payload (payload : Any ) -> CompatAssistantMessage :
0 commit comments