This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Race Condition Issue with KJob and QNetworkAccessManager

Tags: None
(comma "," separated)
User avatar
Gallaecio
KDE Developer
Posts
116
Karma
0
OS
I’ve been playing around with libmediawiki. I’m trying to take on the project and expand the library to cover the whole MediaWiki API, which should actually not be that difficult given the work already done. However, I want to try the “first write a test, then write a class” approach. And it turns out 4 of the 12 test of the project are currently failing, so before anything I should try and fix them.

And I’ve spent several hours today trying to figure out where is the source of a race condition that affects the first one of the broken tests. Guess what… I didn’t.

The test function EditTest::error() simply creates an instance of a FakeServer (a custom class to run a server using QTcpServer), starts the server and waits for it to be initialized, and then creates, configures and executes (exec()) an instance of Edit, which is an indirect subclass of KJob (through a custom Job class).

Edit starts, and it creates, configures and starts (start()) another job, a custom class similar to Edit. When the second job finishes, a slot in Edit gets called, which uses an instance of QNetworkAccessManager to POST an URL to the fake server, whose newConnection() slot gets signaled. A second slot in Edit gets called when the instance of QNetworkReply — returned by the call to the network manager — emits the finished() signal, and there the reply is closed and deleted with deleteLater().

Here comes the issue. The second Edit slot gets called just once, however the fake server — the one the code posts an URL to — gets an extra signal newConnection() sometimes.

After several hours trying to figure it out, I finally gave up, and I decided instead to try to reproduce the issue with the less code possible, to show you the code, in the hope that someone else (hopefully you) can figure out how to fix the race condition here.

Here is the code (you should be able to run the example with CMake):

The code just runs a loop 10000 times, and in the loop a FakeServer is created, then a minimal version of Edit — which I renamed to Job and made a direct subclass from KJob —, and whenever the server gets more than one request after running the job, a “WTF?” is printed with qDebug(). In my machine, with a 10000-long loop, there are always some “WTF?” lines.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Interesting. From what I can tell of your debugging this is likely to be a fairly complex issue, so you may want to ask about this on the kde-devel@kde.org mailing list. The people on that list should be able to help you find a solution here.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]