Strang Socket Crash over Thread

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Strang Socket Crash over Thread

Post by evstevemd »

I get below error intermittently. Restarting app couple of times works. But am puzzled as what it mean and what actually goes wrong.
Does anyone have idea as to what is wrong here.

I'm running socket inside wxThread in blocking mode.
TIA

Code: Select all

 thread #11, stop reason = EXC_BAD_ACCESS (code=1, address=0x18)
  * frame #0: 0x00007fff60a3534c libsystem_pthread.dylib`pthread_mutex_lock
    frame #1: 0x00007fff387b1b70 CoreFoundation`CFSocketDisableCallBacks + 64
    frame #2: 0x000000010144a0e5 libwx_baseu_net-3.1.3.0.0.dylib`wxSocketManagerMac::Uninstall_Callback(this=0x0000000101458190, socket_=0x00000001094d5da0, event=wxSOCKET_INPUT) at sockosx.cpp:268
    frame #3: 0x000000010144a6ee libwx_baseu_net-3.1.3.0.0.dylib`(anonymous namespace)::wxSocketImplMac::DoClose(this=0x00000001094d5da0) at sockosx.cpp:69
    frame #4: 0x000000010143ea2b libwx_baseu_net-3.1.3.0.0.dylib`wxSocketImpl::Close(this=0x00000001094d5da0) at socket.cpp:564
    frame #5: 0x000000010143e5db libwx_baseu_net-3.1.3.0.0.dylib`wxSocketImpl::Shutdown(this=0x00000001094d5da0) at socket.cpp:574
    frame #6: 0x000000010144094c libwx_baseu_net-3.1.3.0.0.dylib`wxSocketBase::ShutdownOutput(this=0x00000001094634f0) at socket.cpp:960
    frame #7: 0x00000001014408e2 libwx_baseu_net-3.1.3.0.0.dylib`wxSocketBase::Close(this=0x00000001094634f0) at socket.cpp:950
    frame #8: 0x0000000101443f52 libwx_baseu_net-3.1.3.0.0.dylib`wxSocketClient::DoConnect(this=0x00000001094634f0, remote=0x000000010946c090, local=0x0000000000000000, wait=false) at socket.cpp:2001
    frame #9: 0x0000000101444241 libwx_baseu_net-3.1.3.0.0.dylib`wxSocketClient::Connect(this=0x00000001094634f0, remote=0x000000010946c090, wait=false) at socket.cpp:2055
    frame #10: 0x0000000101ae4166 liblsp.dylib`ClientTCP::Connect() + 86
    frame #11: 0x00000001000a8289 Studio`ClientProxy::Entry(this=0x000000010946c1b0) at Clientproxy.cpp:75
    frame #12: 0x000000010161bf71 libwx_baseu-3.1.3.0.0.dylib`wxThread::CallEntry(this=0x000000010946c1b0) at thrimpl.cpp:356
    frame #13: 0x000000010161b9d1 libwx_baseu-3.1.3.0.0.dylib`wxThreadInternal::PthreadStart(thread=0x000000010946c1b0) at threadpsx.cpp:869
    frame #14: 0x000000010161d5e5 libwx_baseu-3.1.3.0.0.dylib`wxPthreadStart(ptr=0x000000010946c1b0) at threadpsx.cpp:819
    frame #15: 0x00007fff60a37661 libsystem_pthread.dylib`_pthread_body + 340
    frame #16: 0x00007fff60a3750d libsystem_pthread.dylib`_pthread_start + 377
    frame #17: 0x00007fff60a36bf9 libsystem_pthread.dylib`thread_start + 13
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: Strang Socket Crash over Thread

Post by alys666 »

thread #11, stop reason = EXC_BAD_ACCESS (code=1, address=0x18)
the address 0x18 is definitely wrong, looks like access violation
in body of libsystem_pthread.dylib`pthread_mutex_lock.
looks like attempt to lock deleted mutex object.
ubuntu 20.04, wxWidgets 3.2.1
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Strang Socket Crash over Thread

Post by evstevemd »

alys666 wrote: Thu Aug 01, 2019 9:31 pm
thread #11, stop reason = EXC_BAD_ACCESS (code=1, address=0x18)
the address 0x18 is definitely wrong, looks like access violation
in body of libsystem_pthread.dylib`pthread_mutex_lock.
looks like attempt to lock deleted mutex object.
At least two things are strange here:
1. I connects only and don't disconnected at the point if crash. But then trace shows it's closing and disconnecting. Seems magic to me
2. It some times crashes sometime but with two or three trials with the same server it works fine. Am thinking of even changing sockets to use something else, If I cannot figure this out.
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Strang Socket Crash over Thread

