Skip to content

File handles left open on program termination #433

Closed

Description

@WasabiFan

Currently, we store handles to the attribute files we open for quick access. This means that we get a bunch of warning spew like this from the tests:

/opt/python/3.4.6/lib/python3.4/unittest/case.py:580: ResourceWarning: unclosed file <_io.FileIO name='/home/travis/build/ev3dev/ev3dev-lang-python/tests/fake-sys/arena/lego-sensor/sensor0/address' mode='rb'>

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:

self._address, value = self.get_attr_string(self._address, 'address')

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! 😆

Activity

  1. ddemidov commented on Nov 21, 2017

    @ddemidov
    Member

    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

    with LargeMotor(OUTPUT_A) as m, ColorSensor() as cs:
        # 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. 👻

  2. ddemidov commented on Nov 21, 2017

    @ddemidov
    Member
  3. ddemidov commented on Nov 21, 2017

    @ddemidov
    Member
  4. WasabiFan commented on Nov 21, 2017

    @WasabiFan
    MemberAuthor

    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...

  5. dlech commented on Nov 21, 2017

    @dlech
    Member

    Meanwhile, I'm having a tough time deciphering why removed the file cache...

    #155 (comment)

  6. dlech commented on Nov 21, 2017

    @dlech
    Member

    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.

  7. dwalton76 commented on Sep 3, 2018

    @dwalton76
    Collaborator

    What if we close them in __del__ and have the test cases call gc.collect()?

  8. dlech commented on Sep 4, 2018

    @dlech
    Member

    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.

  9. self-assigned this
    on Nov 2, 2018
  10. added a commit that references this issue on Dec 20, 2025
    9c95277
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions