Skip to content
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

WebSocket: Connection not closed when sole Subscription isStopped after an error #5312

Closed
kostadin-mandulov opened this issue Feb 13, 2020 · 7 comments

Comments

@kostadin-mandulov
Copy link

Bug Report

Current Behavior

When sole WebSocketSubject Subscription encounters an error, it isStopped and no longer handles anything, but the underlying connection is not closed.

Reproduction

Source: rxjs-ws-error-handling-test.zip

  1. Have node and npm installed. I've used versions 12.15.0 and 6.13.7 respectively.
  2. npm install
  3. node server.js
  4. Make sure the index.html is served via localhost. I've used Python 3: python3 -m http.server 1234
  5. Open your favourite modern browser and open its DevTools.
  6. Navigate to http://localhost:1234/
  7. Observing the logs in the console and the WS connection, we can see the following behaviour:
    • [0s] Connection is established (WebSocketSubject's openObserver handler called).
    • [0s] A message is received by the server (Subscription's next handler called).
    • [2s] A faulty message causes an error (Subscription's error handler called). The Subscription is now stopped, but the WebSocket connection remains opened (DevTools > Network > WS).
    • [4s] A message is received by the WebSocket connection but Subscription's next handler is NOT called.
    • [6s] The server closes the connection (WebSocketSubject's closeObserver handler called).

Expected behavior

If all of a WebSocketSubject's Subscriptions are stopped / closed, I believe the connection should also close.

Environment

  • Runtime:
    • Node v12.15.0
    • NPM v6.13.7
    • Chrome v79.0.3945.130
    • Firefox v73.0
    • Microsoft Edge v44.18362.449.0
  • RxJS version: 6.5.4
@keradus
Copy link

keradus commented Apr 24, 2020

I hit the same problem. any help on this one, folks ?

@ZenwalkerD
Copy link

I think i am facing same issue! Any solution? In dev window i still see that specific WS connection shown as 101 but not Finished even though i close the WebSocketSubject or do complete() call on it.

@splincode
Copy link

any news?

@kostadin-mandulov
Copy link
Author

Hey, @kwonoj 👋 I've noticed the issue you've created and pinned earlier this year, #5970. I was wondering if this is somehow related to why there are no responses to this issue. Should I create a new discussion and link this? I'm not sure, as it can be interpreted as both RxJS core bug and behavior change suggestion.

@bever1337
Copy link

bever1337 commented Sep 29, 2022

This is a really interesting bug, and I can imagine why there's been no traction. In this case, there's no error with the underlying WebSocket, but there is an error during de-serialization. Removing RxJS from the equation, throwing an error during an onmessage event would not terminate a network connection. It's an opinion to terminate the socket if an application fails to de-serialize a valid-but-unexpected WebSocket messages. All that said: RxJS did make the opinionated choice to emit an error when de-serialization fails. If the subject closes, so must the websocket.

I think the side-effect of fixing this bug is that some users may have already found a solution to intentionally unsubscribe from the subject and close the socket. In my experience, redundant calls to unsubscribe are a non-issue.

@maknapp
Copy link
Contributor

maknapp commented Oct 10, 2022

The websocket is closed when there are no more observers. When there is a JSON parsing error, all observers unsubscribe, resulting in the websocket closing. I tested this on v7.5.5 (demo here) and it seems to work fine (note demo websocket API key only lasts a week).

Has this issue already been fixed? Or can someone modify the demo above to reproduce the websocket not closing issue?

@kostadin-mandulov
Copy link
Author

As we worked around this, I never got around to rechecking this. But better late than never I suppose 😅

I've upgraded RxJS to the latest version, 7.8.1, and now it looks like the connection gets closed after the faulty message causes an error 🙌 Using Node 18.16.0 & NPM 9.8.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants