Does Combine's timeout operator cancel the upstream publisher after firing?
0 reputation · 21 Jul 2021, 23:08 UTC
I'm designing a networking layer in Swift using Combine (iOS 15+) where requests are wrapped with .timeout(_:scheduler:) so subscribers receive an error if no value arrives in time. My goal is to guarantee that when a timeout fires, the underlying work — for example a URLSession.DataTaskPublisher request — stops consuming resources rather than continuing in the background.
The Combine documentation indicates that timeout emits a failure downstream but does not cancel the upstream publisher, which suggests the data task may keep running after the timeout error is delivered. I'm unclear whether this holds for all upstream types, whether the scheduler choice affects it, and whether holding the pipeline's AnyCancellable versus releasing it changes the outcome.
My questions:
- After
timeoutemits its error, does the upstreamDataTaskPublisheractually keep its URLSession task alive, and how can I verify this reliably (e.g., Instruments or task state logging)? - Is there a documented, idiomatic way to tie upstream cancellation to the timeout firing, without abandoning the
timeoutoperator entirely? - Does the behavior differ between publisher types such as
Future,Justwithdelay, andDataTaskPublisher?