question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Websocket timed out after several episodes

See original GitHub issue

When I train my agent on the MoveToBeacon minigame map I get a “websocket timed out” Exception and the environment closes at irregular intervals. I use Linux. Here are the Exceptions:

I0704 01:36:53.095061 140421693589312 sc2_env.py:433] Starting episode: 8319
Error: expected response: 12, actual response: 0
Error reported: Game has already ended
I0704 01:38:53.473563 140421693589312 sc2_env.py:531] Environment Close
RequestQuit command received.
Closing Application...
unable to parse websocket frame.
Took 23098.302 seconds for 1996332 steps: 86.428 fps
Terminate action already called.
Entering core terminate.
Core shutdown finished.
I0704 01:38:56.193612 140421693589312 sc_process.py:197] Shutdown gracefully.
I0704 01:38:56.194391 140421693589312 sc_process.py:182] Shutdown with return code: 0
Traceback (most recent call last):
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_socket.py", line 81, in recv
    bytes_ = sock.recv(bufsize)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/protocol.py", line 62, in catch_websocket_connection_errors
    yield
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/protocol.py", line 161, in _read
    response_str = self._sock.recv()
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_core.py", line 300, in recv
    opcode, data = self.recv_data()
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_core.py", line 317, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_core.py", line 330, in recv_data_frame
    frame = self.recv_frame()
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_core.py", line 364, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_abnf.py", line 361, in recv_frame
    self.recv_header()
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_abnf.py", line 309, in recv_header
    header = self.recv_strict(2)
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_abnf.py", line 396, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_core.py", line 434, in _recv
    return recv(self.sock, bufsize)
  File "/home/wenzel/.local/lib/python3.6/site-packages/websocket/_socket.py", line 84, in recv
    raise WebSocketTimeoutException(message)
websocket._exceptions.WebSocketTimeoutException: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/wenzel/OC/Praktikum/SC2_Wenzel/MiniGames/MoveToBeaconAgent.py", line 169, in <module>
    app.run(main)
  File "/home/wenzel/.local/lib/python3.6/site-packages/absl/app.py", line 274, in run
    _run_main(main, args)
  File "/home/wenzel/.local/lib/python3.6/site-packages/absl/app.py", line 238, in _run_main
    sys.exit(main(argv))
  File "/home/wenzel/OC/Praktikum/SC2_Wenzel/MiniGames/MoveToBeaconAgent.py", line 139, in main
    run_loop.run_loop([agent], env, max_episodes=_EPISODES)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/env/run_loop.py", line 48, in run_loop
    timesteps = env.step(actions)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/stopwatch.py", line 197, in _stopwatch
    return func(*args, **kwargs)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/env/sc2_env.py", line 452, in step
    return self._step()
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/env/sc2_env.py", line 456, in _step
    self._parallel.run((c.step, self._step_mul) for c in self._controllers)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/run_parallel.py", line 56, in run
    return [funcs[0]()]
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/remote_controller.py", line 88, in _valid_status
    return func(self, *args, **kwargs)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/stopwatch.py", line 197, in _stopwatch
    return func(*args, **kwargs)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/remote_controller.py", line 206, in step
    return self._client.send(step=sc_pb.RequestStep(count=count))
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/protocol.py", line 136, in send
    res = self.send_req(sc_pb.Request(**kwargs))
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/protocol.py", line 122, in send_req
    return self.read()
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/stopwatch.py", line 197, in _stopwatch
    return func(*args, **kwargs)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/protocol.py", line 95, in read
    response = self._read()
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/protocol.py", line 161, in _read
    response_str = self._sock.recv()
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/wenzel/.conda/envs/SC2/lib/python3.6/site-packages/pysc2/lib/protocol.py", line 67, in catch_websocket_connection_errors
    raise ConnectionError("Websocket timed out.")
pysc2.lib.protocol.ConnectionError: Websocket timed out.
I0704 01:38:56.304697 140421693589312 sc2_env.py:531] Environment Close

And I start my training as follows:

    try:
        with sc2_env.SC2Env(
                map_name="MoveToBeacon",
                players=[sc2_env.Agent(sc2_env.Race.terran)],
                agent_interface_format=features.AgentInterfaceFormat(
                    feature_dimensions=features.Dimensions(screen=_SCREEN,
                                                           minimap=_MINIMAP),
                    use_feature_units=True
                ),
                step_mul=8,
                game_steps_per_episode=0,
                visualize=_VISUALIZE
        ) as env:

            run_loop.run_loop([agent], env, max_episodes=_EPISODES)

            

    except KeyboardInterrupt:
        pass

Is there something I am doing wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
parachutelcommented, Apr 29, 2020

It is still happening in SC2 4.10.

2reactions
inoryycommented, Nov 26, 2018

As a temporary fix you can close/reset the environment once you detect the timeout. Here’s how I do it for example: https://github.com/inoryy/reaver-pysc2/blob/master/reaver/envs/sc2.py#L53-L65

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebSocket times out after 100 seconds
Using System.Net.WebSocket with a server that responds with Content-Length: 0 in their HTTP 100 response will result in the connection being unexpectedly ......
Read more >
Timeouts - websockets 10.4 documentation
As a consequence, proxies may terminate WebSocket connections prematurely when no message was exchanged in 30 seconds. Keepalive in websockets#. To avoid these ......
Read more >
Implementing WebSocket communication in Next.js
Real-time communication on the web uses the WebSocket API, which is a full-duplex communication channel over a single TCP connection.
Read more >
Fixing the WebSocket Protocol Support issue when upgrading ...
“A real-time connection could not be established with the server. This may be caused by incorrect configuration.
Read more >
WebSocket Connection timeout - Stack Overflow
/** * inits a websocket by a given url, returned promise resolves with initialized websocket, rejects after failure/timeout. ... timeoutMs : 1500; numberOfRetries ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found