[Resolved] 3 lines to crash FreeCAD (reproducible) in Python console

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
a.l
Posts: 86
Joined: Thu Apr 09, 2020 7:14 pm

[Resolved] 3 lines to crash FreeCAD (reproducible) in Python console

Post by a.l »

I have "managed" to crash FreeCAD by copy pasting 3 simple lines into the Python Console.
It is important to copy paste all lines at once to reproduce.
Crash "works" regardless whether a document is loaded or not.

I have also tried several lines of code where lines like those below are printed in the end. That also triggers the problem. (But in that case mixed together with other lines of code - so not two print statements like one right after the other).
The "if True:" is simply to force some indentation - you can use a for loop as well.
It seems indentation plays a small role as simply printing those lines without indentation does not trigger the problem.

Code: Select all

if True:
    print('ß')
    print('ß')

Code: Select all

if True:
    print('Bøf')
    print('Bøf')

Code: Select all

if True:
    print('Böf')
    print('Böf')

Code: Select all

if True:
    print('Æv')
    print('Æv')
Four lines does it too:

Code: Select all

if True:
    print('ß')
    print('TEST')
    print('ß')

Code: Select all

if True:
    print('ß')
    Gui.Selection.getSelection()
    print('ß')
But these examples do not trigger the crash:

Code: Select all

if True:
    print('Boef')
    print('Boef')

Code: Select all

if True:
    print('Av')
    print('Av')
I run the Snap version of FreeCAD 0.20 (but I have tested on the old PPA too with the same result):

Code: Select all

OS: Ubuntu Core 20 (MATE/mate)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git) Snap 161
Build type: Release
Branch: tag: 0.20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.3, Coin 4.0.0, Vtk 7.1.1, OCC 7.6.2
Locale: Danish/Denmark (da_DK)
Installed mods: 
  * Assembly4 0.12.3
Stacktrace:

Code: Select all

$ /snap/bin/freecad
FreeCAD 0.20, Libs: 0.20R29177 (Git)
© Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2022
FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.
FreeCAD wouldn't be possible without FreeCAD community.
  #####                 ####  ###   ####  
  #                    #      # #   #   # 
  #     ##  #### ####  #     #   #  #   # 
  ####  # # #  # #  #  #     #####  #   # 
  #     #   #### ####  #    #     # #   # 
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

Adding snap-specific PYTHONPATH to sys.path: /home/anders/snap/freecad/common/.local/lib/python3.8/site-packages:/snap/freecad/161/lib/python3.8/site-packages:/snap/freecad/161/usr/lib/python3/dist-packages
Assembly4 workbench (v0.12.3) loaded
Program received signal SIGSEGV, Segmentation fault.
#0  /lib/x86_64-linux-gnu/libc.so.6(+0x43090) [0x7f26dd76d090]
#1  0x7f26e041fb54 in Gui::InteractiveInterpreter::compile(char const*) const from /snap/freecad/161/usr/lib/libFreeCADGui.so+0x54
#2  0x7f26e0421e16 in Gui::InteractiveInterpreter::runSource(char const*) const from /snap/freecad/161/usr/lib/libFreeCADGui.so+0x36
#3  0x7f26e0421f6b in Gui::InteractiveInterpreter::push(char const*) from /snap/freecad/161/usr/lib/libFreeCADGui.so+0xbb
#4  0x7f26e0422184 in Gui::PythonConsole::runSource(QString const&) from /snap/freecad/161/usr/lib/libFreeCADGui.so+0xf4
#5  0x7f26e0423c54 in Gui::PythonConsole::keyPressEvent(QKeyEvent*) from /snap/freecad/161/usr/lib/libFreeCADGui.so+0x4c4
#6  0x7f26de9c21b5 in QWidget::event(QEvent*) from /snap/freecad/161/kf5/usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x745
#7  0x7f26dea723e2 in QFrame::event(QEvent*) from /snap/freecad/161/kf5/usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x22
#8  0x7f26dea75129 in QAbstractScrollArea::event(QEvent*) from /snap/freecad/161/kf5/usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x259
Edited to correct some spelling mistakes etc.
Last edited by a.l on Sun Aug 07, 2022 4:30 pm, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by onekk »

Confirmed with:

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20)
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.9.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: Italian/Italy (it_IT)
Installed mods: 
  * Curves 0.5.2
But I think this is an error to make this sort of test, same code is working perfectly if you same lines in a file and execute it, so it is not the code itself, that trigger the error, probably the fact that "Python Console" is simply a "Console", in other word a place where you "type" commands or lines of code.

Maybe: https://docs.python.org/3/tutorial/inte ... ctive-mode

Code: Select all

if True:
...  print('ß')
...  print('ß')
... 
ß
ß
I think that the problem is the missing "empty line" to terminate the "line continuation" triggered when you type in the python console SHIFT + Enter that is the ... represented in the above "dump" of "Python Console" output.

IMHO pasting a multiline test in "Python Console" should be forbidden (But i don't see how it could be done).

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
a.l
Posts: 86
Joined: Thu Apr 09, 2020 7:14 pm

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by a.l »

Copy pasting multiple lines of code does have a good use case. E.g. when testing out new code inside the FreeCAD Gui environment as that cannot be done easily with e.g. VSCode or FreeCAD run in App/Command/Console mode only (or at least I believe so :) ).

Edit: I am aware about "import importlibs" and "importlibs.reload(someModule)", but still for testing Gui stuff it is sometimes much faster to copy paste stuff when things need to be tested imho. Also it would be bad if some people get advised to copy paste 3 lines that crash their FreeCAD session if they were editing a document at the same time - ok their problem, but still.

I was testing some 25 lines of code (due to a patch I am working on for Assembly 4) when I discovered it.
First I thought it was some of those QTreeWidget / QTreeWidgetItems / QModelIndex stuff I was working on that was causing it.
But to me it seems it might be some interpreter problem re. Unicode e.g. utf-8 related (just a guess) due to those "special" characters that seem to be involved like ß, ö, Æ, ø.
I have not had any problems multi line pasting other lines of code so I guess those characters might have a role to play in the crash.

Also simply changing the problematic characters with standard ASCII does not seem to cause the problem. E.g.

Problematic

Code: Select all

if True:
  print('æ')
  print('ø')

Code: Select all

if True:
  print('ø')
  print('ß')
Non-problematic

Code: Select all

if True:
  print('A')
  print('B')
Last edited by a.l on Tue Aug 02, 2022 1:42 pm, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by onekk »

a.l wrote: Tue Aug 02, 2022 1:26 pm ...
I agree that copying and pasting is a legitimate use, but in a "Console" it seems to be somewhat "searching for complications".

When I want to do some test I make some ".py" file (I have an empty.py file in my home directory" and load this in FC.

This will open the "macro Editor" and I paste the code ans issue a "Save As..", so if FC crash or hang, I have the code I'm working on available.

But this is my experience I miss an handy function, that maybe create a .py file and load it in FC available maybe in a "right click" menu over the "Python Console" to make test more easy or a sort of "scratch buffer" were you could paste and edit the code, as even editing a multiline test is not so immediate in "Python Console".

About unicode thing have you tried to make:

Code: Select all

if True:
    print(u"<problematic string>")
    print(u"<problematic string>")
And see what happens, maybe for some obscure reason (It is not easy to tell as Python version could differ across FC version or OS) it should not trigger the "crash" even if it is strange that it will not show errors, or it could be related to the "default font" used that maybe is not supporting some "unicode character codes".

But these are only guesses, probably asking @wmayer could help. I'm not doing a mention, as in the past days I have abused of this feature and I don't want to be identified as a "bothering man".

Regards

Carlo D.


Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
a.l
Posts: 86
Joined: Thu Apr 09, 2020 7:14 pm

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by a.l »

You have interesting point:

Crashes

Code: Select all

if True:
  print('ü')
  print('ü')
Where this does not crash (printing "ü")

Code: Select all

if True:
  print('\u00FC')
  print('\u00FC')
Mmm if pasting code into the Python console in FreeCAD Gui is searching for complications I ponder why it was put there in the first place.
I do find it useful.
You do have a point of having the content inside a file. I also have my scratch pad of a file from where I copy paste. I guess it is just how we work that differs :) I'll adapt and move on - I just like consoles I guess :D .
Then again I do not know if the code for interpreting the input is used other places in FreeCAD why I find it relevant to report.
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by adrianinsaval »

Whether you agree with the practice of pasting multi line code in console or not, FreeCAD shouldn't crash when you paste something, so that discussion is almost off-topic, let's focus on identifying and solving the problem instead.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by onekk »

adrianinsaval wrote: Tue Aug 02, 2022 1:57 pm Whether you agree with the practice of pasting multi line code in console or not, FreeCAD shouldn't crash when you paste something, so that discussion is almost off-topic, let's focus on identifying and solving the problem instead.
It seems a known problem, as similar thing happens in some implementation:

https://stackoverflow.com/questions/771 ... ndentation

I don't know if it could be a "not managed error" that wil trigger something in FC that make it crash.

This is what is returned running a FreeCAD-0.20.0-Linux-x86_64.AppImage in a terminal and pasting offending lines in "Python Console"

Code: Select all

