Skip to content

python3: issue with __del__ when script terminates #96

Closed

Description

@victorkamensky

After picking up Denis's 'Restore Python3 compatibility' fix,
with python3 I observed weird issue related to del behavior.

It could be related to some race condition when python3
goes down. In one case 'isabs' function from posixpath.py becomes
None. So on call chain triggered by Motor del -> stop ...
it fails. In other case see below 'open' function disappeared.

(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test.py
import ev3dev.ev3 as ev3
m = ev3.LargeMotor("outB")
exit()
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test.py
Exception ignored in: <bound method LargeMotor.__del__ of <ev3dev.core.LargeMotor object at 0xb6a14b30>>
Traceback (most recent call last):
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 221, in **del**
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 670, in stop
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 235, in command
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 178, in set_attr_string
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 166, in _set_attribute
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/posixpath.py", line 357, in abspath
TypeError: 'NoneType' object is not callable

With some slight variation with the test issue disappears:

(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test1.py
import ev3dev.ev3 as ev3
def main():
    m = ev3.LargeMotor("outB")
main()
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test1.py
(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test2.py
import ev3dev.ev3 as ev3
def main():
    m = ev3.LargeMotor("outB")
    exit()
main()
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test2.py
(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test3.py
import ev3dev.ev3 as ev3
m = ev3.LargeMotor("outB")

(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test3.py
(ev3dev_py34)root@ev3dev:~/projects/v/test2# 

It seems that it could be races in the code in other case
'open' name disappeared:

(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test.py
Exception ignored in: <bound method LargeMotor.__del__ of <ev3dev.core.LargeMotor object at 0xb6a2eb30>>
Traceback (most recent call last):
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 221, in **del**
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 670, in stop
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 235, in command
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 178, in set_attr_string
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 166, in _set_attribute
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 88, in write
  File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 74, in file_handle
NameError: name 'open' is not defined

When the same tried with python2 everything looks fine.

By googling info about del I found this:

https://docs.python.org/2/reference/datamodel.html#object.__del__

The pink section pretty much explains the issues I observe,
which begs the question whether it is good idea to have del
method in the first place.

On one hand desire to stop all motors when cotrolling process exits,
especially abnormally, is well understood. But needless to say that
if above condition hit when motor was running it won't stop anyway.

I wonder whether ev3dev infra (file interface) can provide more solid
emergency stop mechanism. For example stop could be trigger on close
of some special file, like /sys/class/tacho-motor/motor?/emergency_stop.
If such file would be opened, ev3dev system will stop motor when file
closes. In this case python can just open the file in Motor contructor
and file will be automatically closed when python exists, that will
trigger motors stop. It would stop the motors even if process
explicitly killed.

Other language bindings can use the same mechanism.

Activity

  1. rhempel commented on Dec 6, 2015

    @rhempel
    Member

    That is a good idea! If I understand correctly, by opening /sys/class/tacho-motor/motor?/emergency_stop we set a hook so that if the process crashes and Linux manages to close any open file descriptors, we'd need to catch that close and reset the motor.

  2. dlech commented on Dec 6, 2015

    @dlech
    Member

    sysfs has no hooks for the opening and closing of files, so an emergency_stop attribute could not work as described.

    Brickman already stops all motors when a process that was run from the file browser ends. You could make a simple script to run programs from a command line that stops all motors when done as well. e.g. run-ev3dev python myprogram.py. This script could also take care of console switching issues. See https://gist.github.com/dlech/4c9b268571af59cb7bb4 and ev3dev/ev3dev-tools#1.

  3. ddemidov commented on Dec 6, 2015

    @ddemidov
    Member

    You could make a simple script to run programs from a command line

    This wrapper script could even be put into shebang line. This works, for example:

    wrapper.sh

    #!/bin/bash
    echo running $2 with $1
    $1 $2
    echo done!

    hello.py

    #!./wrapper.sh /usr/bin/python
    print("Hello World!")

    output of ./hello.py:

    running ./hello.py with /usr/bin/python
    Hello World!
    done!
    

    We could even introduce a system-wide script for this.

  4. rhempel commented on Jan 2, 2016

    @rhempel
    Member

    @ddemidov - is this better handled in the README.rst file - ie a "Best Practices" section?

  5. ddemidov commented on Jan 3, 2016

    @ddemidov
    Member

    @rhempel, I agree. We could remove the motor destructors, and put a warning into the README with a couple of solutions. First, running from brickman does not have this problem. Second, this helper script works:

    $ cat saferun.sh 
    #!/bin/bash
    $*
    for f in /sys/class/tacho-motor/*/command; do
        echo reset > ${f}
    done
    
    $ cat hello.py
    #!./saferun.sh /usr/bin/env python
    print('hello')
    
  6. ddemidov commented on Jan 3, 2016

    @ddemidov
    Member

    There are a couple of other places __del__ is currently used. Namely, we use it in FileCache to close the cached file handles, and in FbMem to close the framebuffer handle. I am not sure how to get rid of those while keeping the functionality and not leaking resources. Any suggestions are welcome.

  7. dlech commented on Jan 3, 2016

    @dlech
    Member

    Add a close or dispose function to those classes?

  8. ddemidov commented on Jan 4, 2016

    @ddemidov
    Member

    Add a close or dispose function to those classes?

    You mean to be called manually? How would that help us to not leak anything? The FileCache and FbMem classes are only used internally and not exposed to the user.

  9. dlech commented on Jan 4, 2016

    @dlech
    Member

    Perhaps the higher level classes could use context managers that manually call the close function of the lower level classes in __exit__.

  10. ddemidov commented on Jan 4, 2016

    @ddemidov
    Member

    I though about context managers. The problem with those is everything has to happen under single with statement, and I don't see how can we do this with file cache (or fbmem handle).

    Well, I think all of the issues we got were related to destructors in motor classes. It is possible that the other destructors we have are trivial enough to not cause any problems. We could try to clean up motors first and see what happens.

  11. victorkamensky commented on Jan 5, 2016

    @victorkamensky
    Author

    I wonder whether just adding 'try' 'except' around code and ignoring all
    exception in motor destructors would be sufficient to address this problem.

    It boils down to:

    1. it was decided that reliable motors stop will be/should be handled
      outside

    2. if program is still running current destructors core seems to work
      fine: no circular dependencies, etc

    3. the only remaining issue is possible messages/exceptions from
      desctructors during process termination, where they are ignored
      anyway, If it is possible to get rid of the message IMHO it would be
      sufficient

    I am working with small group of 7th grade kids. They are learning python
    and
    ev3dev on EV3. They are taught to look at exception messages from python.
    The one that I mentioned at start of the thread are false positives and
    distracting. If those message could be silenced it would be good enough.

    Asking folks to use 'with' statements would too much IMO. I will have hard
    time explaining to kids who just started learning who to program what
    is going on :)

    Thanks,
    Victor

    On Mon, Jan 4, 2016 at 10:10 AM, Denis Demidov notifications@github.com
    wrote:

    I though about context managers. The problem with those is everything has
    to happen under single with statement, and I don't see how can we do this
    with file cache (or fbmem handle).

    Well, I think all of the issues we got were related to destructors in
    motor classes. It is possible that the other destructors we have are
    trivial enough to not cause any problems. We could try to clean up motors
    first and see what happens.

    —
    Reply to this email directly or view it on GitHub
    #96 (comment)
    .

  12. rhempel commented on Jan 5, 2016

    @rhempel
    Member

    Victor, are he kids your own, or are you a teacher? I am very interested in hearing about how you manage to teach 7th Graders to code using Python. I'll look at getting these exceptions fixed in the next pass at the bindings.

  13. victorkamensky commented on Jan 6, 2016

    @victorkamensky
    Author

    Hi Ralph,

    On Tue, Jan 5, 2016 at 2:32 PM, Ralph Hempel notifications@github.com
    wrote:

    Victor, are he kids your own, or are you a teacher? I am very interested
    in hearing about how you manage to teach 7th Graders to code using Python.

    No, I am not a teacher. We had FLL team that I coached. After 3 seasons
    of FLL with EV3 graphical environment programming we decided to move
    further. So with elder part of previous team (my son and two of his
    friends) kids
    are trying to learn real programming language while still doing some
    robotics.
    And of course we have all pieces around from FLL. Experience with EV3
    graphical
    programming environment does help, very often kids relate back to it.

    IMHO python is very good start as programming language. I personally
    recommend the following book:

    http://www.amazon.com/gp/product/1435455002?keywords=python%20programming%20for%20the%20absolute%20beginner&qid=1452043541&ref_=sr_1_1&sr=8-1
    "Python Programming for the Absolute Beginner, 3rd Edition" by Michael
    Dawson

    That one is about writing games with python initially text based and simple
    graphical at the end, while introducing all basic programming concepts on
    the
    way. We use that one as python "textbook" but we try to apply what we
    learned to robotic related tasks so it is more fun and familiar problem
    set.
    Using ev3dev python module kids are writing functions to drive in cm/inches
    (rather than motor rotations), make robot turns in degrees, drive some
    specified route, writing robot class, drive with sensors on FLL field
    following
    line, etc. Some simple things so far.

    LeJOS was another choice, but I find that learning basic programming
    skills with Java is much harder compared to python.

    While we have this thread: I just want to say big thank you to all you
    guys! You are doing great job with ev3dev! It is exactly what I've been
    looking for. Very much appreciated.

    Thanks,
    Victor

    I'll look at getting these exceptions fixed in the next pass at the

    bindings.

    —
    Reply to this email directly or view it on GitHub
    #96 (comment)
    .

  14. dwalton76 commented on Sep 15, 2016

    @dwalton76
    Collaborator

    @ddemidov do you think this one is safe to close? The only __del__ left is in FbMem, I haven't heard mention of any issues with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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