Skip to content

Conversation

@graingert
Copy link
Member

@graingert graingert commented Mar 22, 2025

Fixes #47

currently unwraping exceptions leaves a refcycle that keeps the entire traceback and any locals alive. This PR resolves that by clearing the error field when the Error is unwrapped.

For the previous behaviour of Value (eg in trio) if you receive a large bytes object in a Value that bytes object stays alive long after it's needed. This PR resolves that by clearing the value field when the Value is unwrapped.

this is a breaking change and should probably be released with a major version bump, eg outcome==2.0.0

@graingert graingert force-pushed the invalid-objects-on-unwrap branch from 7c22bcb to 20c75bf Compare March 22, 2025 11:36
@graingert graingert force-pushed the invalid-objects-on-unwrap branch from 49cd098 to 4d40df0 Compare March 22, 2025 11:44
@TeamSpen210
Copy link
Contributor

This might need to be a new method for backwards compatibility, since the attributes are public, users would be able to still read them after unwrapping. Unless we're fine with that breaking change.

@graingert
Copy link
Member Author

could just call it InvalidatingOutcome, InvalidatingError and InvalidatingValue

@TeamSpen210
Copy link
Contributor

A parallel class hierarchy seems problematic, they'd be incompatible with the current ones. More thinking like unwrap_and_clear(), that's all we need.

@CoolCat467
Copy link
Member

Would using a weak reference not be acceptable?

@graingert
Copy link
Member Author

Would using a weak reference not be acceptable?

I think that would be more complicated and it would be odd that unwrapped values are sometimes there sometimes not and not everything can be weakref'd

Copy link

@richardsheridan richardsheridan left a comment

Choose a reason for hiding this comment

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

I'm +1 on "just delete the attribute on unwrap". It's understandable and minimal and the only issue is backwards compatibility, so I'd say slap a unwrap_and_clear() method on version 1.4.0 and make a 2.0.0 release for the cleaner implementation to free more users from refcycles.

@RazerM
Copy link
Member

RazerM commented Apr 4, 2025

I'm -1 on deleting public attributes during unwrap. I don't like that accessing an attribute/property may raise, but if we go that route then I'd much prefer AlreadyUsedError over AttributeError.

The way I've always used outcome is to only .unwrap() or .send(), so I would be fine with an outcome 2.0 that doesn't have the attributes, but I know Trio does use them.

@graingert
Copy link
Member Author

I was off sick and didn't have time to focus on this PR, I'd like to pick it back up again

@graingert graingert force-pushed the invalid-objects-on-unwrap branch from 856bb3c to 6a1174b Compare January 29, 2026 11:31
@graingert graingert force-pushed the invalid-objects-on-unwrap branch from 6a1174b to 47e6abd Compare January 29, 2026 11:39
@graingert graingert force-pushed the invalid-objects-on-unwrap branch from 47e6abd to 2606efb Compare January 29, 2026 11:39
@graingert graingert force-pushed the invalid-objects-on-unwrap branch from 62a114b to fdbd48d Compare January 29, 2026 12:21
@graingert graingert marked this pull request as ready for review January 29, 2026 12:26
@TeamSpen210
Copy link
Contributor

I do wonder, since this is a breaking change, maybe it'd be better to make it entirely breaking - change the properties to peek_value() and peek_error() or something. That way you get type checker or runtime errors at all call sites. Though it would cause a bit of churn in cases where users only accessed attributes and never unwraped. So not sure.

@graingert graingert requested a review from CoolCat467 January 30, 2026 07:55
Copy link
Member

@CoolCat467 CoolCat467 left a comment

Choose a reason for hiding this comment

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

I don't see any immediate issues. Would raise the possibility of sharing peek and return, return/unwrap just being peek and then unset, but as is is still fine.

@graingert
Copy link
Member Author

ok let's add peek

@graingert graingert changed the title remove reference to value/error when unwrapping outcome remove reference to value/error when unwrapping outcome, and add .peek() Jan 30, 2026
@graingert graingert changed the title remove reference to value/error when unwrapping outcome, and add .peek() remove reference to value/error when unwrapping outcome, and add .peek() method Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

leaving .error and .value alive after unwrap results in unnecessary refcycles or potentially large values remaining alive longer than needed

5 participants