dotnet / reactive

The Reactive Extensions for .NET
http://reactivex.io
MIT License
6.71k stars 750 forks source link

Stream canceled sometimes Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 65 #1821

Closed Trades-King closed 1 year ago

Trades-King commented 2 years ago

Hi,

i have the latest npm package and get issue sometimes in Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 65

How fix it?

Exception: OpenAPI.Net.Exceptions.ReceiveException: An exception occurred while reading from stream ---> System.OperationCanceledException: The operation was canceled. at System.Threading.CancellationToken.ThrowOperationCanceledException() at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](TIOAdapter adapter) at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory1 buffer) at OpenAPI.Net.OpenClient.ReadTcp(CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Reactive.AnonymousSafeObserver1.OnError(Exception error) in //Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 65 at System.Reactive.Sink`1.ForwardOnError(Exception error) in //Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 61 at OpenAPI.Net.OpenClient.OnError(Exception exception) --- End of stack trace from previous location --- at System.Reactive.AnonymousSafeObserver1.OnError(Exception error) in /_/Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 65 at System.Reactive.Sink1.ForwardOnError(Exception error) in //Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 61 at OpenAPI.Net.OpenClient.OnError(Exception exception) --- End of stack trace from previous location --- at System.Reactive.AnonymousSafeObserver`1.OnError(Exception error) in //Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 65 at System.Reactive.Sink1.ForwardOnError(Exception error) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 61 at OpenAPI.Net.OpenClient.OnError(Exception exception) --- End of stack trace from previous location --- at System.Reactive.AnonymousSafeObserver1.OnError(Exception error) in //Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 65 at System.Reactive.Sink`1.ForwardOnError(Exception error) in //Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 61 at OpenAPI.Net.OpenClient.OnError(Exception exception)

Despite our best efforts, bugs can slip into releases or corner cases forgotten about. We will try our best to remedy the situation and/or provide workarounds. Note that certain (odd) behaviors are by design and as such are not considered bugs.

Which library version? last release

What are the platform(s), environment(s) and related component version(s)? dotnet

What is the use case or problem? Stream get sometimes canced

What is the expected outcome? transfer data as complete

What is the actual outcome? Stream will be closed

What is the stacktrace of the exception(s) if any?

How can i fix it?

idg10 commented 1 year ago

I'm guessing from the namespaces that you're using https://github.com/spotware/OpenAPI.Net

i have the latest npm package

We don't publish Rx.NET on npm, and I don't think that OpenAPI.Net library is on their either. Presumably you're using NuGet. And it's generally a good idea to specify the actual version of your libraries (both Rx and also OpenAPI.Net, since you seem to be using both) because "latest" is always a moving target.

Based on the stack trace, I would say that what's happened here is that something outside of Rx has supplied on OnError implementation that itself throws an exception. So this doesn't look like an Rx problem. If an observer's OnError itself throws an exception, then things have gone wrong in a way that is outside of Rx's responsibility, and the only thing it can do here is allow the exception to propagate up the stack. And that is apparently what has happened, so from Rx's perspective, you are reporting what looks like correct behaviour.

I see you also reported this at https://github.com/spotware/OpenAPI.Net/issues/26 and that someone has already described the issue:

It can happen sometimes if the server goes down temporarily

You rejected that explanation:

my connection cant be. I ping scorce. I must be a code issue.

I'm afraid you are mistaken. Individual connections can fail for all sorts of reasons, and often without the service as a whole failing. Connectivity comes and goes, sometimes in a very short amount of time. Sometimes intermediate networking components can cause individual connections to fail even though the system as a whole remains up. (E.g., if a service is hosed in a cloud platform such as Azure or AWS, load balancers in those platforms will sometimes terminate individual connections for reasons that are often unclear.)

If you are communicating with an external service, you absolutely need to be able to handle the fact that sometimes requests to that external service will fail. Often you'll need to use some sort of retry mechanism, because if you're in the sort of environment where this can happen even if the system overall is healthy (and that is the norm on cloud platforms). Rx doesn't have any networking features, so it's not its job to help you with this. Its job is to ensure any exceptions that occur are visible to your application one way or another, and it looks like it has done that correctly in this case.