{"record":{"id":"c5e3dfc68f7f3d78","repo":"apache/beam","slug":"cannot-pickle-standard-input","errorCode":null,"errorMessage":"Cannot pickle standard input","messagePattern":"Cannot pickle standard input","errorType":"exception","errorClass":"PicklingError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py","lineNumber":1078,"sourceCode":"\ndef _classmethod_reduce(obj):\n  orig_func = obj.__func__\n  return type(obj), (orig_func, )\n\n\ndef _file_reduce(obj):\n  \"\"\"Save a file.\"\"\"\n  import io\n\n  if not hasattr(obj, \"name\") or not hasattr(obj, \"mode\"):\n    raise pickle.PicklingError(\n        \"Cannot pickle files that do not map to an actual file\")\n  if obj is sys.stdout:\n    return getattr, (sys, \"stdout\")\n  if obj is sys.stderr:\n    return getattr, (sys, \"stderr\")\n  if obj is sys.stdin:\n    raise pickle.PicklingError(\"Cannot pickle standard input\")\n  if obj.closed:\n    raise pickle.PicklingError(\"Cannot pickle closed files\")\n  if hasattr(obj, \"isatty\") and obj.isatty():\n    raise pickle.PicklingError(\"Cannot pickle files that map to tty objects\")\n  if \"r\" not in obj.mode and \"+\" not in obj.mode:\n    raise pickle.PicklingError(\n        \"Cannot pickle files that are not opened for reading: %s\" % obj.mode)\n\n  name = obj.name\n\n  retval = io.StringIO()\n\n  try:\n    # Read the whole file\n    curloc = obj.tell()\n    obj.seek(0)\n    contents = obj.read()\n    obj.seek(curloc)","sourceCodeStart":1060,"sourceCodeEnd":1096,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py#L1060-L1096","documentation":"Beam's cloudpickle _file_reduce refuses to pickle sys.stdin, because the worker process's standard input cannot be meaningfully reconstructed on another machine. Pickling any closure or object referencing sys.stdin raises pickle.PicklingError('Cannot pickle standard input').","triggerScenarios":"Cloudpickling an object/closure that captures sys.stdin directly; a function defined at module level that closes over stdin; serializing an object whose __reduce__ walks into sys.stdin.","commonSituations":"Pipelines run with stdin redirection/piped input where code references sys.stdin; interactive notebooks capturing stdin; CLI tools embedding stdin readers in Beam DoFns.","solutions":["Remove the sys.stdin reference from the code being pickled; read stdin in the main process and pass data (or its path) instead.","Read stdin into a variable/string before defining the pickled closure.","Open a named file (e.g. /dev/stdin path may still fail; prefer a real temp file) and reference it by path inside the worker.","Restructure so worker code receives data via pipeline inputs, not stdin."],"exampleFix":"// before\nclass Reader: src = sys.stdin\n// after\nclass Reader: src = open('input.txt')  # or pass data via pipeline","handlingStrategy":"validation","validationCode":"import sys\nif any(v is sys.stdin for v in closure_vars):\n    raise ValueError('sys.stdin cannot be pickled; pass data instead')","typeGuard":"def references_stdin(obj):\n    return obj is sys.stdin","tryCatchPattern":"try:\n    cloudpickle.dump(fn)\nexcept pickle.PicklingError as e:\n    if 'standard input' in str(e): fn = rewrite_without_stdin(fn)\n    else: raise","preventionTips":["Do not capture sys.stdin in DoFns or closures","Read stdin in the driver; pass data via pipeline","Pass file paths, not stream objects"],"tags":["python","pickle","serialization","stdin"],"backgroundTag":"json-serialization-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}