Skip to content

[FEA]: Add context managers to objects where resource cleanup is required #1719

@mdboom

Description

@mdboom

Is this a duplicate?

Area

cuda.core

Is your feature request related to a problem? Please describe.

#1711 illustrates a number of places where users are required to perform explicit cleanup of objects, by updating the examples to use try/finally and performing cleanup within the finally.

#1586 is related and probably a part of this work, but not as broad as this.

Describe the solution you'd like

We should implement context managers on any objects that need an explicit close or other resource revocation.

This includes at least Graph, GraphBuilder, Stream, possibly others.

Once these context managers are added, we should update any examples/docs/tests to use them.

Describe alternatives you've considered

I have read of resistance to this idea elsewhere based on the performance issues with contextlib.contextmanager, and if we provide them, users will use them over manual resource deallocation and pay the performance penalty everywhere.

However, context managers as a general concept (implemented with __enter__ and __exit__) do not have a major performance problem relative to correctly written code with a try/finally, as far as I know. I'm happy to be corrected on that, if there is in fact a performance problem vs. /correct/ code.

I think supporting context managers would be a major ergonomic improvement over manual tracking of context or even try/finally (which doesn't compose as well as with).

Additional context

No response

Metadata

Metadata

Assignees

Labels

cuda.coreEverything related to the cuda.core moduleenhancementAny code-related improvementsexampleImprovements or additions to code examples

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions