{"record":{"id":"0ae52527037fb78b","repo":"microsoft/aspire","slug":"writefile-failed-with-error-error","errorCode":null,"errorMessage":"WriteFile failed with error {error}","messagePattern":"WriteFile failed with error (.+?)","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":401,"sourceCode":"                                self._handle,\n                                chunk,\n                                len(chunk),\n                                ctypes.byref(bytes_written),\n                                ctypes.byref(overlapped)\n                            )\n\n                            if not success:\n                                error = ctypes.get_last_error()\n                                if error == self.ERROR_IO_PENDING:\n                                    # Wait for the operation to complete\n                                    _kernel32.GetOverlappedResult(\n                                        self._handle,\n                                        ctypes.byref(overlapped),\n                                        ctypes.byref(bytes_written),\n                                        True  # wait\n                                    )\n                                else:\n                                    raise OSError(f\"WriteFile failed with error {error}\")\n\n                            offset += bytes_written.value\n                    finally:\n                        _kernel32.CloseHandle(overlapped.hEvent)\n\n                def close(self) -> None:\n                    '''Close the handle.'''\n                    if self._handle is not None and self._handle != self.INVALID_HANDLE_VALUE:\n                        _kernel32.CloseHandle(self._handle)\n                        self._handle = None\n\n            def _connect_pipe(socket_path: str, timeout_sec: float) -> _PipeSocket:\n                '''Connect to a named pipe with timeout, retrying until available.'''\n                pipe_path = f\"\\\\\\\\.\\\\pipe\\\\{socket_path}\"\n                _logger.debug(\"Connecting to: %s\", pipe_path)\n\n                start_time = time.time()\n                while (time.time() - start_time) < timeout_sec:","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L383-L419","documentation":"Raised by the Windows named-pipe socket wrapper when the WriteFile Win32 call fails with an unexpected error during overlapped I/O. This typically means the pipe's server end is gone (broken pipe) or the handle is invalid, so the write could not be delivered.","triggerScenarios":"Sending a request over the pipe after the host process exited; writing to an already-closed handle; partial-write loop encountering ERROR_BROKEN_PIPE or ERROR_NO_DATA.","commonSituations":"Long-running client whose host was restarted between calls; AppHost terminated by Ctrl+C while client mid-request; race between close() and an in-flight write.","solutions":["Catch OSError around sendall and reconnect before retrying the request.","Verify the host is running when writes start failing (host exit is the usual cause).","Ensure close() is not called concurrently with write operations.","Decode the reported error code to distinguish broken pipe from disk/full or access issues."],"exampleFix":"// before\nsocket.sendall(payload)\n\n// after\ntry:\n    socket.sendall(payload)\nexcept OSError:\n    socket = reconnect_to_pipe(pipe_path, timeout_sec=30)\n    socket.sendall(payload)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    sock.sendall(payload)\nexcept OSError:\n    sock = reconnect_to_pipe(pipe_path, timeout_sec=30)\n    sock.sendall(payload)","preventionTips":["Check host liveness before sending requests.","Avoid concurrent close() and sendall() calls.","Centralize send logic with automatic reconnection."],"tags":["named-pipe","windows","io","python"],"backgroundTag":"broken-pipe","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}