jackfirth / racket-disposable

An experimental Racket library providing an abstraction for values associated with external resources that allows automatic resource pooling, per-thread virtual construction, and monadic composition
Apache License 2.0
7 stars 0 forks source link

Pooled disposables should not allow cross-custodian sharing #11

Closed jackfirth closed 7 years ago

jackfirth commented 7 years ago

This ensures shutting down one custodian doesn't affect resources used by threads in another custodian. The pool should weakly map custodians to a list of in-use values (since more than one thread in the same custodian may access the disposable). The mapping should be weak to allow custodians to be garbage collected after shutdown.

jackfirth commented 7 years ago

This actually isn't always necessary. Cross-custodian sharing is only a problem when the disposable's lifetime is tied to the lifetime of a value managed by a custodian, such as a TCP port. A temporary file, for example, can be shared between custodians because the file ports used to create and delete the file are only used during allocation and deallocation; they're not kept open while the file is in use. So this should be a keyword option of disposable-pool that by default prevents cross-custodian sharing, with the option to enable it for resources where the pool creator knows they don't hold open any values managed by custodians.

jackfirth commented 7 years ago

This turns out to not be necessary at all. Because values in the pool are created in the context of a manager thread spawned when the pool is created, they all share the custodian that was current when the pool was created. The http package does something different because it essentially makes a pool per each scheme, host, and port triplet. Because these pools are constructed when the function is called, it would be the responsibility of whatever code cached the mapping from scheme+host+port to pools to only cache values on a per-custodian basis.