sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sanic crashed with KeyError: 'Sanic-Server-0-0' in `serve` function while was running normally

NikPaushkin opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Sanic function serve crashed at line 858 with the error message Experienced exception while trying to serve, while server was working normally before.

Here is the full traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/sanic/worker/manager.py", line 316, in _sync_states
    state = self.worker_state[process.name].get("state")
            ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "<string>", line 2, in __getitem__
  File "/usr/local/lib/python3.11/multiprocessing/managers.py", line 837, in _callmethod
    raise convert_to_error(kind, result)
KeyError: 'Sanic-Server-0-0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/sanic/mixins/startup.py", line 853, in serve
    manager.run()
  File "/usr/local/lib/python3.11/site-packages/sanic/worker/manager.py", line 122, in run
    self.monitor()
  File "/usr/local/lib/python3.11/site-packages/sanic/worker/manager.py", line 224, in monitor
    self._sync_states()
  File "/usr/local/lib/python3.11/site-packages/sanic/worker/manager.py", line 318, in _sync_states
    process.set_state(ProcessState.TERMINATED, True)
  File "/usr/local/lib/python3.11/site-packages/sanic/worker/process.py", line 41, in set_state
    **self.worker_state[self.name],
      ~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "<string>", line 2, in __getitem__
  File "/usr/local/lib/python3.11/multiprocessing/managers.py", line 837, in _callmethod
    raise convert_to_error(kind, result)
KeyError: 'Sanic-Server-0-0'

Code snippet

I run Sanic like this in dozens of projects:

if __name__ == '__main__':
    ssl = None
    if SSL_CERT_PATH and SSL_KEY_PATH:
        ssl = {'cert': SSL_CERT_PATH, 'key': SSL_KEY_PATH}

    try:
        app.run(host=listen_host, port=listen_port, ssl=ssl)
    except KeyboardInterrupt:
        logging.info("Got KeyboardInterrupt. App terminated successfully.")
        exit(0)

Expected Behavior

Working without crash

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Ubuntu 22

Sanic Version

23.3.0

Additional context

  • Python 3.11
  • sanic-ext==23.3.0
  • aiohttp==3.8.4

I'm facing such crash for the first time. I migrated it to Python 3.11 + v23.3.0 from v21.* recently, was not an issue before.

It seems like it happened during the SIGTERM signal processing, I will reopen if I'm able to reproduce again.

Interesting. Looks like it happened during shutdown, and perhaps a race condition. Please keep me updated if you see something like this again.