Add window overload that takes an executor to prevent stack overflow
authorWalker Mills <wmills@fb.com>
Thu, 19 Oct 2017 07:57:03 +0000 (00:57 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 19 Oct 2017 08:09:37 +0000 (01:09 -0700)
commitebded22a9dae565f3dc6aa4bf7593f52ec189cd0
tree0eb2d6ee0ec9a090ef2602696a2027e2796a784d
parent34a2f64fb65d4cb446e4ac7623a777191bffa43f
Add window overload that takes an executor to prevent stack overflow

Summary:
AIUI, if there is no executor available, then callbacks are executed inline. `folly::window` uses a recursive helper function (`spawn`) to handle chaining callbacks. So if `window` is used on a large enough collection of `Future`s without executors (e.g., created by `makeFuture`, or have otherwise already completed), you get a stack overflow. A minimal repro looks like:
```

int main(int argc, char** argv) {
  std::vector<int> v(100000);
  for(int i=0; i < v.size(); i++) {
    v[i] = i;
  }

  std::vector<folly::Future<folly::Unit>> f =
      folly::window(
          std::move(v),
          [](int /* unused */) { return folly::makeFuture(); },
          1);
  folly::collectAll(f).get();
}
```

This diff resolves the issue by adding an overload of `folly::window` which takes an executor as its first parameter. The executor-less `window` overload calls through to the new function using an `InlineExecutor` as the default executor.

Reviewed By: yfeldblum

Differential Revision: D6038733

fbshipit-source-id: 5dcab575592650efa2e106f12632ec06817a0009
folly/futures/Future-inl.h
folly/futures/helpers.h
folly/futures/test/WindowTest.cpp