Implement a model of resource ownership which allows apps to be used as context managers, triggering resource teardown on close #1324
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduce the '_resource_owner' internal protocol
Several resources in the SDK now define a tracking protocol to allow
resources to participate in a cascading
close()
via a GlobusApp.The design breaks down into two major sections.
Ownership Tracking
_resource_owner: BaseClient | None
is a new property of transports_resource_owner: GlobusApp | None
is a new property of token storage_resource_owner: GlobusApp | None
is a new property of clients_owned_clients: MutableSet[BaseClient]
is a new property of appsThese are the storage mechanisms being used. The
_owned_clients
set isdefined as a
weakref.WeakSet
, meaning that membership in the set willnot prevent garbage collection of the members (which implicitly removes
them).
Each resource "may know its owner" by having the
_resource_owner
assigned, and apps keep tabs on "owned clients" (which are otherwise
not associated).
The rules for when these are set are as follows:
_resource_owner
_owned_clients
_resource_owner
storage's
_resource_owner
Close Semantics
close()
, but it is still onlymeaningful for some -- the base version is a no-op
BaseClient._close()
is a new (private) method which closes theunderlying transport IFF the client is the owner
GlobusApp.close()
is a new (public) method which closes (_close()
)all apps owned by the client and any token storage owned by the client
GlobusApp
supports use as a context manager, withclose()
on exit