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

Connection goes silent without errors (Binance) #96

Open
rm-rf-etc opened this issue May 13, 2021 · 5 comments
Open

Connection goes silent without errors (Binance) #96

rm-rf-etc opened this issue May 13, 2021 · 5 comments

Comments

@rm-rf-etc
Copy link

rm-rf-etc commented May 13, 2021

Erlang version: 21.3
Elixir: 1.11.4
WebSockex: 0.4.2
Computer: MacBook Air (M1, 2020)
OS: Big Sur v11.0.1

Subscribing to the WS feed from Binance results in a successful connection and the first 2 - 3 messages come in as expected, however, the connection then goes silent. I don't see any indication of the connection closing. I've enabled tracing using debug: [:trace], but this also doesn't yield any clues (that I can see). Included is a minimal reproduction of the bug:

defmodule Example do
  use WebSockex

  @url "wss://stream.binance.com:9443/ws/ethusdt"

  @subscribe_msg ~s({"id":1,"method":"SUBSCRIBE","params":["ethusdt@kline_1m"]})

  def start_link() do
    {:ok, pid} = WebSockex.start_link(@url, __MODULE__, :fake_state)
    WebSockex.send_frame(pid, {:text, @subscribe_msg})

    {:ok, pid}
  end

  def handle_frame({type, msg}, state) do
    IO.puts("Received Message - Type: #{inspect(type)} -- Message: #{inspect(msg)}")
    {:ok, state}
  end

  def handle_cast({:send, {type, msg} = frame}, state) do
    IO.puts("Sending #{type} frame with payload: #{msg}")
    {:reply, frame, state}
  end
end

I don't think the server is the cause of the problem, because it seems to work as expected when I test with wscat:

wscat -c 'wss://stream.binance.com:9443/ws/ethusdt'
Connected (press CTRL+C to quit)
> {"id":1,"method":"SUBSCRIBE","params":["ethusdt@kline_1m"]}
< {"result":null,"id":1}
< {"e":"kline","E":1620887236596,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829531,"o":"3996.07000000","c":"3989.84000000","h":"3996.11000000","l":"3988.82000000","v":"105.39922000","n":348,"x":false,"q":"420728.46134770","V":"31.94498000","Q":"127512.32144190","B":"0"}}
< {"e":"kline","E":1620887238601,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829585,"o":"3996.07000000","c":"3989.83000000","h":"3996.11000000","l":"3988.82000000","v":"114.74677000","n":402,"x":false,"q":"458023.10700630","V":"39.87059000","Q":"159134.03102530","B":"0"}}
< {"e":"kline","E":1620887240673,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829631,"o":"3996.07000000","c":"3989.62000000","h":"3996.11000000","l":"3988.82000000","v":"131.52760000","n":448,"x":false,"q":"524975.86242910","V":"50.03652000","Q":"199694.45886110","B":"0"}}
< {"e":"kline","E":1620887242751,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829645,"o":"3996.07000000","c":"3989.65000000","h":"3996.11000000","l":"3988.82000000","v":"132.11121000","n":462,"x":false,"q":"527304.13949430","V":"50.12365000","Q":"200042.07585250","B":"0"}}
< {"e":"kline","E":1620887244861,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829679,"o":"3996.07000000","c":"3988.34000000","h":"3996.11000000","l":"3988.34000000","v":"143.28122000","n":496,"x":false,"q":"571863.00172220","V":"58.58588000","Q":"233800.39506240","B":"0"}}
< {"e":"kline","E":1620887247017,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829698,"o":"3996.07000000","c":"3988.34000000","h":"3996.11000000","l":"3988.34000000","v":"151.36197000","n":515,"x":false,"q":"604091.85678690","V":"66.24685000","Q":"264355.02476190","B":"0"}}
< {"e":"kline","E":1620887249037,"s":"ETHUSDT","k":{"t":1620887220000,"T":1620887279999,"s":"ETHUSDT","i":"1m","f":412829184,"L":412829739,"o":"3996.07000000","c":"3986.62000000","h":"3996.11000000","l":"3986.62000000","v":"161.63306000","n":556,"x":false,"q":"645045.04841330","V":"73.11274000","Q":"291728.58779110","B":"0"}}

...keeps going...
@rm-rf-etc rm-rf-etc changed the title Connection drops silently without errors (Binance) Connection goes silent without errors (Binance) May 13, 2021
@Azolo
Copy link
Owner

Azolo commented May 14, 2021

There was a bug with continuation frames in 0.4.2 can up update to 0.4.3 and try it?

@rm-rf-etc
Copy link
Author

There was a bug with continuation frames in 0.4.2 can up update to 0.4.3 and try it?

Ya, I tried that version too, same result.

@Azolo
Copy link
Owner

Azolo commented May 16, 2021

So, I'm NOT getting this with

Elixir 1.11.3
Erlang/OTP 23.2
WebSockex 0.4.3 & 0.4.2

I'll try to test with a different version of Erlang.

@rm-rf-etc
Copy link
Author

@Azolo that seems to be the fix! Thanks for the help. I had tried a number of versions of Erlang which are more recent than 21.3, but ran into trouble getting them to finish building, and also conflicts with wxmac. Fortunately it seems that all is working seamlessly with 23.2.

@Vagab
Copy link

Vagab commented Oct 4, 2022

@Azolo @rm-rf-etc I am getting the same issue now, but the socket works for a random(I guess) amount of time before the connection goes silent. What I mean by random is that sometimes it happens within hours, sometimes it happens within a week. I am not sure how to debug this case specifically because it's not easily reproducible and logs show nothing.

edit: Since I am not sure why that happens and how would one go about debugging it, I was thinking of a fix and a simple hacky solution would be to send pings and look for pongs and restart the websockex process if the pong doesn't come back in some time(I see that restarting the server actually fixes things), so would you please be able to provide me with some guidance as to how would I proceed with that idea and is that something you reckon is possible at all?

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

3 participants