<#7660 flyte deploy: PicklingError ("Cannot pickle...
# flyte-github
a
#7660 flyte deploy: PicklingError ("Cannot pickle files that are not opened for reading: w") — version hash is cloudpickled inside rich's stderr-redirecting spinner Issue created by Javkhaa Describe the bug
flyte deploy
fails during version derivation with a
PicklingError
whenever any task's module globals reference an object that holds a reference to the real
sys.stderr
— loguru's default logger being the common case:
Copy code
Error invoking command: Failed to compute deployment version: your task or environment
captures an unpicklable object (PicklingError: Cannot pickle files that are not opened
for reading: w). This is usually caused by closing over `sys.stdin` / `sys.stdout` /
`sys.stderr`, an open file handle, a thread, or a lock from module-level code. Move the
captured value inside the task function, or pass an explicit `version=...` to
`flyte.deploy(...)` to skip version derivation.
The error message is misleading: the captured value belongs to a third-party library, and the suggested remedy ("move the captured value inside the task function") is not actionable by the user. Root cause Three things interact: 1.
apply()
derives the deployment version by hashing
cloudpickle.dumps(deployment_plan.envs)
(
flyte/_deploy.py:497
). Task functions are pickled by value, so the module globals they reference are pulled into the pickle graph — including loguru's global
logger
, whose default sink holds a direct reference to the original
sys.__stderr__
(a
mode='w'
file object). 2. cloudpickle's
_file_reduce
pickles a stream by reference only via an identity check (
cloudpickle.py:961-964
): if obj is sys.stdout: return getattr, (sys, "stdout") if obj is sys.stderr: return getattr, (sys, "stderr") ... if "r" not in obj.mode and "+" not in obj.mode: raise pickle.PicklingError( "Cannot pickle files that are not opened for reading: %s" % obj.mode ) 3. That
cloudpickle.dumps
call runs inside a rich status spinner —
with common.cli_status(obj.output_format, "Deploying..."):
(
flyte/cli/_deploy.py:216
). Rich's
Live
display replaces
sys.stdout
/
sys.stderr
with
rich.file_proxy.FileProxy
objects for its duration. So at pickling time
sys.stderr
is a
FileProxy
, the
obj is sys.stderr
identity check misses, cloudpickle falls through to the mode check, and raises. I confirmed this by wrapping cloudpickle's file reducer during a real deploy:
Copy code
repr          : <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>
is sys.stderr : False
sys.stderr is now: <rich.file_proxy.FileProxy object at 0x7f0f67382ce0>
is sys.__stderr__ : True
And the reference chain out of the envs dict:
Copy code
envs['<env>']._tasks['<env>.<task>'].func.__globals__['logger']._core.handlers[0]._sink._stream
Either condition alone is harmless — the envs pickle fine outside the spinner, and they pickle fine under a swapped stderr if the task globals don't hold a stream. It needs both. Expected behavior
flyte deploy
succeeds regardless of whether a task module imports loguru (or any library that holds a stream reference). To Reproduce # tasks.py import flyte from loguru import logger env = flyte.TaskEnvironment(name="demo") @env.task async def hello() -> str: logger.info("hi") return "hi"
Copy code
flyte deploy --all --recursive .
Fails with the
PicklingError
above. Adding
--output-format table-simple
(which makes
cli_status
return a
nullcontext
instead of a spinner, leaving
sys.stderr
intact) makes the same deploy succeed — which isolates the spinner as the trigger. Suggested fix Compute the version hash outside the
Live
display, or restore
sys.stdout
/
sys.stderr
around the
cloudpickle.dumps
calls in
apply()
. Any code that cloudpickles user-supplied object graphs shouldn't run while stdout/stderr are monkeypatched, since cloudpickle's stream handling is identity-based. Secondary: the error message should not attribute the capture to the user's task/environment, since the offending object is frequently a third-party global the user cannot move. Workarounds (for anyone hitting this) •
flyte --output-format table-simple deploy ...
— no spinner,
sys.stderr
untouched. • Give loguru a callable sink that resolves
sys.stderr
at write time rather than capturing it at import: logger.remove() logger.add(lambda m: sys.stderr.write(m), level="INFO") •
--version=...
to skip version derivation entirely (loses content-derived versions). Environment •
flyte
2.3.1 (PyPI) •
cloudpickle
3.1.2 •
loguru
0.7.3 • Python 3.11, Linux flyteorg/flyte