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 fails to start when the source files are frozen with cx_Freeze i.e. when only the pyc files are available

jangxx opened this issue · comments

Describe the bug
Running startup() on the http server fails with

Traceback (most recent call last):
  File "server\main.py", line 133, in async_main
  File "C:\path\venv\lib\site-packages\sanic\app.py", line 1541, in _startup
  File "C:\path\venv\lib\site-packages\sanic\touchup\service.py", line 24, in run
  File "C:\path\venv\lib\site-packages\sanic\touchup\schemes\base.py", line 27, in build
  File "C:\Program Files\Python310\lib\inspect.py", line 1147, in getsource
  File "C:\Program Files\Python310\lib\inspect.py", line 1129, in getsourcelines
  File "C:\Program Files\Python310\lib\inspect.py", line 958, in findsource
OSError: could not get source code

when the scripts are frozen with cx_Freeze and run on a different machine. I'm guessing this is due to the fact that Sanic tries to look in the original location of the source files which obviously doesn't exist on other machines.

Code snippet

http_server = await app.create_server(
	host="localhost", 
	port=64152, 
	return_asyncio_server=True
)
await http_server.startup()

Expected behavior
The server starts.

Environment (please complete the following information):

Sanic v22.6.2
Goin' Fast @ http://localhost:64152
mode: production, single worker
server: sanic, HTTP/1.1
python: 3.10.7
platform: Windows-10-10.0.19044
packages: sanic-routing==22.3.0

Additional context

As a workaround, you should be able to skip the touchup (seemingly an optimizer step) by running in debug mode, or setting the TOUCHUP config setting to False:

app.config.TOUCHUP = False

as per the source here:

sanic/sanic/app.py

Lines 1496 to 1497 in f891995

if self.state.is_debug and self.config.TOUCHUP is not True:
self.config.TOUCHUP = False

sanic/sanic/app.py

Lines 1523 to 1524 in f891995

if self.config.TOUCHUP:
TouchUp.run(self)

Correct, an optimization step. You should disable as shown above in this case.