Skip to content

fix(time): stop wall-clock default from panicking on wasm32 - #704

Merged
jlucaso1 merged 2 commits into
mainfrom
fix/wall-clock-wasm32-no-panic
Jun 4, 2026
Merged

fix(time): stop wall-clock default from panicking on wasm32#704
jlucaso1 merged 2 commits into
mainfrom
fix/wall-clock-wasm32-no-panic

Conversation

@jlucaso1

@jlucaso1 jlucaso1 commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Problem

wacore::time's default wall-clock provider, ChronoTimeProvider, calls chrono::Utc::now() with no cfg-gate. On wasm32-unknown-unknown chrono has no backend (we don't pull wasmbind), so Utc::now() falls through to SystemTime::now(), which panics ("time not implemented on this platform"). Nothing in the repo calls set_time_provider, so a wasm embedder that forgets to register one panics on the first timestamp (any stanza, app-state mutation, or log line).

This is an asymmetry with the monotonic clock, which already cfg-splits: native StdMonotonicProvider, wasm WallDerivedMonotonicProvider. Only the wall clock was left un-gated, and the wasm CI is build-only so it never caught the runtime panic.

Fix

Mirror the monotonic clock's split:

  • ChronoTimeProvider is now #[cfg(not(target_arch = "wasm32"))].
  • On wasm32 the default is UnsetWasmTimeProvider, which returns epoch (0) and warns once. The wall clock has no internal source on wasm32 (and 0 is safe for now_utc() / now_secs_u64()), so embedders register a real provider via set_time_provider (e.g. backed by Date.now()).
  • now_millis() selects via a cfg-split default_time_provider(), exactly like now_nanos() / default_monotonic_provider().

This keeps wacore dependency-free on wasm32 (no js-sys / wasm-bindgen). The alternative, enabling chrono's wasmbind feature only for the wasm32 target, would make Utc::now() work out of the box but pull in wasm-bindgen; happy to switch if you'd prefer zero-config over leanness.

Tests

