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

Deepcopy of objects with attached finalizers. #52037

Open
vchuravy opened this issue Nov 5, 2023 · 2 comments
Open

Deepcopy of objects with attached finalizers. #52037

vchuravy opened this issue Nov 5, 2023 · 2 comments
Labels
design Design of APIs or of the language itself speculative Whether the change will be implemented is speculative

Comments

@vchuravy
Copy link
Member

vchuravy commented Nov 5, 2023

I meet @kbarros the other day and he recounted a story where he lost a lot of time on a tricky corner-case of deepcopy.

We often use mutable structs with pointers to underlying foreign memory and attached finalizers for resource management. Users can thus easily get into the situation where they perform a deep copy of the struct and then GC frees the original copy, runs the finalizer and the user access the now release foreign memory through new object and has to deal with obscure crashes.

We already deal with these situations on serialization, where we null out raw pointers. Once could think of deep copy as effectively doing a deserialize(serialize(obj)) and thus I think it would be justifiable for deep copy to zero out raw pointers as well, and requiring folks to write a deepcopy_internal method that deals with this situation.

Due to the behaviour of serialization we already have users who check if the pointer is null and then either provide a user friendly error, or lazily re-initialize the foreign memory.

@vtjnash mentioned on Slack that there was already an issue about this, but I was unable to find it.

@vchuravy vchuravy added speculative Whether the change will be implemented is speculative design Design of APIs or of the language itself labels Nov 5, 2023
@jariji
Copy link
Contributor

jariji commented Nov 6, 2023

Imo deepcopy is more trouble than it's worth and should probably be deprecated if not removed. #42796 (comment)

@kbarros
Copy link

kbarros commented Nov 6, 2023

Thanks for starting the conversation @vchuravy. Here is an example bug that can arise from naive use of deepcopy: JuliaMath/FFTW.jl#261. Here was the confusion prior to the correct diagnosis: #48722.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

3 participants