Skip to content

Preserve co_filename for code pickled by value from a source file - #629

Open
lyn1dev wants to merge 1 commit into
cloudpipe:masterfrom
lyn1dev:preserve-co-filename-for-file-backed-code
Open

lyn1dev wants to merge 1 commit into
cloudpipe:masterfrom
lyn1dev:preserve-co-filename-for-file-backed-code

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026

Copy link
Copy Markdown

What was wrong

_code_reduce replaced the co_filename of every pickled code object with <dynamic-code>. That makes payloads deterministic for code without a source file (the notebook case of #538), but it also strips the filename of code that is pickled by value from a regular source file, for example a nested function returned by another function.

Tools that map executed code back to its source rely on co_filename. After such a round trip, coverage.py reports no data for the restored function, inspect.getsource() raises OSError, and tracebacks point at <dynamic-code>. Since 3.1.3, downstream projects that serialize functions and then measure their execution (e.g. Dask wrappers) lose that coverage; #628 has the details and the downstream CI links.

The fix

_code_reduce now keeps co_filename when it names a source file that exists on disk, and only falls back to <dynamic-code> when it does not. Code without a real file behind it (<string>, <ipython-input-...>, removed files) keeps the deterministic placeholder, so the behaviour that #560 introduced for notebooks is unchanged. The check is a single os.path.isfile call per pickled code object.

Testing

New tests in tests/cloudpickle_test.py:

  • test_file_backed_func_preserves_co_filename: a nested function from the test module is pickled by value; the restored code object keeps the filename, the file exists, and inspect.getsource() returns the function source.
  • test_dynamic_func_gets_placeholder_co_filename: a function compiled from <string> still restores with co_filename == "<dynamic-code>".

Results on Windows, CPython 3.13.18:

  • Without the fix: test_file_backed_func_preserves_co_filename fails for both protocol classes ('<dynamic-code>' != '...\tests\cloudpickle_test.py'), the placeholder test passes.
  • With the fix: 256 passed, 24 skipped, 2 xfailed for the full suite (pytest tests/), including tests/cloudpickle_ipykernel_test.py::test_deterministic_payload_for_dynamic_func_in_notebook (notebook payloads are still identical across two kernels).
  • End-to-end check with coverage.py 7.17.4 on a function pickled by value from an importable module: without the fix get_data().lines(path) is None and the run warns "No data was collected"; with the fix the body line is recorded.
  • ruff check --select E4,E7,E9,F and black --check are clean on the changed files.

A CHANGES.md entry is included.

Fixes #628

AI was used to write this code.

@vibhor-aggr vibhor-aggr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordinary source-file case works on 61e4de8: a restored nested function points to its original file, inspect.getsource finds its body, and line tracing records the source line. There is still one notebook case from #538 that the new isfile check does not distinguish: the notebook debugger writes real, process-specific cell files.

I checked the same function in two independent processes per case using ipykernel 6.30.1 and CPython 3.13.5. Both executed the cell via ZMQInteractiveShell.run_cell with its normal compiler. In the second case I first called ipykernel's actual Debugger.dumpCell handler with that cell's source (no frontend/network transport). All restored functions returned the same result.

Across two processes Base b92b61d PR 61e4de8
Cell not dumped to disk Identical payloads Identical payloads
Same cell written by dumpCell Identical payloads Different payloads

On the head, the restored filenames were /tmp/ipykernel_7/2290290463.py and /tmp/ipykernel_9/2290290463.py; the payload SHA-256 prefixes were 976bfbfa and a9639414. The base normalized both to <dynamic-code>. This explains why the undumped notebook test can pass while a debugged cell invalidates a downstream cache after a kernel restart.

Could the cross-kernel test cover a debug-dumped cell as well as the ordinary-file coverage case? File existence alone is not enough to identify a persistent source file. If this tradeoff is intentional, it is worth narrowing the changelog's determinism claim.

Verified with OpenAI Codex.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3.1.3 co_filename normalization breaks coverage of serialized file-backed functions

2 participants