Native native_default_time_provider_returns_real_time guards that the native default still yields a real (post-2020) timestamp after the cfg-split. The wasm32 default is panic-free by construction (it no longer calls chrono::Utc::now()); confirmed by building whatsapp-rust --target wasm32-unknown-unknown locally (the wasm runtime panic itself can't be exercised in CI, which is build-only).

cargo fmt --all
cargo clippy -p wacore --all-targets -- -D warnings   # clean
cargo test -p wacore time::                            # pass (incl. 1 new)
RUSTFLAGS='--cfg getrandom_backend="wasm_js"' cargo build -p whatsapp-rust --lib \
  --release --target wasm32-unknown-unknown --no-default-features --features debug-diagnostics  # ok

Breaking

None. Native behavior is unchanged. On wasm32 the default goes from "panic on first timestamp" to "epoch + warning until a provider is set", which is strictly safer.

ChronoTimeProvider called chrono::Utc::now() with no cfg-gate; on wasm32
(no wasmbind backend) that falls through to SystemTime::now(), which panics
on the first timestamp. The monotonic clock already cfg-splits with a wasm
fallback; the wall clock was left un-gated.

cfg-gate ChronoTimeProvider to non-wasm32 and add UnsetWasmTimeProvider
(returns epoch + warns once) for wasm32, selected via a cfg-split
default_time_provider(), mirroring default_monotonic_provider(). No new
deps; embedders register a real provider via set_time_provider().
@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5f4181ec-4654-4822-80f7-e1037c5c64c6

📥 Commits

Reviewing files that changed from the base of the PR and between 5addf0f and 03035b3.

📒 Files selected for processing (1)
  • wacore/src/time.rs

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Time provider now adapts by platform: native builds use the real system clock; WebAssembly returns epoch milliseconds and emits a one-time warning when no provider is configured. Later provider registration will correctly take effect even if time was queried early.
  • Tests

    • Added test coverage to verify the default time provider behavior across platforms (ensures native provider yields a post-2020 timestamp).

Walkthrough

The time provider selection is now cfg-gated: native builds default to a chrono-backed ChronoTimeProvider; wasm builds default to UnsetWasmTimeProvider which returns epoch millis (0) and emits a one-time warning when first used without a registered provider.

Changes

Architecture-Specific Time Provider Selection

Layer / File(s) Summary
Provider implementations by architecture
wacore/src/time.rs
ChronoTimeProvider is now cfg(not(target_arch = "wasm32")). A new UnsetWasmTimeProvider is added for wasm32, implementing TimeProvider to return 0 milliseconds and log a single warning on first now_millis() call.
Default provider initialization
wacore/src/time.rs
now_millis() is refactored to be target-specific: native uses TIME_PROVIDER.get_or_init(default_time_provider) while wasm checks TIME_PROVIDER.get() and otherwise calls UnsetWasmTimeProvider directly. default_time_provider() is cfg-selected (Chrono on native, Unset on wasm).
Default provider validation
wacore/src/time.rs
A native-only #[cfg(test)] module calls default_time_provider().now_millis() and asserts the result is post-2020.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • oxidezap/whatsapp-rust#611: Modifies wacore/src/time.rs and interacts with wasm monotonic provider logic; related to the wasm fallback behavior.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(time): stop wall-clock default from panicking on wasm32' directly and clearly summarizes the main change: fixing a panic issue on wasm32 by preventing the wall-clock default from calling chrono::Utc::now() unconditionally.
Description check ✅ Passed The description thoroughly explains the problem (ChronoTimeProvider panicking on wasm32), the fix (cfg-gating ChronoTimeProvider and adding UnsetWasmTimeProvider), and testing approach, all directly related to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/wall-clock-wasm32-no-panic

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5addf0f6aa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread wacore/src/time.rs
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Benchmark Results

67 unchanged benchmark(s)
Benchmark Current Baseline Change
reporting_token_benchmark::content_extraction_group::bench_content_extraction simple:setup_simple_message() 2,838 2,838 +0.0%
reporting_token_benchmark::content_extraction_group::bench_content_extraction extended:setup_extended_message() 8,272 8,272 +0.0%
reporting_token_benchmark::key_derivation_group::bench_key_derivation 31,317 31,317 +0.0%
reporting_token_benchmark::token_calculation_group::bench_token_calculation 13,827 13,827 +0.0%
reporting_token_benchmark::full_generation_group::bench_full_token_generation simple:setup_full_gen_simple() 49,398 49,398 +0.0%
reporting_token_benchmark::full_generation_group::bench_full_token_generation extended:setup_full_gen_extended() 54,827 54,827 +0.0%
reporting_token_benchmark::message_encoding_group::bench_message_encoding simple:setup_simple_message() 1,592 1,592 +0.0%
reporting_token_benchmark::message_encoding_group::bench_message_encoding extended:setup_extended_message() 4,219 4,219 +0.0%
send_receive_benchmark::dm_send::bench_dm_send text:setup_dm_send() 112,838 113,083 -0.2%
send_receive_benchmark::dm_recv::bench_dm_recv text:setup_dm_recv() 1,656,123 1,656,232 -0.0%
send_receive_benchmark::group_send::bench_group_send group_10:setup_group_send_10() 650,264 650,178 +0.0%
send_receive_benchmark::group_send::bench_group_send group_50:setup_group_send_50() 873,593 873,761 -0.0%
send_receive_benchmark::group_send::bench_group_send group_256:setup_group_send_256() 2,081,428 2,081,591 -0.0%
send_receive_benchmark::group_send_skdm::bench_group_send_skdm skdm_10:setup_group_skdm_10() 747,431 747,253 +0.0%
send_receive_benchmark::group_send_skdm::bench_group_send_skdm skdm_50:setup_group_skdm_50() 1,330,445 1,326,397 +0.3%
send_receive_benchmark::group_send_skdm::bench_group_send_skdm skdm_256:setup_group_skdm_256() 4,366,695 4,393,099 -0.6%
send_receive_benchmark::group_recv::bench_group_recv text:setup_group_recv() 515,972 519,911 -0.8%
binary_benchmark::marshal_group::bench_marshal_allocating 45,401 45,401 +0.0%
binary_benchmark::marshal_group::bench_marshal_auto_allocating 45,451 45,451 +0.0%
binary_benchmark::marshal_group::bench_marshal_exact_allocating 66,354 66,354 +0.0%
binary_benchmark::marshal_group::bench_marshal_reusing_buffer 43,512 43,512 +0.0%
binary_benchmark::marshal_group::bench_marshal_reusing_buffer_vec_writer 45,507 45,507 +0.0%
binary_benchmark::marshal_group::bench_marshal_long_string 4,930 4,930 +0.0%
binary_benchmark::marshal_group::bench_marshal_auto_long_string 4,961 4,961 +0.0%
binary_benchmark::marshal_group::bench_marshal_exact_long_string 6,732 6,732 +0.0%
binary_benchmark::marshal_group::bench_marshal_huge_bytes_allocating 528,529 528,529 +0.0%
binary_benchmark::marshal_group::bench_marshal_auto_huge_bytes_allocating 528,150 528,150 +0.0%
binary_benchmark::marshal_group::bench_marshal_exact_huge_bytes_allocating 529,396 529,396 +0.0%
binary_benchmark::marshal_group::bench_marshal_many_children_allocating 5,417,786 5,417,786 +0.0%
binary_benchmark::marshal_group::bench_marshal_auto_many_children_allocating 5,362,043 5,362,043 +0.0%
binary_benchmark::marshal_group::bench_marshal_exact_many_children_allocating 13,276,336 13,276,336 +0.0%
binary_benchmark::unmarshal_group::bench_unmarshal small:setup_small_marshaled() 1,850 1,850 +0.0%
binary_benchmark::unmarshal_group::bench_unmarshal large:setup_large_marshaled() 29,217 29,217 +0.0%
binary_benchmark::unpack_group::bench_unpack_uncompressed 618 618 +0.0%
binary_benchmark::unpack_group::bench_unpack_compressed 672,890 672,890 +0.0%
binary_benchmark::attr_parser_group::bench_attr_parser attr_lookup:setup_attr_marshaled() 3,736 3,736 +0.0%
binary_benchmark::roundtrip_group::bench_roundtrip small:setup_small_marshaled() 3,840 3,840 +0.0%
binary_benchmark::roundtrip_group::bench_roundtrip large:setup_large_marshaled() 48,283 48,283 +0.0%
binary_benchmark::roundtrip_group::bench_roundtrip_auto small:setup_small_marshaled() 3,866 3,866 +0.0%
binary_benchmark::roundtrip_group::bench_roundtrip_auto large:setup_large_marshaled() 48,344 48,344 +0.0%
binary_benchmark::roundtrip_group::bench_roundtrip_exact small:setup_small_marshaled() 5,206 5,206 +0.0%
binary_benchmark::roundtrip_group::bench_roundtrip_exact large:setup_large_marshaled() 66,668 66,668 +0.0%
binary_benchmark::child_iteration_group::bench_get_children_by_tag 310,312 310,312 +0.0%
binary_benchmark::jid_optimization_group::bench_jid_to_owned_access jid_access:setup_jid_heavy_marshaled() 8,286 8,286 +0.0%
numeric_attr_benchmark::bench_group::bench_baseline_u32 254 254 +0.0%
numeric_attr_benchmark::bench_group::bench_proposed_u32 91 91 +0.0%
numeric_attr_benchmark::bench_group::bench_baseline_u64 292 292 +0.0%
numeric_attr_benchmark::bench_group::bench_proposed_u64 137 137 +0.0%
numeric_attr_benchmark::bench_group::bench_baseline_i64 317 317 +0.0%
numeric_attr_benchmark::bench_group::bench_proposed_i64 145 145 +0.0%
numeric_attr_benchmark::bench_group::bench_baseline_loop_100_u64 27,425 27,425 +0.0%
numeric_attr_benchmark::bench_group::bench_proposed_loop_100_u64 10,725 10,725 +0.0%
libsignal_benchmark::dm_group::bench_dm_session_establishment setup:setup_dm_users() 4,141,609 4,138,544 +0.1%
libsignal_benchmark::dm_group::bench_dm_encrypt_first_message first_msg:setup_dm_session() 100,131 100,133 -0.0%
libsignal_benchmark::dm_group::bench_dm_decrypt_first_message decrypt_prekey:setup_dm_with_first_message() 4,263,702 4,263,702 +0.0%
libsignal_benchmark::dm_group::bench_dm_encrypt_subsequent_message subsequent:setup_established_dm_session() 99,803 99,803 +0.0%
libsignal_benchmark::group_messaging_group::bench_group_create_distribution_message create:setup_group_sender() 210,262 210,249 +0.0%
libsignal_benchmark::group_messaging_group::bench_group_encrypt_message encrypt:setup_group_with_distribution() 496,921 496,921 +0.0%
libsignal_benchmark::group_messaging_group::bench_group_decrypt_message decrypt:setup_group_with_encrypted_message() 505,136 510,136 -1.0%
libsignal_benchmark::conversation_group::bench_full_dm_conversation full:setup_conversation_data() 11,977,043 11,978,068 -0.0%
libsignal_benchmark::signature_group::bench_signature_creation sign:setup_keypair_with_message() 2,466,138 2,466,138 +0.0%
libsignal_benchmark::signature_group::bench_signature_verification verify:setup_keypair_with_message() 4,902,732 4,929,252 -0.5%
libsignal_benchmark::signature_group::bench_key_generation keygen 2,043,397 2,043,397 +0.0%
libsignal_benchmark::session_optimization_group::bench_decrypt_with_previous_session previous_session:setup_with_archived_sessions() 37,950 37,950 +0.0%
libsignal_benchmark::session_optimization_group::bench_out_of_order_decryption out_of_order:setup_out_of_order_messages() 3,616,036 3,616,036 +0.0%
libsignal_benchmark::session_optimization_group::bench_promote_matching_session promote:setup_promote_matching_session() 223,150 223,150 +0.0%
libsignal_benchmark::session_optimization_group::bench_message_key_eviction eviction:setup_message_key_eviction() 9,980,959 9,980,959 +0.0%
No significant changes detected.

An early now_millis() on wasm32 stored UnsetWasmTimeProvider in the OnceLock, so a later set_time_provider() returned Err and the wall clock (and the monotonic clock that derives from it) stayed at epoch forever. Use the epoch fallback transiently and only read a provider once one is set, so set_time_provider() always wins.
@jlucaso1
jlucaso1 merged commit 10f0874 into main Jun 4, 2026
12 checks passed
@jlucaso1
jlucaso1 deleted the fix/wall-clock-wasm32-no-panic branch June 4, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant