Skip to content
This repository was archived by the owner on Feb 15, 2023. It is now read-only.

Keep-alive request causes segfault on connection close#55

Merged
nikhilm merged 1 commit intonikhilm:masterfrom
brendanbates89:fix-keepalive
Jan 26, 2016
Merged

Keep-alive request causes segfault on connection close#55
nikhilm merged 1 commit intonikhilm:masterfrom
brendanbates89:fix-keepalive

Conversation

@brendanbates89
Copy link
Copy Markdown

Following the guidelines regarding the memory management of the request object in the docs:

QHttpRequest is never deleted by QHttpServer. Since it is not possible to determine till what point the application may want access to its data, it is up to the application to delete it. A recommended way to handle this is to create a new responder object for every request and to delete the request in that object's destructor. The object itself can be deleted by connecting to QHttpResponse's done() slot as explained below.

When on a browser that supports keep-alive connections, the closing of the connection will cause a segfault in the MOC file for the QHttpConnection, as shown below:

screen shot 2015-07-23 at 10 43 57 am

This segfault occurs because the open connection handles multiple requests, and they are each deleted using the QObject::deleteLater() slot attached to the QHttpRequest::done() signal. The problem occurs when the keep-alive connection is finally closed. The current request will most likely have already been deleted, but then the connection being closed calls the end() slot on it right here:

if (m_request) {
    if (m_request->successful())
        return;

    m_request->setSuccessful(false);
    Q_EMIT m_request->end();
}

Even if the request itself was successful, it seems that the deleting of the request invalidates that memory and the call to QHttpRequest::successful() will sometimes return false, causing the Q_EMIT to occur. I used Chrome for testing my current application and it seemed very strange to me that the application would (very unconsistently) crash after sitting for about 5 minutes! It was only after I noticed that the crashes often occured after closing the browser completely.

The presented fix is simple and will simply attach a slot to the QObject::destroyed signal. We can use this to complete the request if not successful set the request to NULL. This invalidateRequest function can now also be used in the connection closed handler.

…to be emitted on a previously deleted request. This would cause a segfault coming from the Qt MOC code where the emit occurs.
@brendanbates89
Copy link
Copy Markdown
Author

Just wanted to note, I know at some point you plan on redoing the memory management, but I still find this library very useful and thought this fix might be useful for anyone planning to use the library in its current state!

nikhilm added a commit that referenced this pull request Jan 26, 2016
Keep-alive request causes segfault on connection close
@nikhilm nikhilm merged commit ca8f327 into nikhilm:master Jan 26, 2016
@nikhilm
Copy link
Copy Markdown
Owner

nikhilm commented Jan 26, 2016

Thanks for the contribution. I've merged this, but I no longer intend to work on this project. This might change in the future, but for now I've updated the README with an alternative.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants