Combine retain cycle created in .flatMap & Fail

This issue can be recreated by copying the below code into a playground. You will see that in the first example below the publisher is not released when using .flatMap with Fail

Code Block
import Combine
import Foundation
struct SampleError: Error {}
weak var weakPublisher: CurrentValueSubject<SampleError, Never>!
autoreleasepool {
  let publisher = CurrentValueSubject<SampleError, Never>(SampleError())
  weakPublisher = publisher
   
  publisher
    .flatMap(Fail<Void, SampleError>.init(error:))
    .sink(
      receiveCompletion: { print("completed \($0)") },
      receiveValue: { print("value \($0)") }
    )
    .cancel()
}
assert(weakPublisher == nil, "should be nil BUT IS NOT !!!")


In this second example, the publisher is released as expected using .tryMap with throw

Code Block
import Combine
import Foundation
struct SampleError: Error {}
weak var weakPublisher2: CurrentValueSubject<SampleError, Never>!
autoreleasepool {
  let publisher = CurrentValueSubject<SampleError, Never>(SampleError())
  weakPublisher2 = publisher
   
  publisher
    .tryMap{ throw $0 }
    .sink(
      receiveCompletion: { print("completed \($0)") },
      receiveValue: { print("value \($0)") }
    )
    .cancel()
}
assert(weakPublisher2 == nil, "is nil as expected")

Is this the expected behavior?

I've just stumbled upon the same issue. At first, I had something more complex going on in flatMap and assumed that I created a retain cycle there myself. Then I dumbed down flatMap to just returning Fail, just as in the example above and saw a memory leak. I replaced flatMap with tryMap and the leak was gone. Sadly, for my needs, I do need that flatMap, but I certainly don't understand the reason for that leak...
Combine retain cycle created in .flatMap &amp; Fail
 
 
Q