Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

semaphore.h: handle spurious wakeups in TimedWait() on Linux #1021

Merged
merged 4 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion app/src/semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,30 @@ class Semaphore {
return WaitForSingleObject(semaphore_, milliseconds) == 0;
#else // not windows and not mac - should be Linux.
timespec t = internal::MsToAbsoluteTimespec(milliseconds);
return sem_timedwait(semaphore_, &t) == 0;
while (true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add a test exercising this failure/fix to semaphore_test.cc? (Or, even better, does re-enabling the disabled MultithreadedStressTest in that file now work?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests added in #1036 and #1037

int result = sem_timedwait(semaphore_, &t);
if (result == 0) {
// Return success, since we successfully locked the semaphore.
return true;
}
switch (errno) {
case EINTR:
// Restart the wait because we were woken up spuriously.
DellaBitta marked this conversation as resolved.
Show resolved Hide resolved
continue;
case ETIMEDOUT:
// Return failure, since the timeout expired.
return false;
case EINVAL:
assert("sem_timedwait() failed with EINVAL" == 0);
DellaBitta marked this conversation as resolved.
Show resolved Hide resolved
DellaBitta marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure, you want this to NOT actually assert in release builds, yes? (the default assert behavior?)

return false;
case EDEADLK:
assert("sem_timedwait() failed with EDEADLK" == 0);
return false;
default:
assert("sem_timedwait() failed with an unknown error" == 0);
return false;
}
}
#endif
}

Expand Down
7 changes: 7 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@ workflow use only during the development of your app, not for publicly shipping
code.

## Release Notes
### Upcoming Changes
- Changes
- General (Android,Linux): Fixed a concurrency bug where waiting for an
event with a timeout could occasionally return prematurely, as if the
timeout had occurred
([#1021](https://github.com/firebase/firebase-cpp-sdk/pull/1021)).

### 9.2.0
- Changes
- GMA: Added the Google Mobile Ads SDK with updated support for AdMob. See
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy