Discussion:
[Interest] Problem with QSharedMemory
Guido Seifert
2013-01-26 10:38:58 UTC
Permalink
Hi,
I have a minor problem with QSharedMemory. I do the necessary lock/unlock and attach/detach. Process 1 creates a shared memory segment, writes into it. Process 2 attaches, reads, and detaches. Process 1 detaches. This should destroy the shared memory segment, since no process references it anymore. All attach/detach/lock/unlock return always true.

The program runs for hrs and the code seems to be ok. Most of the time. But in my logs I see, that from time to time I get an error. The shared memory cannot be created, because it already exists. I can live with it, but it looks unclean to me. Could it be that there exists a race condition between the last detach() and a create()?

Maybe it would be nice to have a signal, which tells, when a segment really is destroyed? OR course, if such a signal is even possible.

Guido
Thiago Macieira
2013-01-26 17:58:35 UTC
Permalink
Post by Guido Seifert
The program runs for hrs and the code seems to be ok. Most of the time. But
in my logs I see, that from time to time I get an error. The shared memory
cannot be created, because it already exists. I can live with it, but it
looks unclean to me. Could it be that there exists a race condition between
the last detach() and a create()?
Yes. The QSharedMemory code is really racy. If you attach and detach, create
and attempt to destroy, it's quite easy to get the QSharedMemory code to fail.

See
http://qt.gitorious.org/qt/qtbase/commit/60fc88a09c0127bf79563e5449e049f3c9e0fc55

You'll note two things:
1) the race condition begins with QSystemSemaphore
2) the commit doesn't fix the race condition, it only works around it
3) the code was actually entering into a deadlock

Unfortunately, I don't know how to fix the problem. I'd probably have to
rewrite both classes with different semantics to make it work.
Post by Guido Seifert
Maybe it would be nice to have a signal, which tells, when a segment really
is destroyed? OR course, if such a signal is even possible.
Since any segment can be recreated, it's not easy to do that.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Guido Seifert
2013-01-26 18:20:54 UTC
Permalink
Post by Thiago Macieira
Yes. The QSharedMemory code is really racy. If you attach and detach,
create and attempt to destroy, it's quite easy to get the QSharedMemory
code to fail.
Thanks. It is easy to workaround this problem. Just makes my code a little bit more complicated than it has to be. I wanted a second opinion to be sure it isn't my code. And since it is known I suppose I don't need to write a bug report. :-)

Currently my code creates and destroys roughly every 10 seconds. Once every 20-30 minutes I have a failed create. Might be more efficient if I figure out what the max amount of memory is I need and keep it this size.
Post by Thiago Macieira
Post by Guido Seifert
Maybe it would be nice to have a signal, which tells, when a segment
really is destroyed? OR course, if such a signal is even possible.
Since any segment can be recreated, it's not easy to do that.
Thought so. Especially for many different platforms.

Guido

Loading...