Crash using sequence()
Closed this issue · 3 comments
The following test case reproduces a problem I was seeing:
func testUtilsLargeSequence() {
let promises = (1...100).map { _ in Promise<Int,NoError>() }
let futures = promises.map { $0.future }
let e = self.expectation()
for (i, promise) in promises.enumerate() {
Queue.global.async {
sleep(1)
promise.success(i)
}
}
futures.sequence().onSuccess { nums in
for (index, num) in nums.enumerate() {
XCTAssertEqual(index, num)
}
e.fulfill()
}
self.waitForExpectationsWithTimeout(2, handler: nil)
}
When I run this test case, it crashes usually with a backtrace that is nearly 3000 frames long. My guess is that something about how sequence() works creates a chain of onComplete callbacks which ends up blowing out the stack
Thanks for reporting the issue! I'm not yet sure what the best solution should be, but one workaround would be to not use the ImmediateExecutionContext and use an asynchronous context instead. Unfortunately sequence does not take a context as a parameter, but you could try to use traverse directly with a different context.
Bolts solves this in the following way: https://github.com/BoltsFramework/Bolts-iOS/blob/master/Bolts/Common/BFExecutor.m#L57