10 ms·
This is to some extent a Python-specific problem. In most languages with destructors, you're guaranteed a destructor call when a local variable's scope is exite
by Animats 6y ago
This is to some extent a Python-specific problem. In most languages with destructors, you're guaranteed a destructor call when a local variable's scope is exited. But not Python.
Python isn't RAII, it's an explicit close language. That's what "with" is for. Python's "with" clauses, unusually, play well with exceptions at scope exit. Nested "with" clauses can fault out with a close exception and still have everything unwind properly. Very few languages get that right. Python probably has the most useful exception system around.
Calling destructors from the garbage collector has a whole other set of problems. Suppose you're writing to a network and the network connection fails during close. Now you can be stuck in the garbage collector calling the destructor waiting for the network to time out. I'm not sure how garbage-collected Python implementations handle this. C# can potentially get into that state, too. Anybody know?
Raising an exception in a C++ destructor is usually not happy-making. Microsoft tried to make that work in Managed C++, which has "re-animation". A destructor can pass a reference to its own object outside of the destructor, which "re-animates" the object. Turned out not to be a good idea.
I would have liked to see exceptions in Rust, which has enough static ownership analysis to determine whether a destructor is trying to make something outlive its lifetime. Most of the problems with destructors vs exceptions involve scoping issues. If such errors are caught at compile time, exceptions are better behaved.
- tsimionescu 6y agoIn most managed-memory languages, try...finally (and using/try-with-resources which are based on it) handle exceptions just fine. The only problem that sometimes occurs is that exceptions thrown by a finally handler override the exception being bubbled, which can be bad if you wanted to handle that exception later on, but otherwise the resources get cleared properly. D has better behavior here, where throwing an exception on this path will automatically keep track of many in-flight exceptions. Also, the problem of finalizers blocking is partly handled by the fact that finalizers are not run by the GC threads, but by a separate finalizer thread. Still, if a finalizer blocks, eventually you will run out memory, as all objects which require finalization will be pooling up. Also, the re-animation behavior you describe is common to finalizers in all GC languages which expose them, it's not in any way specific to Managed C++ (C++/CLR). It is simply a consequence of how finalization works: when an object which requires finalization is eligible for GC, it is not collected, but moved to the finalization queue. Objects in that queue are still live objects. When the finalizer thread successfully runs the finalizer of such an object, it is simply removed from the finalization queue. If it's finalizer has added back a reference from some other GC root to the object, then it will still stay live. The only special property is that an object can't be finalized twice, so even if its finalizer attaches it to the live object graph once, when it becomes unreachable again it will be collected, it will not be put back in the finalization queue, it will be directly collected. Basically, I don't think there is too much chance to find a valid use for this behavior, but it is very hard to prevent it without complicating the language significantly.
- twic 6y ago> Basically, I don't think there is too much chance to find a valid use for this behavior, but it is very hard to prevent it without complicating the language significantly. One approach is to provide a mechanism for cleanup that doesn't require the object to still be alive. Java has phantom references for this: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ref/PhantomReference.html https://docs.oracle.com/en/java/javase/11/docs/api/java.base... https://stackoverflow.com/a/43659951/116639 https://stackoverflow.com/a/43659951/116639 And i learn that Smalltalk introduced the "ephemeron", which is now present in some other languages: https://en.wikipedia.org/wiki/Ephemeron https://en.wikipedia.org/wiki/Ephemeron
- tsimionescu 6y agoSure, there are other possible cleanup mechanisms, and I believe Java is depreciating finalizers in favor of those other mechanisms. My point was only about finalizers themselves: a language with the finalizer mechanism I was describing above would need to add a lot of features to prevent finalized objects from becoming reachable again.
- masklinn 6y ago> One approach is to provide a mechanism for cleanup that doesn't require the object to still be alive. Java has phantom references for this: Java has pretty much deprecated finalizers due to how non-deterministic their run is. Modern java uses try-with-resources blocks which… are the exact same thing as Python's with.
- patrec 6y ago> That's what "with" is for. Python's "with" clauses, unusually, play well with exceptions at scope exit. [...] Very few languages get this right. I wonder if I'm missing something subtle here. What languages with garbage collection, exceptions and higher order functions get this wrong? I'd expect this to work fine in common lisp, smalltalk, ruby, or pretty much anything else that offers a similar idiom -- either by macros or by passing thunks.
- deleted 6y ago[deleted]