Skip to contentSkip to conversation

occasional EBADF in select() in notifier.c

Xenomai Applied Matthias Schneider 3 participants 8 messages Apr 21, 2014 – Apr 23, 2014

lore
Share & tools
lore
#0 lore raw

Still working on thread suspension in forge/mercury, I occasionally get a EBADF of the select() call in notifier.c. I suspect that this is due to accessing a copy of the file descriptor list notifier_rset while one of the file descriptors is being closed. This seems to be due to concurrent access on the notifier_rset from notifier_sighandler() and notifier_destroy(). "notifier_lock" is held in notifier_lock(), but not when copying and invoking select in notifier_sighandler(). The EBADF leads to a "spurious notification" reporting and process termination - obviously, the thread suspension was not triggered.

I can think of several ways of addressing this issue but I am not sure about side effects:
a) hold the "notifier_lock" mutex between copying the descriptor list and calling select
b) repeating the select() call in the case of EBADF

Any ideas?

Anyway, why is the select call necessary, isnt the file descriptor signaled via siginfo->si_fd, too?

Regards,
Matthias

#1 lore raw
Quoted 14 lines · Matthias Schneider

Still working on thread suspension in forge/mercury, I occasionally get a EBADF of the select() call in notifier.c. I suspect that this is due to accessing a copy of the file descriptor list notifier_rset while one of the file descriptors is being closed. This seems to be due to concurrent access on the notifier_rset from notifier_sighandler() and notifier_destroy(). "notifier_lock" is held in notifier_lock(), but not when copying and invoking select in notifier_sighandler(). The EBADF leads to a "spurious notification" reporting and process termination -
obviously, the thread suspension was not triggered.

I can think of several ways of addressing this issue but I am not sure about side effects:
a) hold the "notifier_lock" mutex between copying the descriptor list and calling select
b) repeating the select() call in the case of EBADF

Quoted 4 lines

Any ideas?

Anyway, why is the select call necessary, isnt the file descriptor signaled via siginfo->si_fd, too?

I do not know this part of the code, but normally, when select is called for a closed descriptor, select will return with the descriptor readable, EOF is returned when reading the descriptor, which can then be closed. We had a bug report some time ago to implement this behaviour in xenomai posix skin (so for cobalt).

Signature · 1 line
                                                                Gilles.
#2 lore raw
Quoted 17 lines · Gilles Chanteperdrix
Quoted 16 lines · Matthias Schneider

Still working on thread suspension in forge/mercury, I occasionally get a EBADF of the select() call in notifier.c. I suspect that this is due to accessing a copy of the file descriptor list notifier_rset while one of the file descriptors is being closed. This seems to be due to concurrent access on the notifier_rset from notifier_sighandler() and notifier_destroy(). "notifier_lock" is held in notifier_lock(), but not when copying and invoking select in notifier_sighandler(). The EBADF leads to a "spurious notification" reporting and process termination -
obviously, the thread suspension was not triggered.

I can think of several ways of addressing this issue but I am not sure about side effects:
a) hold the "notifier_lock" mutex between copying the descriptor list and calling select
b) repeating the select() call in the case of EBADF

Any ideas?

Quoted 8 lines
Quoted 2 lines

Anyway, why is the select call necessary, isnt the file descriptor signaled via siginfo->si_fd, too?

I do not know this part of the code, but normally, when select is called for a closed descriptor, select will return with the descriptor readable, EOF is returned when reading the descriptor, which can then be closed. We had a bug report some time ago to implement this behaviour in xenomai posix skin (so for cobalt).

Obviously, if it is already closed, read will not return EOF, and there is no need to close it again, I got all mixed up, sorry for the noise.

Quoted 1 line
Signature · 1 line
                                                                Gilles.
#3 lore raw
In reply to Matthias Schneider
Quoted 9 lines · Matthias Schneider

Still working on thread suspension in forge/mercury, I occasionally get a EBADF of the select() call in notifier.c. I suspect that this is due to accessing a copy of the file descriptor list notifier_rset while one of the file descriptors is being closed. This seems to be due to concurrent access on the notifier_rset from notifier_sighandler() and notifier_destroy(). "notifier_lock" is held in notifier_lock(), but not when copying and invoking select in notifier_sighandler(). The EBADF leads to a "spurious notification" reporting and process termination -
obviously, the thread suspension was not triggered.

Quoted 3 lines

I can think of several ways of addressing this issue but I am not sure about side effects:
a) hold the "notifier_lock" mutex between copying the descriptor list and calling select

Not an option, we would need a threaded handler for grabbing the mutex-based lock, which would defeat the purpose of using a directed signal for forcing the recipient thread to stop execution until released.

Quoted 2 lines

b) repeating the select() call in the case of EBADF

EBADF should be ignored. This just means that we won't find the notifier block in the scanned list anyway, which is a possible and correct outcome.

Quoted 5 lines

Any ideas?

Anyway, why is the select call necessary, isnt the file descriptor signaled via siginfo->si_fd, too?

Yes it is. This select() loop is a left-over.

Quoted 8 lines

Regards,
Matthias

_______________________________________________
Xenomai mailing list
[email protected]
http://www.xenomai.org/mailman/listinfo/xenomai

Signature · 1 line
Philippe.
#4 lore raw

----- Original Message -----

Quoted 25 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Tuesday, April 22, 2014 9:32 AM
Subject: Re: [Xenomai] occasional EBADF in select() in notifier.c

Quoted 1 line · Matthias Schneider

  Still working on thread suspension in forge/mercury, I occasionally get a

EBADF

Quoted 1 line

  of the select() call in notifier.c. I suspect that this is due to accessing

a

Quoted 1 line

  copy of the file descriptor list notifier_rset while one of the file

descriptors

Quoted 1 line

  is being closed. This seems to be due to concurrent access on the

notifier_rset

Quoted 1 line

  from notifier_sighandler() and notifier_destroy().

"notifier_lock" is held in

Quoted 1 line

  notifier_lock(), but not when copying and invoking select in

notifier_sighandler().

Quoted 1 line

  The EBADF leads to a "spurious notification" reporting and

process termination -

Quoted 3 lines

   obviously, the thread suspension was not triggered.

  I can think of several ways of addressing this issue but I am not sure

about

Quoted 8 lines
Quoted 2 lines

  side effects:
  a) hold the "notifier_lock" mutex between copying the descriptor

list and calling select

Not an option, we would need a threaded handler for grabbing the mutex-based lock, which would defeat the purpose of using a directed signal for forcing the recipient thread to stop execution until released.

Ok, I understand.

Quoted 5 lines
Quoted 2 lines

  b) repeating the select() call in the case of EBADF

EBADF should be ignored. This just means that we won't find the notifier block in the scanned list anyway, which is a possible and correct outcome.

I do not agree. EBADF only signals that any of the fds is invalid, but not necessarily the one the current thread is interested in. In the scenario being produced in my test, descriptor "A" was being signaled and "B" was closed, being the cause for EBADF. If I had ignored the error, I would have missed a notification for "A". Repeating the select call with a fresh copy of notifier_rset seemed to correctly retrieved the right entry.

Quoted 9 lines
Quoted 4 lines

  Any ideas?

  Anyway, why is the select call necessary, isnt the file descriptor signaled

via

Quoted 2 lines

  siginfo->si_fd, too?

Yes it is. This select() loop is a left-over.

So would this be a third variant, getting rid of select() and using si_fd?

Quoted 8 lines
Quoted 3 lines

  Regards,
  Matthias

Signature · 1 line
Philippe.
#5 lore raw
Quoted 37 lines · Matthias Schneider

----- Original Message -----

Quoted 24 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Tuesday, April 22, 2014 9:32 AM
Subject: Re: [Xenomai] occasional EBADF in select() in notifier.c

Quoted 1 line · Matthias Schneider

Still working on thread suspension in forge/mercury, I occasionally get a

EBADF

Quoted 1 line

of the select() call in notifier.c. I suspect that this is due to accessing

a

Quoted 1 line

copy of the file descriptor list notifier_rset while one of the file

descriptors

Quoted 1 line

is being closed. This seems to be due to concurrent access on the

notifier_rset

Quoted 1 line

from notifier_sighandler() and notifier_destroy().

"notifier_lock" is held in

Quoted 1 line

notifier_lock(), but not when copying and invoking select in

notifier_sighandler().

Quoted 1 line

The EBADF leads to a "spurious notification" reporting and

process termination -

Quoted 3 lines

obviously, the thread suspension was not triggered.

I can think of several ways of addressing this issue but I am not sure

Quoted 9 lines

about

Quoted 2 lines

side effects:
a) hold the "notifier_lock" mutex between copying the descriptor

list and calling select

Not an option, we would need a threaded handler for grabbing the mutex-based lock, which would defeat the purpose of using a directed signal for forcing the recipient thread to stop execution until released.

Ok, I understand.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

b) repeating the select() call in the case of EBADF

EBADF should be ignored. This just means that we won't find the notifier block in the scanned list anyway, which is a possible and correct outcome.

I do not agree. EBADF only signals that any of the fds is invalid, but not necessarily the one the current thread is interested in. In the scenario being produced in my test, descriptor "A" was being signaled and "B" was closed, being the cause for EBADF. If I had ignored the error, I would have missed a notification for "A". Repeating the select call with a fresh copy of notifier_rset seemed to correctly retrieved the right entry.

This is what I implied: ignore the invalid fd causing EBADF, drop it from the poll mask and redo. We obviously don't want to miss other valid notifications.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Any ideas?

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

Anyway, why is the select call necessary, isnt the file descriptor signaled

via

Quoted 2 lines
   siginfo->si_fd, too?

Yes it is. This select() loop is a left-over.

So would this be a third variant, getting rid of select() and using si_fd?

Yes, we don't need to maintain the notifier_rset and the select loop. Just check for psfd[0] == siginfo->si_fd in loop for a match.

Signature · 1 line
Philippe.
#6 lore raw

----- Original Message -----

Quoted 70 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>; "[email protected]" <[email protected]> Cc:
Sent: Tuesday, April 22, 2014 3:02 PM
Subject: Re: [Xenomai] occasional EBADF in select() in notifier.c

Quoted 3 lines · Matthias Schneider

----- Original Message -----

Quoted 2 lines

From: Philippe Gerum <[email protected]>
To: Matthias Schneider <[email protected]>;

"[email protected]" <[email protected]>

Quoted 6 lines
Quoted 6 lines

Cc:
Sent: Tuesday, April 22, 2014 9:32 AM
Subject: Re: [Xenomai] occasional EBADF in select() in notifier.c

Quoted 1 line · Matthias Schneider

    Still working on thread suspension in forge/mercury, I

occasionally get a

Quoted 2 lines
Quoted 2 lines

EBADF

Quoted 1 line

    of the select() call in notifier.c. I suspect that this is due

to accessing

Quoted 2 lines
Quoted 2 lines

a

Quoted 1 line

    copy of the file descriptor list notifier_rset while one of the

file

Quoted 2 lines
Quoted 2 lines

descriptors

Quoted 1 line

    is being closed. This seems to be due to concurrent access on

the

Quoted 6 lines
Quoted 6 lines

notifier_rset

Quoted 1 line

    from notifier_sighandler() and notifier_destroy().

"notifier_lock" is held in

Quoted 1 line

    notifier_lock(), but not when copying and invoking select in

notifier_sighandler().

Quoted 1 line

    The EBADF leads to a "spurious notification" reporting

and

Quoted 4 lines
Quoted 4 lines

process termination -

Quoted 3 lines

    obviously, the thread suspension was not triggered.

    I can think of several ways of addressing this issue but I am

not sure

Quoted 3 lines
Quoted 3 lines

about

Quoted 2 lines

    side effects:
    a) hold the "notifier_lock" mutex between copying the

descriptor

Quoted 5 lines
Quoted 5 lines

list and calling select

Not an option, we would need a threaded handler for grabbing the mutex-based lock, which would defeat the purpose of using a directed signal for forcing the recipient thread to stop execution until

released.

Quoted 8 lines
Quoted 1 line

Ok, I understand.

Quoted 4 lines
Quoted 2 lines

    b) repeating the select() call in the case of EBADF

EBADF should be ignored. This just means that we won't find the

notifier

Quoted 1 line
Quoted 1 line

block in the scanned list anyway, which is a possible and correct

outcome.

Quoted 3 lines

I do not agree. EBADF only signals that any of the fds is invalid, but not

necessarily the one the current thread is interested in. In the scenario being produced in my test, descriptor "A" was being signaled and
"B" was closed, being the cause for EBADF. If I had ignored the error, I would have missed a notification for "A". Repeating the select call with a fresh copy of notifier_rset seemed to correctly retrieved the right entry.

Quoted 4 lines

This is what I implied: ignore the invalid fd causing EBADF, drop it from the poll mask and redo. We obviously don't want to miss other valid notifications.

Ok, sorry I got you wrong.

Quoted 10 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

    Any ideas?

    Anyway, why is the select call necessary, isnt the file

descriptor signaled

Quoted 4 lines
Quoted 4 lines

via

Quoted 2 lines

    siginfo->si_fd, too?

Quoted 12 lines
Quoted 4 lines
Quoted 1 line

Yes it is. This select() loop is a left-over.

So would this be a third variant, getting rid of select() and using si_fd?

Yes, we don't need to maintain the notifier_rset and the select loop. Just check for psfd[0] ==  siginfo->si_fd in loop for a match.

Signature · 1 line
Philippe.

Considering the three options I think I prefer the last one and remove the select() altogether. I have attached a small patch that does exactly that.

Matthias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: notifier_without_select.patch
Type: text/x-patch
Size: 1812 bytes
Desc: not available
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20140422/7412edcd/attachment.bin>

#7 lore raw
Quoted 4 lines · Matthias Schneider

Considering the three options I think I prefer the last one and remove the select() altogether. I have attached a small patch that does exactly that.

Merged, thanks.

Signature · 1 line
Philippe.