Program received signal SIGSEGV, Segmentation fault.
#0  /usr/lib/libc.so.6(+0x3e8e0) [0x7fe68c03e8e0]
#1  0x7fe68e8d2d92 in Gui::InteractiveInterpreter::compile(char const*) const from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0x52
#2  0x7fe68e8d47e4 in Gui::InteractiveInterpreter::runSource(char const*) const from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0x34
#3  0x7fe68e8d4daa in Gui::InteractiveInterpreter::push(char const*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0xba
#4  0x7fe68e8d5844 in Gui::PythonConsole::runSource(QString const&) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0xf4
#5  0x7fe68e8da172 in Gui::PythonConsole::keyPressEvent(QKeyEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0x492
#6  0x7fe68d19f59d in QWidget::event(QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0x1b3
#7  0x7fe68d208ded in QFrame::event(QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0x27
#8  0x7fe68d20b453 in QAbstractScrollArea::event(QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0x50f
#9  0x7fe68d290b07 in QPlainTextEdit::event(QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0x21f
#10  0x7fe68d174822 in QApplicationPrivate::notify_helper(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0xdc
#11  0x7fe68d178ceb in QApplication::notify(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0x467
#12  0x7fe68e68cc49 in Gui::GUIApplication::notify(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0x89
#13  0x7fe68c5abf88 in QCoreApplication::notifyInternal2(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Core.so.5+0x86
#14  /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5(+0x1aff9c) [0x7fe68d1aff9c]
#15  0x7fe68d174822 in QApplicationPrivate::notify_helper(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0xdc
#16  0x7fe68d17a34d in QApplication::notify(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Widgets.so.5+0x1ac9
#17  0x7fe68e68cc49 in Gui::GUIApplication::notify(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0x89
#18  0x7fe68c5abf88 in QCoreApplication::notifyInternal2(QObject*, QEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Core.so.5+0x86
#19  0x7fe68cafbedc in QGuiApplicationPrivate::processKeyEvent(QWindowSystemInterfacePrivate::KeyEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Gui.so.5+0xca
#20  0x7fe68cafe96b in QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Gui.so.5+0x79
#21  0x7fe68caebc95 in QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Gui.so.5+0x63
#22  /tmp/.mount_FreeCAnFICaz/usr/plugins/platforms/../../lib/libQt5XcbQpa.so.5(+0x59f09) [0x7fe688374f09]
#23  0x7fe68c5a899b in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Core.so.5+0x187
#24  0x7fe68c5ac811 in QCoreApplication::exec() from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libQt5Core.so.5+0xfd
#25  0x7fe68e61175e in Gui::Application::runApplication() from /tmp/.mount_FreeCAnFICaz/usr/bin/../lib/libFreeCADGui.so+0x1b2e
#26  /tmp/.mount_FreeCAnFICaz/usr/bin/freecad(+0x67b1) [0x5605725257b1]
#27  /usr/lib/libc.so.6(+0x29290) [0x7fe68c029290]
#28  /usr/lib/libc.so.6(__libc_start_main+0x8a) [0x7fe68c02934a]
#29  /tmp/.mount_FreeCAnFICaz/usr/bin/freecad(+0x6af1) [0x560572525af1]
This is the FC info of the AppImage used (I've modified the executable name to survive version changing, as I invoke the executable in some scripts to start FC with different set of language and Mod dirs)

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20)
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.9.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: Italian/Italy (it_IT)
Installed mods: 
  * Curves 0.5.2

Hope ti helps.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by adrianinsaval »

I'm not sure it's the same, here the issue seems related to special characters only
User avatar
NewJoker
Veteran
Posts: 3018
Joined: Sun Oct 11, 2020 7:49 pm

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by NewJoker »

It's quite inconsistent on Windows. None of these lines of code cause crash in 0.21 (details below) and only this message is displayed in Python console:

Code: Select all

TypeError: __call__() missing 1 required positional argument: 'source'

However, crashes happen in 0.20 (again, details below) but not each time for all the pieces of code. Also, there's an error message in the Report view in this version:

Code: Select all

Illegal storage access...

Version details:

Code: Select all

OS: Windows 10 (10.0)
Word size of FreeCAD: 64-bit
Version: 0.21.29485 (Git)
Build type: Release
Branch: master
Hash: a236ca843fdd6674afb6d7ed1454fbd3b547f5ea
Python 3.8.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: Polish/Poland (pl_PL)

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: Polish/Poland (pl_PL)
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: 3 lines to crash FreeCAD (reproducible) in Python console

Post by heda »

learned a long time ago to include empty (non-indent) lines at the end when pasting indented code in fc console...

this (always) fails

Code: Select all

if True:
  print('æ')
  print('ø')
this (always) does not...

Code: Select all

if True:
  print('æ')
  print('ø')


with that said, it would be nicer with a syntax error instead of segfault...
Post Reply