<#1291 fix: restore original std streams while clo...
# flyte-github
a
#1291 fix: restore original std streams while cloudpickling for version derivation Pull request opened by cosmicBboy Why are the changes needed? Fixes flyteorg/flyte#7660.
flyte deploy
fails with
PicklingError: Cannot pickle files that are not opened for reading: w
whenever any task module's globals hold a reference to the real
sys.stderr
— loguru's default logger being the common case. Two things interact: 1.
apply()
derives the deployment version by
cloudpickle.dumps(deployment_plan.envs)
. Task functions are pickled by value, so module globals they reference (e.g. loguru's
logger
, whose default sink holds the original stderr) are pulled into the pickle graph. 2. The CLI runs
apply()
inside a rich status spinner, which rebinds `sys.stdout`/`sys.stderr` to
FileProxy
objects for its duration. cloudpickle only pickles streams by reference via an identity check against the current `sys.stdout`/`sys.stderr`, so the captured real stderr misses the check, falls through to the write-mode check, and raises. Reproduced on main with a task module holding a stream reference (loguru-style) pickled inside
console.status(...)
— fails exactly as reported; succeeds outside the spinner or with
--output-format table-simple
. What changes were proposed in this pull request? • Add
flyte._utils.original_std_streams()
, a context manager that temporarily rebinds `sys.stdout`/`sys.stderr` to `sys.__stdout__`/`sys.__stderr__`. • Wrap the version-hash
cloudpickle.dumps
calls in
flyte/_deploy.py
(
apply()
) and
flyte/_serve.py
with it, so cloudpickle's identity-based stream handling sees the original streams even while a spinner is active. • Reword the version-derivation error message: the unpicklable object is frequently a third-party module global the user cannot move, so it no longer attributes the capture to "your task or environment" unconditionally. How was this patch tested? • New regression test `test_apply_version_derivation_under_redirected_std_streams`: an env graph holding
sys.__stderr__
(mimicking loguru's default sink) is deployed while `sys.stdout`/`sys.stderr` are swapped for proxies. It fails on main with the exact error from the issue and passes with this change. •
tests/flyte/test_deploy.py
,
tests/flyte/test_serve.py
,
tests/utils/
all pass (81 tests). • ruff format/check and mypy (pre-commit) pass. 🤖 Generated with Claude Code flyteorg/flyte-sdk