kristynsmith opened a new pull request, #41859:
URL: https://github.com/apache/beam/pull/41859

   
   
   ### 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`.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to