Skip to content

Semantics of Lwt.pick / choose #856

Closed

Description

@cyberhuman

The documentation for Lwt.pick says:

It's possible for multiple promises in ps to become resolved simultaneously. This happens most often when some promises ps are already resolved at the time Lwt.pick is called.

In that case, if at least one of the promises is rejected, the result promise p is rejected with the same exception as one such promise, chosen arbitrarily. If all promises are fulfilled, p is fulfilled with the value of one of the promises, also chosen arbitrarily.

However, the first sentence in the second paragraph isn't true, judging by the code
https://github.com/ocsigen/lwt/blob/master/src/core/lwt.ml#L2753-L2755

a simple test confirms this:

let rec loop (ok, err) = function
  | n when n <= 0 -> Lwt_io.printlf "ok %d err %d" ok err
  | n ->
  let%lwt res =
    let exception Err of (int * int) in
    try%lwt Lwt.pick [ Lwt.return (succ ok, err); Lwt.fail (Err (ok, succ err)); ] with Err x -> Lwt.return x
  in
  loop res (pred n)
in
loop (0, 0) 10;;
ok 5 err 5

Activity

  1. raphael-proust commented on May 25, 2021

    @raphael-proust
    Collaborator

    Hello,

    Your diagnostic is correct. In fact, when multiple promises are already resolved, pick simply takes a random one of them. It doesn't special case whether some of the already resolved promises are rejected or not.

    I'll prepare an MR to fix that.

  2. added a commit that references this issue on Jul 24, 2021
    7c5b453
  3. added a commit that references this issue on Nov 7, 2021
    c2f77f8
  4. added a commit that references this issue on Nov 17, 2021
    7c36522
  5. added a commit that references this issue on Jan 3, 2022
    43a920c
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions