ReactiveX / RxPY

ReactiveX for Python

Home Page:https://rxpy.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

take(0) does not dispose the source

gabis-precog opened this issue · comments

RxPy version: 3.2.0
Python: 3.8, 3.9, 3.10

I am implementing Rsocket (rsocket.io) in python (official repository rsocket/rsocket-py on github)

I am attempting integration with rxpy

As i am not that proficient in rxpy, and am basically bumbling my way around it, i'm not sure if the issue is with my implementation, or an edge case in rxpy.

these seems to be a shortcut taken in the implementation of take(count), specifically this from "rx/core/operators/take.py:25"

if not count: return empty()

which removes/disconnects the 'source' from the pipeline.

Due to this, when take(0) is one of the operators on the pipe, a cancel event (triggered by dispose) is not sent to the rsocket server.

I wasn't able to reproduce this using purly rxpy.

An example which reproduces this exists in the rsocket-py repository (mentioned above) in the following test:

tests.rx_support.test_rx_canceled.test_rx_support_request_stream_take_only_n

it can be observed by changing the parametrization and adding or setting one of them to 0.

Hi, thanks for submitting the issue. The reason why it's not cancelled is that it was never subscribed. The source should only attach at subscribe time, so if it's never subscribed then there's no reason to dispose either.

@dbrattli ok. so now my question would be if the edge case optimization is more important than consistency.

What I'm trying to say is that you need to make your implementation so the resources (e.g server connection) that you want to dispose are created at subscribe time. So if the subscribe function given to rx.create() is not called then it will not get any dispose call either.

PS: Note that operators.take is just a plain function. So if you need different semantics (which is totally fine), you can easily make your own take function and use it in your library. Just copy the take code, modify and use.

@dbrattli thank you. I have looked at my code again and have reached enlightenment ( i think ).