Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Segmentation Fault



  • Hi, I was working on a GUI in PyQt5, and I seem to often get Segmentation Faults.

    It did give a backtrace once, which is here: https://hastebin.com/imoxorimoq.coffeescript
    My entire code: https://hastebin.com/uwozawexoh.rb (requires https://github.com/ammaraskar/pyCraft)

    I wasn't sure what happened so I asked some people and they said it's likely an issue with PyQt5, so.. here I am.

    Basically, the program randomly exits with Segmentation fault (core dumped) at random times.
    The program is meant to receive text, and add it to a QLineEdit.
    PyQt5 version: 5.11.3



  • Okay first off you are probably biting off way more than you should with this basic program so instead of trying so much at once let us make it even more basic -- I would have tested this myself but do not have the minecraft stuff loaded so you will have to test it instead -- by doing the following:

    import sys
    
    from time import sleep
    from PyQt5.QtWidgets import QApplication
    
    from minecraft import authentication
    from minecraft.exceptions import YggdrasilError
    from minecraft.networking.connection import Connection
    from minecraft.networking.packets import Packet, clientbound, serverbound
    from minecraft.compat import input
    
    def handle_join_game(join_game_packet):
        print('Connected:',join_game_packet)
    
    def print_chat(chat_packet):
        print("Chat :",chat_packet.json_data)
        sleep(2) #I had to add this or it instantly crashed
    
    def main():
        print('Start Main')
    
        try:
            connection = Connection(options_addr, options_port, username=options_user)
        except Exception as err:
            print("ERROR 1:",err)
            sys.exit()
    
        try:
            connection.register_packet_listener(handle_join_game, clientbound.play.JoinGamePacket)
        except Exception as err:
            print("ERROR 2:",err)
            sys.exit()
    
        try:
            connection.register_packet_listener(print_chat, clientbound.play.ChatMessagePacket)
        except Exception as err:
            print("ERROR 3:",err)
            sys.exit()
    
        try:
            connection.connect()
        except Exception as err:
            print("ERROR 4:",err)
            sys.exit()
    
        sys.exit()
    
    
    if __name__ == '__main__':
        app = QApplication([])
        ex = main()
        sys.exit(app.exec_())
    

    What you are trying to do with the above is figure out where the error is occurring and perhaps get a better idea of what that error actually is. Having looked at the dump and the logic you have in place my guess is that it is not actually a pyqt5 error exactly but perhaps more an error contained within the Minecraft process or a misunderstanding of how to handle what you are getting back from Minecraft -- this is meant to help you determine both.

    Now if the above runs without crashing and you are getting what you expect back within those various calls then post again with sample data of what you are getting back and I will check the rest of the code but again my guess is the issue resides in Minecraft or not fully understanding (aka correctly handling) what you are getting back from Minecraft Still if you catch the error at least we should have a slightly better idea of exactly what the error is this way and that should help as well.



  • Hi,
    I did test out your code above - it simply ran and outputted "Start Main". However I tried to go back to the original version as much as I could:

    def handle_join_game(join_game_packet):
        print('Connected:',join_game_packet)
    
    def print_chat(chat_packet):
        print("Chat :",chat_packet.json_data)
        sleep(2) #I had to add this or it instantly crashed
    
    def main():
        try:
            connection = Connection(options_addr, options_port, username=options_user)
        except Exception as e:
            print("conn error")
        connection.register_packet_listener(handle_join_game, clientbound.play.JoinGamePacket)
        connection.register_packet_listener(print_chat, clientbound.play.ChatMessagePacket)
        connection.connect()
    

    and the output that I was expecting was

    Username set to testing249399
    Connected: 0x25 JoinGamePacket(entity_id=3343, game_mode=0, dimension=0, difficulty=3, max_players=20, level_type='flat', reduced_debug_info=False)
    Chat : {"text":""}
    Chat : {"text":""}
    Chat : {"text":""}
    
    

    (This should be a replica of what you have above without try/excepts (I'll see if any error at all happens), not sure why yours isn't working)

    There should be a tcp connection established until the client quits. The messages at this point should be something along the lines of endless {"text":""}.
    Nothing has thrown an error so far.



  • Okay well start adding in elements of your full program in the smallest chunks feasible and keep encapsulating things with the try except -- eventually you should catch your error where it is occurring and once you have that it might be easier to figure out how to fix the issue



  • Hi,
    I believe that the problem is during

           def print_chat(chat_packet):
               self.textbox.append(chat_packet.json_data)
    

    It only exits with seg fault if the values are appended - however it happens if any value is appended. The program runs fine without issues if no value is appended... however I have no clue on fixes for this. Do you have any recommendations?
    (The lib can perfectly return the values, I tried printing them and it works - it's just when anything is appended to that box that there's a seg fault at different times)



  • Okay @KCocco what I would suggest is the following:

    def print_chat(chat_packet):
        try:
            self.textbox.append(chat_packet.json_data)
        except Exception as err:
            print ("Append Error:",err)
            print ("Append Data :","["+chat_packet.json_data+"]")
    
    

    The key here is that it appears this is where the error is and since its intermittent it makes me feel its more a data issue of some sort (again something coming through that is not expected to) as such we print the error along with the data that we are shipping to the append routine. The "[ ]" are to make sure there are no hidden characters preceding or following the regular textual string we assume is contained within this packet. Once we see what the string contains then we ought to be able to duplicate the error by simply duplicating that string and sending it through the append string. If this does not work then perhaps its the packet just before the packet that triggers the error that is the issue -- in this case do thing following:

    newPacket = "[" + chat_packet.json_data + "]"
    self.textbox.append(newPacket)
    

    This way you can look at the contents of the file and determine if any hidden characters are corrupting the file and causing the issue.



  • @Denni said in Segmentation Fault:

    self.textbox.append(newPacket)

    Hi,
    I've tried the solution you've suggested. However, I still get segfaults without any exceptions thrown. Also - this time I got a copy of the crash log, which I've uploaded here: https://a.uguu.se/HxYIyIjBjeMn__usr_bin_python3.5.1001.crash
    Maybe it can be useful to you.
    Also, here's the stacktrace: https://hastebin.com/kubiguberu.shell



  • Okay first off not sure if this is any part of the issue but from my understanding pyqt5 (which I am assuming you are using) does not play well with anything earlier than python 3.7 (and it appears you are using python 3.5) -- now I know you can perhaps get it to work with python 3.5 but that does not mean it will be 100% stable and this might be a symptom of that -- so my suggestion at this point is to make sure you are using pyqt5 on python 3.7+ --- and if you can do a clean install of latest version python 3.7 along with pyqt5 that would be best ..... while it might not happen I have run into situations where having earlier versions of platform software cause issues - note I am currently upgrading a python 2.7 / pyqt4 project to python 3.7 / pyqt5 and I made sure to load nothing but python 3.7 and pyqt5 on the development machine .... I use a different machine to run the py2.7/qt4 stuff

    Note keep in mind this kind of bug is not only catastrophic as you have seen but hard to track down -- especially since I cannot run it myself using your environment so this means you have to try and think outside the box a bit and figure out how to catch the error if possible -- normally a try except within code (if placed properly) would catch the error and keep it from being catastrophic so not sure why its not catching it unless its not placed in the right spot --- try encapsulating all of your code elements within try except blocks -- aka start each function there-abouts with a try and end it there-abouts with an except printing an error statement that clearly lets you know where you are at -- the issue might be cropping up outside our context without being realized -- then again if its deep enough issue perhaps a try except will not catch it but its always worth a try (pun intended) ;)

    P.S. It appears that there have been several of these crash reports for py3.5 using pyqt5 and they all "seem" to have to do with graphic rendering -- that is just my quick glance out on the internet using the crash designated [ python3.5 crashed with SIGSEGV in QTextEngine::shapeTextWithHarfbuzzNG() ]



  • Hi,
    I didn't expect to have that many issues with an older version of python... welp.
    Either way, thanks for your help :).



  • Yeah sometimes you do and sometimes you do not -- it is a crap shoot... box cars or snake eyes do come up from time to time.

    When investigating this I saw that I could use earlier versions of python but frankly there are reasons that they make changes to the software and using an earlier version (if you do not have to) is just asking for trouble (imho). Further I had noticed in my quick research that while pyqt5 has been tweaked to work with earlier versions of python it was not designed too.

    As a final note -- when developing something new I almost always make sure I am using the latest most up to date tools especially since in today's rapidly advancing technological process hardware actually becomes obsolete in about 5 years or so and software while it will last longer sometimes -- It to has been changing fairly quickly. For instance Python 2.7 will no longer be supported come next year and we are only on Python 3.7 So it behooves you to get the latest and greatest stable platforms to do any new coding on or with.

    If you are needing more assistance once you have upgraded do drop a line if I am about I would enjoy lending you a hand.



  • Hey @KCocco your project interested me so I started looking into it and I came across this and thought you might be interested https://www.howtogeek.com/197947/how-to-install-python-on-windows/

    Granted I think you are using ubuntu but I have found that their are similar aspects from one OS to another so maybe there is something here you can use to help you and I also see my you might be using an earlier version of python -- still all-things-considered -- I would simply get the source code in bits and pieces and begin updating it to 3.7 and pyqt5 ... which is kind of what I plan to do ... so perhaps we can help one another I was going to send you an email but could not so figured I would try it this way.


Log in to reply