Skip to content

context managers for KernelManager and KernelClient #1080

@minrk

Description

@minrk

KernelManager and KernelClient have nontrivial amounts of resources (usually sockets, but sometimes also dedicated event loops) to cleanup. #1076 had some issues that were caused by lots and lots of leftover unclosed zmq contexts, sockets, and event loops. This is understandable, because we don't actually have easy APIs for cleaning up resources used by these objects.

We should have:

  1. easy single cleanup method on both KernelManager and KernelClient that cleanup everything:
  • sockets
  • context, if created
  • event loop, if created, for blocking implementations
  1. context manager methods that make it easier to ensure these things are cleaned up, even on exception

I suggest calling these cleanup methods .close(), just like most open-resource-having objects.

we have some cleanup in __del__, but relying on del is super unreliable and Python implementation-specific, and should really always throw a ResourceWarning that something wasn't closed properly, as is done with sockets, files, etc..

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