Skip to content

feat(watch): require T: Send + Sync for thread-safe endpoints - #497

Open
wzxzhuxi wants to merge 1 commit into
apache:mainfrom
wzxzhuxi:watch-tighten-bounds
Open

wzxzhuxi wants to merge 1 commit into
apache:mainfrom
wzxzhuxi:watch-tighten-bounds

Conversation

@wzxzhuxi

@wzxzhuxi wzxzhuxi commented Dec 1, 2026

Copy link
Copy Markdown

Summary

watch::{Sender, Receiver} are now Send/Sync only when T: Send + Sync, matching tokio::sync::watch and Asyncband's other shared-read primitives. Previously the exclusive value mutex made the endpoints Send + Sync for any T: Send, so a cross-thread Sender<Cell<_>> could cross threads even though the API never hands out &T.

  • Thread-safety bounds are enforced with a PhantomData<std::sync::RwLock<T>> marker in the private shared state (no unsafe, no runtime cost); the read path is unchanged.
  • Adds a module # Thread safety section and two compile_fail doctests pinning Sender<Cell<u8>>: !Send and Receiver<Cell<u8>>: !Sync.
  • CHANGELOG.md records the breaking change with migration guidance.

Benchmark

Issue #456 also proposed swapping the exclusive value mutex for a shared read lock so concurrent readers could clone snapshots in parallel. Benchmarks against main with the in-repo suite (Ryzen 5 5600H, --repeat 10, alternating runs) showed that variant regressing watch/get by 9% and watch/concurrent-read-write by 61%, caused by std RwLock reader-count contention and writers waiting on readers; the other three watch scenarios were unchanged. The shared-read path was dropped, and this PR ships the bounds tightening only. All five scenarios are at parity with main (for example watch/get 348-350 ms vs 349 ms), so concurrent-read optimization is left for a future design that avoids the lock.

Verification

  • cargo x build, cargo x test
  • cargo +nightly x check (feature matrix with -Dwarnings)
  • cargo +nightly x lint
  • cargo +nightly x miri with the CI MIRIFLAGS (72 suites)

Fixes #456

@QwQBiG

QwQBiG commented Dec 1, 2026

Copy link
Copy Markdown
Member

Thanks for benchmarking the shared-read implementation! :3

The mixed read/write result makes a good case for keeping the mutex for small payloads. I’d keep the current bounds for now and record those results in #456.

One use case we’d lose is moving a Receiver<Cell> to a worker that only calls changed(). That works in 0.8.1, and #451 explicitly tested that the receiver is Send.

We can ask for @tisonkun, with get() and recv() still cloning under the mutex, would you tighten the bounds before 1.0 to keep parallel reads an option for 1.x?

@wzxzhuxi

wzxzhuxi commented Dec 1, 2026

Copy link
Copy Markdown
Author

Thanks for benchmarking the shared-read implementation! :3

The mixed read/write result makes a good case for keeping the mutex for small payloads. I’d keep the current bounds for now and record those results in #456.

One use case we’d lose is moving a Receiver to a worker that only calls changed(). That works in 0.8.1, and #451 explicitly tested that the receiver is Send.

We can ask for @tisonkun, with get() and recv() still cloning under the mutex, would you tighten the bounds before 1.0 to keep parallel reads an option for 1.x?

@QwQBiG Thanks for the review — and good catch on Receiver<Cell<u8>> with a changed()-only worker; agreed that tightening would remove behavior that works today and that #451 explicitly covered.

I've recorded the shared-read and bounds-only benchmark results in #456:
#456 (comment)

I'll wait for @tisonkun's call on whether to tighten before 1.0.

This branch has not been deployed

No deployments
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.

Tighten watch::{Sender, Receiver} cross-thread bounds to T: Send + Sync before 1.0

2 participants