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

Fix issue where self._streams set is used as channels but cannot be s… #398

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

briangu
Copy link

@briangu briangu commented Mar 12, 2021

Fix issue where self._streams set is used as channels but cannot be serialized.

When the websocket restarts due to closure, self._streams is used as channels but since it is a set, it cannot be serialized:

at alpaca-trade-api-python/alpaca_trade_api/stream2.py:100> exception=TypeError('Object of type set is not JSON serializable')>
Traceback (most recent call last):
File "/scm/alpaca-trade-api-python/alpaca_trade_api/stream2.py", line 108, in _ensure_ws
await self.subscribe(self._streams)
File "/scm/alpaca-trade-api-python/alpaca_trade_api/stream2.py", line 124, in subscribe
await self._ws.send(json.dumps({
File "/usr/local/lib/python3.9/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/local/lib/python3.9/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/lib/python3.9/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/local/lib/python3.9/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type set is not JSON serializable

@nickvertucci
Copy link

CAN WE GET THIS MERGED!!!

This is because Alpaca API gets disconnected midway. So the API tries to reconnect. But when it does so, the channels are not reinitialized, hence it remains a set that is not JSON serializable. Hence, in alpaca_trade_api > stream2.py change 'streams': channels to 'streams': list(channels) in line 132.
That should solve the reconnecting to api and 1006 problem.

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

Successfully merging this pull request may close these issues.

2 participants