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

Server crash after client calls QtTcpSocket::disconnectFromHost();



  • Hello!
    I have a server running in linux and a client running in windows.
    When the client close the application or lost connection everything works fine, but when the application runs the code:

    void Network::disconnect()
    {
       ...
        socket->disconnectFromHost();
     ...
    }
    

    The server will crash here::

    void SendJSON(json j)
    {
    	vector <uint8_t> bson = json::to_bson(j);
    	auto s = bson.size();
    
    	vector <uint8_t> final;
    	final.push_back(s >> 0);
    	final.push_back(s >> 8);
    	final.push_back(s >> 16);
    	final.push_back(s >> 24);
    
    	for (int i = 0; i < s; i++)
    	{
    		final.push_back(bson[i]);
    	}
    
        if (write(client, &final.front(), final.size()) <= 0)  <- at this line the application will crash
    	{
    		connected = false;
    		printf("connection failed\n");
    		close(client);
    
    		piThreadCreate(CheckForConnections);
    	}
    }
    

    Is the problem that I'm using QtTcpSocket for the client and not for the server and if so, how can I disconnect in a safe way without crashing the server or closing the application?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What are you using of the server ?

    Please provide the stack trace of your crash.



  • I'm new to linux and I dont know how can I get the stack trace?

    The server is running on a RaspberryPI.
    The RaspberryPI is controlling my boiler and gathering some information like temperature, etc.
    And the server is sending that information to the client so I can watch my boiler on my pc even when I'm not home.


  • Lifetime Qt Champion

    Use gdb to run your application.



  • This is the stack trace:

    Thread 5 "test" received signal SIGPIPE, Broken pipe.
    [Switching to Thread 0xb4fff450 (LWP 7463)]
    __GI___libc_write (nbytes=52, buf=0xb50008a0, fd=5)
        at ../sysdeps/unix/sysv/linux/write.c:26
    26      ../sysdeps/unix/sysv/linux/write.c: No such file or directory.
    
    


  • __libc_write (int fd, const void *buf, size_t nbytes)
    {
      return SYSCALL_CANCEL (write, fd, buf, nbytes); <- this is that line of code
    }
    

  • Lifetime Qt Champion

    Are you using a QTcpServer ?


  • Lifetime Qt Champion

    If not, are you handling that signal ? See here for some suggestions related to that.



  • Such a simple mistake I have not used TCP in such a long time that I forgot all about signals.
    I was getting so frustrated trying to solve it for several hours.
    Now it works, thank you so much!



  • Just of curiosity, why does disconnectFromHost() sends a sigpipe but when I close application it does not send a sigpipe?


  • Lifetime Qt Champion

    Sorry, I currently don't know although I am also intrigued.


Log in to reply