Skip to content

design(watch): finalize value ownership and sender state APIs #251

Closed

Description

@tisonkun

Goal

Finalize the public watch API before 0.7 by separating latest-state/version semantics from the value ownership policy.

Potential changes

  • Replace the public Arc<T> results from Receiver::{borrow, borrow_and_update, changed} with a borrowed core: Ref<'_, T> for value access and changed() -> Result<(), RecvError> for notification.
  • Optionally add an owned convenience such as recv() -> Result<T, RecvError> where T: Clone; callers can choose T = Arc<U> when they need cheap shared snapshots.
  • Add Sender::send_replace(T) -> T so the retained state can be updated while no receivers exist, while keeping the current send failure behavior unchanged.
  • Consider symmetric Sender::borrow and Sender::is_disconnected inspection methods.

Contracts to preserve

  • The initial value and the current value at subscribe time are considered seen.
  • Every successful publication creates a new version, even when values compare equal.
  • Intermediate updates may coalesce independently for each receiver.
  • Receiver clones inherit the source receiver's seen state and then advance independently.
  • A final unseen value is observable before disconnection is reported.
  • changed remains cancellation safe, and user wakers/destructors continue to run outside internal locks.

Design note

The ownership choices have different costs: a borrowed Ref exposes the lock lifetime, returning T requires cloning, and returning Arc<T> fixes shared ownership and allocation in the public contract. This should be decided as one coherent API model rather than as a return-type-only substitution.

Follow-up to #215 and #233. Related discussion: #222.

Activity

  1. added
    enhancement
    on Aug 30, 2026
  2. tisonkun commented on Aug 30, 2026

    @tisonkun
    MemberAuthor

    @QwQBiG Here are some rough ideas I instructed GPT to outline to polish the watch primitives.

    I suppose some of them can be reasonable, while others may be LLM hallucinations.

    You can take this input and see whether the contract of watch can be refined and improved (it's possible nothing needs to change).

    I can handle this when I have time, but I think it's a fair design quiz, so I'd like to leave it to you first.

  3. tisonkun commented on Aug 30, 2026

    @tisonkun
    MemberAuthor

    asyncband-watch-api-review.md

    Here is my chat with GPT in Chinese. I may disagree some GPT outputs so need not to follow what GPT says one by one.

  4. QwQBiG commented on Aug 30, 2026

    @QwQBiG
    Member

    Thanks for the design quiz! :P

    GPT hallucinates, but so do I—maybe peer-reviewing each other under pressure will keep us both honest…

    Give me 48 hours to dig into the existing contract and work through the ownership/liveness trade-offs before touching the code. I’ll follow up either way tomorrow. If I still don’t have a defensible proposal by then, I won’t force a half-baked solution—I'll let you know so you can take a look whenever you have bandwidth.

    And…晚安~

  5. QwQBiG commented on Aug 31, 2026

    @QwQBiG
    Member

    Hi~ @tisonkun

    After going through the issue and the current implementation once more, I agree with the issue’s central framing: this should be settled as one coherent ownership model, not as a few isolated return-type changes.

    Among the options already laid out in the issue, my preference is the borrowed core—Ref<'_, T> for access, changed() -> Result<(), RecvError> for notification, together with send_replace(T) -> T. The current Arc<T> API from #233 is sound: it supports non-Clone values, returns owning snapshots, and does not leave callers holding the channel lock. Its trade-off is that shared ownership and an allocation per publication become part of the public contract. If Asyncband wants the more ownership-neutral primitive, the borrowed direction looks cleaner to me. If a full redesign is not worthwhile, however, I think keeping the current Arc<T> model unchanged would be better than adopting only part of the proposal.

    My main implementation concern is that a borrowed design should separate the value lock from version and waiter state. A retained Ref would then block publication, but should not block endpoint lifecycle or waiter bookkeeping. The documentation would also need to clearly warn against holding a Ref across .await.

    I would defer recv() and the optional sender inspection methods until their use cases and exact semantics are clearer. In particular, I checked async-watch::recv(): it calls changed().await and then borrow().clone(), so another publication between those operations means it does not guarantee an atomic same-version snapshot. I would not copy that convenience without first deciding whether this race is part of the intended contract.

    Since you mentioned that you could handle this when you have time, I’d like to leave the implementation to you rather than add a speculative patch or more review noise. This is my current reading of the trade-offs, and I’ll stop here and learn from whichever final model you choose. Thanks again for the design quiz! :D

  6. tisonkun commented on Aug 31, 2026

    @tisonkun
    MemberAuthor

    OK. I will consider this today.

    Since you mentioned that you could handle this when you have time, I’d like to leave the implementation to you rather than add a speculative patch or more review noise.

    Actually, I mean to leave it to you altogether. But I'd prefer to include this API design decision in the upcoming 0.7 release and it may require some experience on API design, I can handle it myself now. However, I'd appreciate it if more contributors would consider what asyncband's APIs should look like, whether they are reasonable to deliver, and whether they have good ergonomics for downstream users.

  7. QwQBiG commented on Aug 31, 2026

    @QwQBiG
    Member

    Got it—thanks for clarifying! I misunderstood “leave it to you” in exactly the opposite direction TwT

    Given that this will shape the 0.7 public API, it makes sense for you to drive the implementation. I’ll keep following the design and review the final diff from a downstream user’s perspective. If I spot a concrete ergonomics issue or useful example, I’ll chime in.

    Looking forward to learning from this one!

  8. tisonkun commented on Aug 31, 2026

    @tisonkun
    MemberAuthor

    @QwQBiG You may check your email inbox :D

  9. tisonkun commented on Aug 31, 2026

    @tisonkun
    MemberAuthor

    Decision range:

    1. Sender
      • Cloneable
      • send(T) -> Result<(), SendError>
      • send_replace(T) -> T
    2. Receiver
      • Cloneable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions