Non-deterministic objects destruction in unit tests and demos
As a workaround to #390 we should avoid using explicit del
in tests/demos. There a lot of patterns like
xdmf = XDMFFile(mesh.mpi_comm(), filename)
xdmf.write(mesh)
del xdmf
del
does not ensure calling object destructor. So there should be
- either
xdmf.close()
, - or keeping
xdmf
in scope until a test function returns and teardown invokesgc.collect()
automatically.
Comments (10)
-
reporter -
del
just removes the reference, thus decreases the reference count. If the reference count reaches zero, the object is freed immediately (calling__del__
first if defined). In case of reference cycles, the object may be freed at some point when Python secondary garbage collector runs. If any of the objects in a reference cycle has__del__
defined, then the objects of that cycle are never freed because Python does not know in which order to run the destructors.with
statement is the Pythonic way of allocating and freeing resources. -
reporter If the reference count reaches zero, the object is freed immediately
Could you give some reference to this. I believe that this is not true which is why we are struggling with collective destructors causing problems. I thought that garbage collectors is allowed to deallocate the object any time it decides to do so (unless you call
gc.collect()
assuming there are no cycles). -
CPython implementation detail: CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the gc module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (so you should always close files explicitly).
-
reporter as soon as they become unreachable
is only vaguely interpreted as immediately.
Do not depend on immediate finalization of objects when they become unreachable
-
reporter - edited description
- changed title to Non-deterministic objects destruction in unit tests and demos
Mention demos too.
-
reporter - changed milestone to 2017.1
-
assigned issue to
Most of the explicit
del
instances taken care of by @trlandet. I will clean the rest afterwads. Implicit deletion (by becoming unreachable) is worse. Need to go through the tests line-by-line I guess. Typical casedef test_foo(): x = Vector() # Do something with x x = Vector() # original vector eventually deleted
-
reporter - changed milestone to 2017.2
Partial fixes for this in pull request #338 to be merged after 2017.1 release.
-
reporter - changed milestone to 2018.1
-
reporter - removed responsible
- removed milestone
- Log in to comment
Or equivalent to 1.
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: