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 hanging close after Alpaca WebSocketException, add closing_timeout #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alpaca_trade_api/polygon/streamconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def connect(self):
await self._dispatch({'ev': 'status',
'status': 'connecting',
'message': 'Connecting to Polygon'})
self._ws = await websockets.connect(self._endpoint)
self._ws = await websockets.connect(self._endpoint, close_timeout=1)
self._stream = self._recv()

msg = await self._next()
Expand Down
6 changes: 3 additions & 3 deletions alpaca_trade_api/stream2.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, key_id: str, secret_key: str, base_url: URL):
self._consume_task = None

async def _connect(self):
ws = await websockets.connect(self._endpoint)
ws = await websockets.connect(self._endpoint, close_timeout=1)
await ws.send(json.dumps({
'action': 'authenticate',
'data': {
Expand Down Expand Up @@ -80,7 +80,7 @@ async def _consume_msg(self):
await self._dispatch(stream, msg)
except websockets.WebSocketException as wse:
logging.warn(wse)
await self.close()
self._ws = None
asyncio.ensure_future(self._ensure_ws())

async def _ensure_ws(self):
Expand All @@ -91,7 +91,7 @@ async def _ensure_ws(self):
try:
await self._connect()
if self._streams:
await self.subscribe(self._streams)
await self.subscribe(list(self._streams))
break
except websockets.WebSocketException as wse:
logging.warn(wse)
Expand Down