public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dhatch at ilm dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sources.redhat.com
Subject: [Bug nptl/12674] sem_post/sem_wait race causing sem_post to return EINVAL
Date: Mon, 18 Apr 2011 19:26:00 -0000	[thread overview]
Message-ID: <bug-12674-131-T6MQuYcE4T@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-12674-131@http.sourceware.org/bugzilla/>

http://sourceware.org/bugzilla/show_bug.cgi?id=12674

--- Comment #2 from Don Hatch <dhatch at ilm dot com> 2011-04-18 19:25:40 UTC ---
(In reply to comment #1)
> Why would this at all be a bug?  The fact that the sem_wait succeeds doesn't
> indicate at all that the semaphore is unused and destroying an unused semaphore
> is of course completely illegal.  Your code is wrong in assuming what it does. 
> You have to wait for the sem_post call to also return before destroying the
> semaphore.

Hi Ulrich,
Thanks for looking at this.

We're not completely confident that this usage is legal...
but we're not convinced yet that it's illegal either.

In our program, the sem_post itself is intended to indicate to the waiting
thread that it's safe to destroy the semaphore
(and, in a real program, to destroy some associated resource as well).
If the waiter thread has to wait for the sem_post call to return, as you say,
what would be a mechanism for doing that?  Another semaphore?
Would you agree that then either the semaphore,
or the semaphore-that-protects-the-semaphore, etc.
would need to be an object that persists significantly longer
than the resources being protected?
Maybe this is a reasonable or necessary
restriction, but it's a significant one,
and if it's intentional, it would be very helpful to have it documented.

Various manual pages I've seen which come close to mentioning it,
and which seem to me to (weakly) to imply my usage is legal, are:

sem_destroy man page from my RHEL5.3 distribution (man-pages-2.39-12.el5):
"Destroying  a  semaphore  that other processes or threads are currently
blocked on (in sem_wait(3)) produces undefined behaviour."
(doesn't mention sem_post, but it seems like this would be
the appropriate place to mention it if it's illegal,
and the fact that it doesn't mention it seems to imply it's legal).

Various other sem_destroy man pages, such as the one from
Open Group Base Specifications
(http://pubs.opengroup.org/onlinepubs/009695399/functions/sem_destroy.html)
say:
"It is safe to destroy an initialized semaphore upon which no threads are
currently blocked. The effect of destroying a semaphore upon which other
threads are currently blocked is undefined."
(the most literal reading of this implies that in my case,
it's safe to destroy the semaphore, since it's certainly
the case that no threads are currently blocked on it).

The pthread_mutex_destroy man page (from man-pages-2.39-12.el5):
"It shall be safe to destroy an  initialized  mutex  that  is  unlocked.
Attempting to destroy a locked mutex results in undefined behavior."
(again, a literal reading of this implies my usage is safe.
of course this is talking about mutexes, not semaphores,
but I imagine all the same limitations and considerations apply).

The pthread_cond_destroy man page (from man-pages-2.39-12.el5):
"It shall be safe to destroy  an  initialized  condition  variable  upon which 
no threads are currently blocked. Attempting to destroy a condition variable
upon which other threads are currently blocked results in undefined behavior."
(my comment on this would be the same as for pthread_mutex above)

Unfortunately I don't have access to the pthreads standard...
does it take a definite position on this?  If it does,
it would be great to have that clarification added to all these man pages
so that future programmers will have no doubts about it.

Thanks,
Don Hatch

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


  parent reply	other threads:[~2011-04-18 19:26 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-14  6:33 [Bug nptl/12674] New: " dhatch at ilm dot com
2011-04-14  6:34 ` [Bug nptl/12674] " dhatch at ilm dot com
2011-04-14  8:15 ` albertito at blitiri dot com.ar
2011-04-15  8:31 ` vapier at gentoo dot org
2011-04-15 14:57 ` ppluzhnikov at google dot com
2011-04-17  3:50 ` drepper.fsp at gmail dot com
2011-04-18 19:26 ` dhatch at ilm dot com [this message]
2011-04-19  0:04 ` dhatch at ilm dot com
2011-04-19 11:26 ` drepper.fsp at gmail dot com
2011-04-19 22:07 ` dhatch at ilm dot com
2011-08-04  4:49 ` lopresti at gmail dot com
2011-08-07 18:10 ` bugdal at aerifal dot cx
2012-02-10 16:26 ` kevin.dempsey at aculab dot com
2012-02-10 16:29 ` kevin.dempsey at aculab dot com
2012-02-16 15:31 ` carlos at systemhalted dot org
2012-09-25 21:20 ` raj at mischievous dot us
2013-01-12  0:40 ` piotr.stanczyk at gmail dot com
2013-01-12  0:48 ` raj at mischievous dot us
2013-01-24 16:51 ` piotr.stanczyk at gmail dot com
2013-09-13 20:21 ` michael.ballantyne at gmail dot com
2013-09-13 20:27 ` carlos at redhat dot com
2013-09-14 16:47 ` bugdal at aerifal dot cx
2013-11-04 13:29 ` ismail at donmez dot ws
2013-11-06 16:44 ` ljanyst at cern dot ch
2013-12-20 17:50 ` triegel at redhat dot com
2014-02-16 18:29 ` jackie.rosen at hushmail dot com
2014-05-28 19:44 ` schwab at sourceware dot org
2014-06-20 12:24 ` kevin.dempsey at aculab dot com
2014-06-20 18:30 ` triegel at redhat dot com
2014-06-27 13:36 ` fweimer at redhat dot com
2015-01-19 16:36 ` mtk.manpages at gmail dot com
2015-01-21  5:57 ` cvs-commit at gcc dot gnu.org
2015-01-21  5:59 ` carlos at redhat dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-12674-131-T6MQuYcE4T@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).