-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stack overflows caused by MergeLimitedSink operators #2615
Comments
The simplest solution is to add |
To both sides of the if statement? Yes- I suppose that would work - but it was a surprise that using the API like this could cause this in the first place. Its weird to have to know to balance it like this. |
Very weird indeed. Especially given that if the body of the closure is only |
Yes, my point is that devs should feel free to pass any given Observable back to |
What is the performance hit? I see if I use: |
I'm seeing that if I take your fix and use your initial code, the stack size goes up to 42 but the runtime is only 3 seconds... Looks like a good fix to me (I assume all the tests that use |
yes tests pass both in this repo (they didn't auto run in CI though) and like 3000 unit tests that heavily rely on this func in our own codebase. When I tested with this function with device -iPhone XS:
I got 6.6 seconds runtime with my fix and about the same on master. |
I found an even simpler repro function for this issue, only the first inner subscription needs to be a delay (or anything that queues), then the
|
Let's move the discussion over to #2616 |
Short description of the issue:
At Dropbox, we use RxSwift heavily in a serial queue that must be FIFO to process something that requires us to wait sometimes (for reasons im not going to go into). We have 1 main observable that represents our input, and uses
concatMap
in conjunction with a.just()
and.delay()
to achieve this. The delay period, which is rare, is < 3 seconds. The input is generally all at once (10,000s of elements in short period of time). Since at least 2021, our top crash has been a stack overflow in Rx code we've never been able to address. This crash affects a minority of users on launch and is rarely reproduced until now...We found reproduction case (see sample code) that can cause stack overflows when using standard RxSwift
concatMap()
operator (orconcatMap/ merge(maxConcurrent:
in Merge.swift) when in combination with randomly delayed sequences. This example function will cause super deep stack traces (or cause a stack overflow crash directly if you are lucky). It seems to be important that we do not exclusively.delay
or not, only that there is a random mix of delayed and not delayed "just" elements.If you run this code, if a crash doesn't happen, you can at least see a super deep stack size inside
MergeLimitedSinkIter.on
with the Thread API (just printThread.callStackReturnAddresses.count
). This is the source of the S/O crash we are experiencing for some users.It is a concurrency issue where a
.just()
emitting immediately on the current queue seems to mess up all internal uses ofMergeLimitedSinkIter
(which is concat/concatMap/merge(maxConcurrnet:)).Expected outcome:
The above code sample should be protected against stack overflows by intelligently scheduling the next inner subscribe.
What actually happens:
Stack over flow that looks like this:
Self contained code example that reproduces the issue:
RxSwift/RxCocoa/RxBlocking/RxTest version/commit
we are on 6.6.0 but this has not been addressed or even noted.
Platform/Environment
How easy is to reproduce? (chances of successful reproduce after running the self contained code)
Xcode version:
15.4
Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)
The text was updated successfully, but these errors were encountered: