You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
File handles left open on program termination #433
It would be preferable if we kept track of the handles and closed them explicitly in __del__ rather than leaving them for the interpreter to (hopefully) clean up. This would normally be pretty easy, but all of our calls to read from attributes in the device subclasses look like this:
In other words, there is no centralized location where we store the file handles. What do others think? I would be in favor of moving the cache a level down into the device base class.
There is the alternative option of configuring the unit tests to ignore warnings... but that feels a bit wrong.
Side-note: Autogen would come in really handy in this case! 😆
withLargeMotor(OUTPUT_A) asm, ColorSensor() ascs:
# do stuff with m and cs, clean up on exit
but that would be inconvenient for use if one wants to store a device as a class field. My understanding is that what we have now is considered acceptable in python, but I am no expert here.
I am not sure what the problem with centralized file caching was, but we had it implemented at some point and moved away for some reason. 👻
My recollection was that we had a __del__ implementation which would stop motors on termination, and that had some obvious issues, especially because we were attempting to go through the file file write process. I am guessing that closing file handles wouldn't be problematic, but I'm not sure. My thinking is that closing file handles explicitly is nice to have for sanity, but it doesn't affect program behavior if that code doesn't run when we expected it to. Meanwhile, I'm having a tough time deciphering why removed the file cache...
My recollection was that we had a __del__ implementation which would stop motors on termination
That was the intention, but __del__ only runs on garbage collection, so it practice, it is rarely, if ever, called since the motor objects stay alive for the entire program. So, it would be "correct" to close any open file handles in __del__, but it most likely won't fix your unit test.
Even if you manually run the garbage collector, there is no guarantee that __del__ will be called. It could only happen if there are no references to the object remaining. If you want to actually close all file handles, then you should implement a close() method that does so.
Activity
ddemidov commentedon Nov 21, 2017
I think we discussed this somewhere, and the problem with file handles was that python does not have proper class destructors. For example, see https://stackoverflow.com/questions/35488705/is-relying-on-del-for-cleanup-in-python-unreliable.
We could probably come up with an interface like
but that would be inconvenient for use if one wants to store a device as a class field. My understanding is that what we have now is considered acceptable in python, but I am no expert here.
I am not sure what the problem with centralized file caching was, but we had it implemented at some point and moved away for some reason. 👻
ddemidov commentedon Nov 21, 2017
#109, #96
ddemidov commentedon Nov 21, 2017
#162 (4a87384)
WasabiFan commentedon Nov 21, 2017
My recollection was that we had a
__del__implementation which would stop motors on termination, and that had some obvious issues, especially because we were attempting to go through the file file write process. I am guessing that closing file handles wouldn't be problematic, but I'm not sure. My thinking is that closing file handles explicitly is nice to have for sanity, but it doesn't affect program behavior if that code doesn't run when we expected it to. Meanwhile, I'm having a tough time deciphering why removed the file cache...dlech commentedon Nov 21, 2017
#155 (comment)
dlech commentedon Nov 21, 2017
That was the intention, but
__del__only runs on garbage collection, so it practice, it is rarely, if ever, called since the motor objects stay alive for the entire program. So, it would be "correct" to close any open file handles in__del__, but it most likely won't fix your unit test.dwalton76 commentedon Sep 3, 2018
What if we close them in
__del__and have the test cases callgc.collect()?dlech commentedon Sep 4, 2018
Even if you manually run the garbage collector, there is no guarantee that
__del__will be called. It could only happen if there are no references to the object remaining. If you want to actually close all file handles, then you should implement aclose()method that does so.suppress unittest ResourceWarning messages (ev3dev#556)