{"record":{"id":"23a0474bf0908367","repo":"microsoft/aspire","slug":"readfile-failed-with-error-error","errorCode":null,"errorMessage":"ReadFile failed with error {error}","messagePattern":"ReadFile failed with error (.+?)","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":366,"sourceCode":"                            self._handle,\n                            buffer,\n                            n,\n                            ctypes.byref(bytes_read),\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_read),\n                                    True  # wait\n                                )\n                            else:\n                                raise OSError(f\"ReadFile failed with error {error}\")\n                    finally:\n                        _kernel32.CloseHandle(overlapped.hEvent)\n\n                    return buffer.raw[:bytes_read.value]\n\n                def sendall(self, data: bytes) -> None:\n                    '''Write all data using overlapped I/O.'''\n                    bytes_written = wintypes.DWORD()\n                    overlapped = self._create_overlapped_event()\n\n                    try:\n                        offset = 0\n                        while offset < len(data):\n                            chunk = data[offset:]\n\n                            success = _kernel32.WriteFile(\n                                self._handle,\n                                chunk,","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L348-L384","documentation":"Raised by the Windows named-pipe socket wrapper when the ReadFile Win32 call fails with an error other than the expected pending/overlap states during overlapped I/O. It wraps the raw Win32 error code, meaning the pipe read itself failed (pipe broken, disconnected, or handle invalid).","triggerScenarios":"Reading from a pipe whose host end has closed (ERROR_BROKEN_PIPE), or after the handle was invalidated/closed, or an overlapped-operation failure not converted to a pending state.","commonSituations":"Host process exited mid-session; abrupt termination of the AppHost while the client waits for a response; network-less local pipe torn down on host restart.","solutions":["Catch OSError in the receive path and reconnect to the pipe (re-run connect logic).","Check whether the host process is still alive; restart it if it crashed.","Log the Win32 error code (231=ERROR_BROKEN_PIPE is most common) to distinguish disconnect from code bugs.","Avoid sharing a single _PipeSocket across threads without synchronization; concurrent reads can corrupt overlapped I/O state."],"exampleFix":"// before\ndata = socket.recv(4096)\n\n// after\ntry:\n    data = socket.recv(4096)\nexcept OSError:\n    socket = reconnect_to_pipe(pipe_path, timeout_sec=30)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    data = sock.recv(n)\nexcept OSError:\n    sock = reconnect_to_pipe(pipe_path, timeout_sec=30)\n    data = sock.recv(n)","preventionTips":["Verify host liveness before long sessions.","Serialize access to the socket across threads.","Wrap every receive in a reconnect-on-OSError helper."],"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"}