Repository navigation
Stable code identifier pickling - #41859
kristynsmith wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
📋 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, mutatingsys.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_fnis mutated directly and only deleted at the end of the test. If an assertion fails beforedelattr,local_main_fnremains attached to__main__for subsequent tests (and iflocal_main_fnalready existed on__main__, it would be overwritten and deleted). Wrap the attribute assignment and cleanup in atry...finallyblock or useunittest.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_refuses 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 ofCloudpicklePicklerTestuse 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):
|
Assigning reviewers: R: @damccorm for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment |
Summary
Update
CloudPickler._stable_identifier_function_reduceinsdks/python/apache_beam/internal/cloudpickle/cloudpickle.pyto fall back to_dynamic_function_reducewhen pickling a function from__main__withconfig.pickle_main_by_ref=False.Motivation
When
CloudPickleConfig.get_code_object_paramsis set (stable code identifier pickling) andpickle_main_by_ref=False(e.g., interactive notebooks or setups where workers run a different binary than the driver),_function_reducechecks_should_pickle_by_reference(which returnsFalsefor__main__) and then falls through to_stable_identifier_function_reduce.Previously,
_stable_identifier_function_reducedid not checkconfig.pickle_main_by_ref, soget_code_object_identifieremitted__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:
__module__ == "__main__"fall back to dynamic by-value pickling whenpickle_main_by_ref=False.__main__still use stable code identifiers whenpickle_main_by_ref=True.__main__modules continue using stable code identifiers regardless ofpickle_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 = Falsewhenever 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_refcheck here incloudpickle.py, settingpickle_main_by_ref = Falsein the runner still falls through into_stable_identifier_function_reduceand emits__main__.*identifiers. After this import lands, settingpickle_main_by_ref = Falsewill fall back to by-value pickling for__main__while keeping stable code identifiers enabled for all non-__main__modules.Testing
test_stable_identifier_respects_pickle_main_by_refinsdks/python/apache_beam/internal/cloudpickle_pickler_test.py.