Skip to content

Stable code identifier pickling - #41859

Open
kristynsmith wants to merge 2 commits into
apache:masterfrom
kristynsmith:stable-code-identifier-pickling
Open

kristynsmith wants to merge 2 commits into
apache:masterfrom
kristynsmith:stable-code-identifier-pickling

Conversation

@kristynsmith

Copy link
Copy Markdown
Contributor

Summary

Update CloudPickler._stable_identifier_function_reduce in sdks/python/apache_beam/internal/cloudpickle/cloudpickle.py to fall back to _dynamic_function_reduce when pickling a function from __main__ with config.pickle_main_by_ref=False.

Motivation

When CloudPickleConfig.get_code_object_params is set (stable code identifier pickling) and pickle_main_by_ref=False (e.g., interactive notebooks or setups where workers run a different binary than the driver), _function_reduce checks _should_pickle_by_reference (which returns False for __main__) and then falls through to _stable_identifier_function_reduce.

Previously, _stable_identifier_function_reduce did not check config.pickle_main_by_ref, so get_code_object_identifier emitted __main__.* code paths anyway. When the worker process's __main__ module does not match the driver's, unpickling fails with:
AttributeError: module '__main__' has no attribute ...

With this change:

  • Functions with __module__ == "__main__" fall back to dynamic by-value pickling when pickle_main_by_ref=False.
  • Functions from __main__ still use stable code identifiers when pickle_main_by_ref=True.
  • Functions from non-__main__ modules continue using stable code identifiers regardless of pickle_main_by_ref.

Follow-up Work

Once this PR is merged and imported downstream, a follow-up change will be needed in the downstream runner to set CloudPickleConfig.pickle_main_by_ref = False whenever workers do not share the driver's __main__ module (for example, when notebook mode is enabled or a separate worker/manager binary is configured). That follow-up relies on importing this change first: without the __main__ + pickle_main_by_ref check here in cloudpickle.py, setting pickle_main_by_ref = False in the runner still falls through into _stable_identifier_function_reduce and emits __main__.* identifiers. After this import lands, setting pickle_main_by_ref = False will fall back to by-value pickling for __main__ while keeping stable code identifiers enabled for all non-__main__ modules.

Testing

  • Added test_stable_identifier_respects_pickle_main_by_ref in sdks/python/apache_beam/internal/cloudpickle_pickler_test.py.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

This PR skips stable code identifier pickling for functions in __main__ when pickle_main_by_ref is False, preventing unpickling failures when __main__ differs across processes, and adds a corresponding unit test. The core logic change in cloudpickle.py is sound, though there are a couple of issues in the newly added test (unrestored global state mutation on __main__ and mixed indentation that violates Beam's linting rules).

🔍 General Feedback

  • In cloudpickle_pickler_test.py, mutating sys.modules['__main__'] without cleanup can leak state into subsequent tests if an assertion fails or after the test finishes.
  • Ensure the modified files adhere to Apache Beam's 2-space indentation style so yapf / lint checks pass.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

💬 Inline Findings

  • sdks/python/apache_beam/internal/cloudpickle_pickler_test.py (line 223): 🟡 Medium: sys.modules['__main__'].local_main_fn is mutated directly and only deleted at the end of the test. If an assertion fails before delattr, local_main_fn remains attached to __main__ for subsequent tests (and if local_main_fn already existed on __main__, it would be overwritten and deleted). Wrap the attribute assignment and cleanup in a try...finally block or use unittest.mock.patch.object(sys.modules['__main__'], 'local_main_fn', local_main_fn, create=True).

  • sdks/python/apache_beam/internal/cloudpickle_pickler_test.py (line 216): 🟢 Low: test_stable_code_identifier_skips_main_when_not_by_ref uses 4-space indentation for the method declaration and 2-space indentation for its body (6 spaces total), whereas Apache Beam Python files and the rest of CloudpicklePicklerTest use 2-space indentation (2 spaces for methods, 4 spaces for method bodies). This will cause formatter/linter checks (yapf) to fail.

  def test_stable_code_identifier_skips_main_when_not_by_ref(self):

@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm for label python.
Backup reviewer: shunping (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@kristynsmith

Copy link
Copy Markdown
Contributor Author

R: @claudevdm @praneetnadella

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant