Skip to content

cobalt: Implement the hermetic gmtime_r without ICU - #13142

Merged
sacuff merged 2 commits into
youtube:mainfrom
hferreiro:log-gmtime-deadlock
Oct 8, 2026
Merged

sacuff merged 2 commits into
youtube:mainfrom
hferreiro:log-gmtime-deadlock

Conversation

@hferreiro

@hferreiro hferreiro commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

#6748 replaced the whole time_t to struct tm family with ICU implementations, but only localtime, localtime_r and mktime need the timezone database. UTC has no offset to apply, so gmtime_r can use the plain computation musl already provides.

This removes a deadlock: ICU initializes lazily and logs when it fails, and Starboard's logging calls gmtime_r to timestamp every message, so the failure reenters the initialization and spins forever inside its pthread_once. It was exposed by
#12193, which moved the ICU data from libcobalt.so to a file. That made AOSP hang at startup, since its apk didn't ship the file until #12651 added it.

The gmtime overflow tests used values that fit in a struct tm, which only the ICU implementation from #6748 rejected, so they had to be hermetic-only. They now test the real tm_year limits, which musl and glibc both honour, and drop that guard.

Bug: 495203133

youtube#6748 replaced the whole `time_t` to
`struct tm` family with ICU implementations, but only `localtime`,
`localtime_r` and `mktime` need the timezone database. UTC has no offset to
apply, so `gmtime_r` can use the plain computation musl already provides.

This removes a deadlock: ICU initializes lazily and logs when it fails, and
Starboard's logging calls `gmtime_r` to timestamp every message, so the
failure reenters the initialization and spins forever inside its
`pthread_once`. It was exposed by
youtube#12193, which moved the ICU data from
`libcobalt.so` to a file. That made AOSP hang at startup, since its apk
didn't ship the file until youtube#12651
added it.

Bug: 495203133
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

🤖 Gemini Suggested Commit Message


cobalt: Implement hermetic gmtime_r without ICU

Replace the ICU-based gmtime_r implementation with the native musl
computation. While localtime and mktime require ICU for timezone
database lookups, gmtime_r handles UTC which has no offset and can
be computed directly without the ICU dependency.

This change resolves a deadlock where Starboard logging calls
gmtime_r to timestamp messages. If ICU fails to initialize, it logs
a message that re-enters gmtime_r and hangs inside a pthread_once
block. This occurred on AOSP when the ICU data file was missing.

Bug: 495203133

💡 Pro Tips for a Better Commit Message:

  1. Influence the Result: Want to change the output? You can write custom prompts or instructions directly in the Pull Request description. The model uses that text to generate the message.
  2. Re-run the Generator: Post a comment with: /generate-commit-message
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor
Context State Description
CI / All Pass ✅ PASS All checks passed
CI / android ✅ PASS Passed
CI / evergreen ✅ PASS Passed
CI / linux ✅ PASS Passed
CI / tvos ✅ PASS Passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request simplifies the gmtime implementation by removing the ICU-dependent gmtime_r override in favor of musl's upstream gmtime_r and __secs_to_tm implementations, which are now included in the build. Additionally, a __utc constant is defined in __tz.c. The review feedback recommends adding a defensive nullptr check for the input time pointer in gmtime to prevent a potential null pointer dereference.

Comment thread cobalt/common/libc/time/gmtime.cc Resolved
The overflow cases used values that still fit in a `struct tm`, which the
ICU-based `gmtime_r` only rejected because
youtube#6748 miscalculated its limits.

Test the real limits, where `tm_year` is `INT_MAX` and `INT_MIN`: each must
convert, and one second past it must fail with `EOVERFLOW`.

Bug: 495203133
@sacuff
sacuff merged commit 8d73819 into youtube:main Oct 8, 2026
1005 of 1014 checks passed
@hferreiro
hferreiro deleted the log-gmtime-deadlock branch October 8, 2026 22:12
briantting pushed a commit that referenced this pull request Oct 12, 2026
…t ICU

Refer to original PR: #13142

#6748 replaced the whole `time_t`
to `struct tm` family with ICU implementations, but only `localtime`,
`localtime_r` and `mktime` need the timezone database. UTC has no offset
to apply, so `gmtime_r` can use the plain computation musl already
provides.

This removes a deadlock: ICU initializes lazily and logs when it fails,
and Starboard's logging calls `gmtime_r` to timestamp every message, so
the failure reenters the initialization and spins forever inside its
`pthread_once`. It was exposed by
#12193, which moved the ICU data
from `libcobalt.so` to a file. That made AOSP hang at startup, since its
apk didn't ship the file until
#12651 added it.

The `gmtime` overflow tests used values that fit in a `struct tm`, which
only the ICU implementation from
#6748 rejected, so they had to be
hermetic-only. They now test the real `tm_year` limits, which musl and
glibc both honour, and drop that guard.

Bug: 495203133

(cherry picked from commit 8d73819)
briantting pushed a commit that referenced this pull request Oct 16, 2026
…t ICU

Refer to original PR: #13142

#6748 replaced the whole `time_t`
to `struct tm` family with ICU implementations, but only `localtime`,
`localtime_r` and `mktime` need the timezone database. UTC has no offset
to apply, so `gmtime_r` can use the plain computation musl already
provides.

This removes a deadlock: ICU initializes lazily and logs when it fails,
and Starboard's logging calls `gmtime_r` to timestamp every message, so
the failure reenters the initialization and spins forever inside its
`pthread_once`. It was exposed by
#12193, which moved the ICU data
from `libcobalt.so` to a file. That made AOSP hang at startup, since its
apk didn't ship the file until
#12651 added it.

The `gmtime` overflow tests used values that fit in a `struct tm`, which
only the ICU implementation from
#6748 rejected, so they had to be
hermetic-only. They now test the real `tm_year` limits, which musl and
glibc both honour, and drop that guard.

Bug: 495203133

(cherry picked from commit 8d73819)
briantting pushed a commit that referenced this pull request Oct 16, 2026
…t ICU

Refer to original PR: #13142

#6748 replaced the whole `time_t`
to `struct tm` family with ICU implementations, but only `localtime`,
`localtime_r` and `mktime` need the timezone database. UTC has no offset
to apply, so `gmtime_r` can use the plain computation musl already
provides.

This removes a deadlock: ICU initializes lazily and logs when it fails,
and Starboard's logging calls `gmtime_r` to timestamp every message, so
the failure reenters the initialization and spins forever inside its
`pthread_once`. It was exposed by
#12193, which moved the ICU data
from `libcobalt.so` to a file. That made AOSP hang at startup, since its
apk didn't ship the file until
#12651 added it.

The `gmtime` overflow tests used values that fit in a `struct tm`, which
only the ICU implementation from
#6748 rejected, so they had to be
hermetic-only. They now test the real `tm_year` limits, which musl and
glibc both honour, and drop that guard.

Bug: 495203133

(cherry picked from commit 8d73819)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants