Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction to event slots #233

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

fcollonval
Copy link
Member

@fcollonval fcollonval commented Feb 17, 2025

Fixes #232
Fixes #234

Add transaction to event slots.

This ends up a bit more complex to be able to keep a reference to the transaction in the event, this PR introduced a BaseTransaction that only keeps reference to the document and the origin (independently of the rust object).
That ensures the introspection of the event at any time.

It also introduces a more robust way to maintain the mapping of Python origin and rust origin.

@fcollonval fcollonval added the enhancement New feature or request label Feb 17, 2025
fcollonval and others added 4 commits February 19, 2025 11:50
More robust handling of mapping between origin and its hashed value
@davidbrochart
Copy link
Collaborator

Thanks for the PR, I will have a look soon.

Comment on lines +37 to +38
if getattr(self, "_origin_hash", None) is not None:
self._doc._origins.remove(cast(int, self._origin_hash))
Copy link
Collaborator

@davidbrochart davidbrochart Feb 20, 2025

Choose a reason for hiding this comment

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

Why using getattr, since _origin_hash is set in __init__?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know why, but I got hit by missing _origin_hash in some cases. It may have to do with some inheritance pattern.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that it's something to investigate, instead of using getattr because we don't know why it doesn't work without. Otherwise that's going to lead to code that we don't understand and don't know how to maintain.

Comment on lines -78 to -80
origin_hash = self._txn.origin()
if origin_hash is not None:
del self._doc._origins[origin_hash]
Copy link
Collaborator

Choose a reason for hiding this comment

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

That was a very deterministic behavior, why did you move that part to the transaction's __del__ method?

Copy link
Member Author

Choose a reason for hiding this comment

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

If you leave the logic there, then the tests won't work. Because when requesting the transaction origin from the event object the entry in the doc._origins will be gone.

The usage of __del__ is not less deterministic (at least in CPython) and it is more consistent as Transaction.origin will always be correct while the transaction object is reference somewhere.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you leave the logic there, then the tests won't work. Because when requesting the transaction origin from the event object the entry in the doc._origins will be gone.

I think it's just a matter of dropping the transaction after removing the origin from the doc.

The usage of __del__ is not less deterministic

Yes it is, because now tests behave differently if running on CPython or PyPy.

@davidbrochart
Copy link
Collaborator

@fcollonval Could you explain the reasoning about adding a reference count to origins?

Copy link
Member Author

@fcollonval fcollonval left a comment

Choose a reason for hiding this comment

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

Thanks for the review @davidbrochart

Could you explain the reasoning about adding a reference count to origins?

If you create a helper model with methods like:

class MyDoc:

     def __init__(self, ...):
         self._doc = YDoc()
         self._id = "my-unique-id"
         
     def clear_things(self):
         with self._doc.transaction(origin=self._id):
             # Change the document

     def update_things(self):
         with self._doc.transaction(origin=self._id):
             self.clear_things()
             # Other document changes

Not using a counter will result in discarding the origin from the mapping too early.

Comment on lines -78 to -80
origin_hash = self._txn.origin()
if origin_hash is not None:
del self._doc._origins[origin_hash]
Copy link
Member Author

Choose a reason for hiding this comment

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

If you leave the logic there, then the tests won't work. Because when requesting the transaction origin from the event object the entry in the doc._origins will be gone.

The usage of __del__ is not less deterministic (at least in CPython) and it is more consistent as Transaction.origin will always be correct while the transaction object is reference somewhere.

Comment on lines +37 to +38
if getattr(self, "_origin_hash", None) is not None:
self._doc._origins.remove(cast(int, self._origin_hash))
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know why, but I got hit by missing _origin_hash in some cases. It may have to do with some inheritance pattern.

@davidbrochart
Copy link
Collaborator

Not using a counter will result in discarding the origin from the mapping too early.

I don't think it's the case if the transaction is dropped after removing the origin from the doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discarding origin is brittle Missing transaction in Python event
2 participants