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

Use ResourceContexts.jl for resource handling #12

Merged
merged 5 commits into from
May 28, 2021

Conversation

c42f
Copy link
Contributor

@c42f c42f commented May 13, 2021

This would use the context passing mechanism in https://github.com/c42f/ResourceContexts.jl to manage resources.

This allows datasets to be opened in the REPL without risking their underlying resource being freed, thereby fixing #8. The existing open()-based API is retained but made safe by attaching finalizers to the returned types (as a result, Blob and BlobTree had to become mutable which was unfortunate).

In addition, the API from ResourceContexts.jl can be used, though this is kept as an implementation detail for now. This is how it looks to open a resource in the REPL using ResourceContexts.jl:

julia> @! open(dataset("TrueFX"))
📂 Tree  @ /home/chris/.julia/datasets/TrueFX
 📄 AUDUSD-2016-01.zip
 📄 AUDUSD-2016-02.zip
 📄 AUDUSD-2016-03.zip
 📄 CADJPY-2020-01.zip

TODO

  • Register Contexts.jl
  • Cleanup WIP code
  • Tests
  • Docs

@c42f c42f marked this pull request as draft May 13, 2021 06:19
src/DataSets.jl Outdated
Comment on lines 544 to 555
# This is pretty delicate — it won't work if the compiler
# retains a reference to `x` due to its existence in local
# scope.
#
# Likely, it depends on
# - inlining the current closure into open()
# - Assuming that the implementation of open() itself
# doesn't keep a reference to x.
# - Assuming that the compiler (or interpreter) doesn't
# keep any stray references elsewhere.
#
# In all, it seems like this can't be reliable in general.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StefanKarpinski this is the technical reason that attempting to use finalizers to turn the existing do-based API into a finalizer based one won't work. This could certainly be hacked around by a breaking change to the DataSets storage driver API and converting some structs to be mutable in order to attach finalizers.

But more generally, there's a need to have "a place to hold references to resources" which can't be solved by finalizers and are solved by passing a resource context.

c42f added 2 commits May 17, 2021 22:47
The trick here is to root the resource context within the finalizer. It
only works for mutable objects, or objects with mutable fields, where a
finalizer can be attached by proxy. But that's already pretty good.
@c42f c42f force-pushed the cjf/context-resource-handling branch from 8ab21c9 to 7ac2700 Compare May 17, 2021 12:47
Of course immutable struct can't be finalized, but also there's no way
around this; despite initial apperances, _root_resource is semantically
unsound in general. So just bite the bullet and make Blob and BlobTree
mutable.

Uses the new ResourceContexts.detach_context_cleanup to transform
context-based resource handling into finalizer-based resource handling.
@c42f c42f changed the title WIP: Use Contexts.jl for resource handling Use ResourceContexts.jl for resource handling May 28, 2021
@c42f c42f marked this pull request as ready for review May 28, 2021 21:43
Updated to be more explicit the role of the unscoped vs scoped forms of
open.
@c42f c42f merged commit 2dd797b into master May 28, 2021
@delete-merged-branch delete-merged-branch bot deleted the cjf/context-resource-handling branch May 28, 2021 23:52
@c42f
Copy link
Contributor Author

c42f commented May 29, 2021

To record some slack discussion — we decided that ResourceContexts.jl is too experimental to be the main API for DataSets, but I figured out how to use it as a backend for managing resources needed by unscoped open() by attaching the context to the finalizer of the returned object.

Likely ResourceContexts.jl will become part of the API for storage backends, but it's a strictly optional API for users (currently not even mentioned in the docs).

@c42f c42f mentioned this pull request Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant