Skip to content

Commit

Permalink
Fix deadlock caused by timeout overflow
Browse files Browse the repository at this point in the history
b/346812667
Change-Id: I9fd9ba7652a1aaee9d748c2dee60cb57b59d4cda

Change-Id: I48e03ebc80e441619a0131dd62917016b986f925
  • Loading branch information
haozheng-cobalt committed Aug 15, 2024
1 parent 2ca4fc9 commit 72b8437
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions starboard/common/condition_variable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,24 @@ bool ConditionVariable::WaitTimed(int64_t duration) const {
bool was_signaled = SbConditionVariableIsSignaled(
SbConditionVariableWaitTimed(&condition_, mutex_->mutex(), duration));
#else
if (duration < 0) {
duration = 0;
}
#if !SB_HAS_QUIRK(NO_CONDATTR_SETCLOCK_SUPPORT)
int64_t timeout_time_usec = starboard::CurrentMonotonicTime();
#else
int64_t timeout_time_usec = starboard::CurrentPosixTime();
#endif // !SB_HAS_QUIRK(NO_CONDATTR_SETCLOCK_SUPPORT)
timeout_time_usec += duration;

// Detect overflow if timeout is near kSbInt64Max. Since timeout can't be
// negative at this point, if it goes negative after adding now, we know we've
// gone over. Especially posix now, which has a 400 year advantage over
// Chromium (Windows) now.
if (timeout_time_usec < 0) {
timeout_time_usec = kSbInt64Max;
}

struct timespec timeout;
timeout.tv_sec = timeout_time_usec / 1000'000;
timeout.tv_nsec = (timeout_time_usec % 1000'000) * 1000;
Expand Down

0 comments on commit 72b8437

Please sign in to comment.