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

Dynamic borrow checking for stackrefs #131527

Open
mpage opened this issue Mar 20, 2025 · 0 comments
Open

Dynamic borrow checking for stackrefs #131527

mpage opened this issue Mar 20, 2025 · 0 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@mpage
Copy link
Contributor

mpage commented Mar 20, 2025

Feature or enhancement

Proposal:

#130708 introduces stackrefs whose lifetime must be shorter than the lifetime of another stackref. We should verify this property in the debug build of stackrefs: warn anytime a stackref with outstanding borrowed references is closed.

Here is a sketch of a possible implementation:

  • Add a counter, borrows, to each stackref that records the number of borrows.
  • Add a borrowed_from field to each stackref that records the index of the stackref it was borrowed from, if any. This field is preserved by PyStackRef_DUP.
  • PyStackRef_Borrow(from) sets the the borrowed_from field on the returned stackref and increments the borrows field on from. We should follow chains of borrows until we reach the unborrowed reference at the head.
  • PyStackRef_CLOSE on a borrowed stackref decrements the borrows field on the borrowed-from stackref.
  • PyStackRef_CLOSE warns if borrows is non-zero.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

@mpage mpage added type-feature A feature request or enhancement interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 20, 2025
@mpage mpage self-assigned this Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant