Skip to content

Conversation

Flamefire
Copy link
Contributor

@Flamefire Flamefire commented Mar 17, 2025

The type that is cached is a dictionary containing the EasyConfig in the "ec" key. Hence we end up invoking dict.copy not ec.copy

This results in not making independent copies and hence modifications affect all references even those gotten later

Revert 3836140 which introduced the issue

Our current EasyConfig.copy method effectively parses the EasyConfig text again invoking e.g. the parse_hook again.

I guess __deepcopy__ needs to be more smart than this to not also miss attributes set after the construction. Hence I left that out and just used ec.copy() as was likely intended by the above commit. It just needs to be done on the EasyConfig instance while the dict itself can be deep-copied if it doesn't contain the easyconfig.

The `__deepcopy__` needs to be very generic to avoid misleading results
by not copying all (possibly changed) attributes.
So leave that alone and use `ec.copy()` on the `'ec'` key directly in
the cache code.
@boegel boegel added this to the release after 5.1.1 milestone Jul 3, 2025
@Flamefire
Copy link
Contributor Author

Rebased

@boegel boegel changed the title Make sure cached ECs don't get modifed Make sure cached easyconfigs don't get modifed Jul 30, 2025
@boegel boegel added the bug fix label Jul 30, 2025

def _copy_ec_dict(easyconfig):
"""Copy an easyconfig dict as (initially) parsed"""
ec = easyconfig.pop('ec') # Don't copy this via deepcopy
Copy link
Member

Choose a reason for hiding this comment

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

Comment should be clarified: why not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done: We need ec.copy

"""Copy an easyconfig dict as (initially) parsed"""
ec = easyconfig.pop('ec') # Don't copy this via deepcopy
try:
new_easyconfig = copy.deepcopy(easyconfig) # Copy the rest of the dict
Copy link
Member

Choose a reason for hiding this comment

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

Can you clarify why this is in a try?

What if this fails? Then new_easyconfig will be undefined, and we'll get a nasty crash, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a try-finally. So it will still propagate all errors. The only difference is that in all cases ec (removed above) is put back to the original easyconfig

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.

2 participants