Repository navigation
Description
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
rhempel commentedon Dec 6, 2015
That is a good idea! If I understand correctly, by opening
/sys/class/tacho-motor/motor?/emergency_stopwe 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.dlech commentedon Dec 6, 2015
sysfs has no hooks for the opening and closing of files, so an
emergency_stopattribute 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.ddemidov commentedon Dec 6, 2015
This wrapper script could even be put into shebang line. This works, for example:
wrapper.sh
hello.py
output of
./hello.py:We could even introduce a system-wide script for this.
rhempel commentedon Jan 2, 2016
@ddemidov - is this better handled in the README.rst file - ie a "Best Practices" section?
ddemidov commentedon Jan 3, 2016
@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:
ddemidov commentedon Jan 3, 2016
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.dlech commentedon Jan 3, 2016
Add a
closeordisposefunction to those classes?ddemidov commentedon Jan 4, 2016
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.
dlech commentedon Jan 4, 2016
Perhaps the higher level classes could use context managers that manually call the
closefunction of the lower level classes in__exit__.ddemidov commentedon Jan 4, 2016
I though about context managers. The problem with those is everything has to happen under single
withstatement, 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.
victorkamensky commentedon Jan 5, 2016
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:
it was decided that reliable motors stop will be/should be handled
outside
if program is still running current destructors core seems to work
fine: no circular dependencies, etc
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:
rhempel commentedon Jan 5, 2016
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.
victorkamensky commentedon Jan 6, 2016
Hi Ralph,
On Tue, Jan 5, 2016 at 2:32 PM, Ralph Hempel notifications@github.com
wrote:
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
dwalton76 commentedon Sep 15, 2016
@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.