Post by ONEEYEMAN »

Hi,
Maybe the server is dropping connection and you code does not handle it gracefully?
Is the server remote? Do you have control over it? Can you see the log there?

Thank you.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Strang Socket Crash over Thread

Post by evstevemd »

ONEEYEMAN wrote: Fri Aug 02, 2019 1:24 pm Hi,
Maybe the server is dropping connection and you code does not handle it gracefully?
That is possible but how do I handle it graceful?
ONEEYEMAN wrote: Fri Aug 02, 2019 1:24 pm Is the server remote? Do you have control over it? Can you see the log there?
I have it here locally but dunno yet how to get logs
Thank you!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Strang Socket Crash over Thread

Post by ONEEYEMAN »

Hi,
What type of server is it?
Web one? UDP? TCP?

There should be some kind of a log folder which you can inspect...

Thank you.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Strang Socket Crash over Thread

Post by evstevemd »

ONEEYEMAN wrote: Fri Aug 02, 2019 5:44 pm Hi,
What type of server is it?
Web one? UDP? TCP?

There should be some kind of a log folder which you can inspect...

Thank you.
It is TCP. Here is the ctor code

Code: Select all

ClientTCP::ClientTCP(int port, wxEvtHandler *handler)
    : ClientBase(handler)
{
    wxCriticalSectionLocker locker(m_sockCS);
    m_socket = new wxSocketClient(wxSOCKET_BLOCK | wxSOCKET_WAITALL);

    m_addr.AnyAddress();
    m_addr.Service(port);

    Connect();
}
and the connect method

Code: Select all

bool ClientTCP::Connect()
{
    wxCriticalSectionLocker locker(m_sockCS);
    m_socket->Connect(m_addr, false);
    return m_socket->WaitOnConnect(0, 400); // 0sec 400ms
}
and method for checking if server is connected

Code: Select all

bool ClientTCP::IsConnected()
{
    wxCriticalSectionLocker locker(m_sockCS);
    return m_socket->IsConnected();
}
and write

Code: Select all


bool ClientTCP::Send(::jsonrpc::Request::Ptr_t request)
{
    wxCriticalSectionLocker locker(m_sockCS);

    if(!IsConnected()) {
        wxPuts("Sockets not connected. Not sending!");
        return false;
    }

    // write to socket
    if(m_socket->IsOk()) {
        wxString data = //gets data here
        if(!m_socket->Write(data.mb_str(), wxStrlen(data)).Error()) {
            ReadSocket(); 
        }
        else
        {
            switch(m_socket->LastError())
            {
                case wxSOCKET_INVOP:
                {
                    wxPuts("SOCKET INVALID OPERATION");
                    break;
                }
                case wxSOCKET_IOERR:
                {
                    wxPuts("SOCKET IO ERROR");
                    break;
                }
                case wxSOCKET_INVADDR:
                {
                    wxPuts("SOCKET INVALID ADDRESS PASSED");
                    break;
                }
                case wxSOCKET_INVSOCK:
                {
                    wxPuts("SOCKET NOT INITIALIZED");
                    break;
                }
                case wxSOCKET_WOULDBLOCK:
                {
                    wxPuts("SOCKET BLOCKING CONFUSION");
                    break;
                }
                case wxSOCKET_TIMEDOUT:
                {
                    wxPuts("SOCKET TIMEOUT");
                    break;
                }
                case wxSOCKET_MEMERR:
                {
                    wxPuts("SOCKET MEMORY ERROR");
                    break;
                }
                default:
                {
                }
            }
            return false;
        }
    }
    else
    {
        return false;
    }

    return true;
}
and read method

Code: Select all

void ClientTCP::ReadSocket(bool block)
{
    wxCriticalSectionLocker locker(m_sockCS);

    char c = 0x00;
    wxString data;
    int dataLength = 0;
    while(m_socket->IsConnected()) {
        // read a char
        if(!block) {
            if(!m_socket->WaitForRead(0, 1)) break; 
        }
        m_socket->Read(&c, 1);

        if(m_socket->LastReadCount() == 0 || m_socket->Error()) {
            break;
        }
        data.Append((wxChar)c, 1);
        //do something with data
    }

    if(block) ReadSocket(false); // continue reading but in timeout mode
}
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply