Skip to content

[Python] Skip attributes that raise during stable code identifier lambda search - #41003

Open
thelastbackspace wants to merge 1 commit into
apache:masterfrom
thelastbackspace:fix/41002-stable-id-getattr-errors
Open

thelastbackspace wants to merge 1 commit into
apache:masterfrom
thelastbackspace:fix/41002-stable-id-getattr-errors

Conversation

@thelastbackspace

Copy link
Copy Markdown

Fixes #41002

With stable code identifier pickling enabled, _search_module_or_class walks dir(node) and calls getattr on every name while looking for a lambda. If some unrelated attribute raises when read (a descriptor that only works on instances, or a PEP 562 lazy module attribute whose optional dependency is missing), that exception escaped get_code_object_identifier and cloudpickle_pickler.dumps failed, even though plain pickling of the same lambda works.

An attribute that can't be read can't be the lambda we're looking for, so the search now just skips it and keeps going. If nothing matches, it still returns None as before and cloudpickle falls back to pickling by value.

Testing:

  • Added test_lambda_search_skips_attributes_that_raise in code_object_pickler_test.py, using a class with an instance-only descriptor that sorts before the lambda in dir(). It fails with TypeError: only valid on instances without the change and passes with it.
  • pytest apache_beam/internal/code_object_pickler_test.py

Note: #40711 also touches this file; happy to rebase if that lands first.

…bda search

The lambda search in _search_module_or_class read every name from dir()
with getattr, so an unrelated attribute that raises on access (an
instance-only descriptor, a lazy PEP 562 module attribute) broke
cloudpickle dumps with stable code identifier pickling enabled.

Fixes apache#41002
@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Nov 4, 2026

@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 fixes #41002 by catching Exception when reading attributes via getattr(node, name) during the lambda search in _search_module_or_class, preventing unrelated descriptors or lazy module attributes from crashing stable code identifier pickling. The implementation and unit test are clean, minimal, and well-targeted.

🔍 General Feedback

  • Catching Exception (rather than BaseException) and continuing the loop in _search_module_or_class is the right approach and preserves fallback behavior when no matching lambda is found.
  • Minor test portability nit in code_object_pickler_test.py regarding module name prefix when executed directly as __main__.

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

def test_lambda_search_skips_attributes_that_raise(self):
fn = ClassWithLambdaAndRaisingAttribute.fn
path = code_object_pickler.get_code_object_identifier(fn)
self.assertEqual(

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.

🟢 Low: Since ClassWithLambdaAndRaisingAttribute is defined in this test module, fn.__module__ will be '__main__' if the test file is executed directly via python code_object_pickler_test.py. Using f"{prefix}." (defined at line 141 as prefix = __name__) keeps this consistent with test_cases above.

Suggested change
self.assertEqual(
f"{prefix}."

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.
Backup reviewer: tvalentyn (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).

@tvalentyn

Copy link
Copy Markdown
Contributor

R: @praneetnadella @kristynsmith

@github-actions

github-actions Bot commented Nov 5, 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.

[Bug]: Stable code identifier pickling crashes cloudpickle.dumps when a sibling attribute raises on access

2 participants