Skip to content

Python::assume_gil_acquired() can infer static lifetime. #800

Closed
@davidhewitt

Description

@davidhewitt

As per the comment on #797 we should probably remove Python::assume_gil_acquired(), because it can give you a Python<'static> thanks to the lifetime inference. This is almost certainly unsound.

I think it probably explains this crash #653

See this playground for a minimal example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=213173005786e15199cf0312079c3d72

A function assume_gil_acquired() would still be useful, but I think it needs to return a type without a lifetime, so that there is no risk of incorrect inference. For example, something like this:

struct AssumeGILAcquired(Unsendable);

fn assume_gil_acquired() -> AssumeGilAcquired { ... }

impl AssumeGILAcquired {
    fn py(&self) -> Python { ... }
}

/// later on:
let gil = assume_gil_acquired();
let py = gil.py();

In the above example, the Python lifetime is tied exactly to the lifetime of the AssumeGILAcquired token, so we can avoid 'static lifetime problems.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions