Skip to content

Inconsistent ordering in select APIs #2135

Open
@betamos

Description

@betamos

Selecting over >=2 futures has subtle fairness issues when more than one future is ready. The current offering has a few inconsistencies in docs and naming:

  1. futures::select!: pseudo-random, documented
  2. futures::select_biased!: in-order, documented
  3. futures::future::{select, try_select, select_ok}: in-order, undocumented
  4. futures::future::select_all: in-order, documented that one should not rely on order
  5. futures::stream::select: round-robin, documented
  6. futures::stream::select_all: (based on FuturesUnordered - not sure), undocumented

Golang uses random order for select to prevent starvation, which is a hard-to-detect and problematic pitfall. IMO, any select API with a starvation risk should be explicitly documented as such. Even "don't rely on order" is too benign of a statement.

For user-facing behavior, I'd divide the behavior of the current offering into three classes:

  • Order specified in the API. (2)
  • Order not specified, but has mitigations against starvation. (1, 5, 6?)
  • Order not specified, but can cause starvation. (3, 4)
    • Ideally I'd suggest eliminating this class entirely in favor of either of the other two

Initially, adding coverage of these behaviors to the existing API docstrings would be nice. Happy to create a PR upon endorsement from a maintainer.

Regarding naming, I like the precedent set by the macros: select! is starvation-protected by default, and select_biased! has explicit ordering. Following the principle of least surprise, ideally that convention could be applied to the other APIs as well. Now, going down this road would introduce breaking changes. Maybe a maintainer can chime in?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-futureArea: futures::futureA-macroArea: macro relatedA-streamArea: futures::streamS-needs-api-designStatus: Before implementing this, a discussion or decision on the new API is needed.S-needs-decisionStatus: A decision on whether or not to do this is needed.docs

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions