A Python interpreter symlink nearly became a restart loop
My WSGI startup file could restart itself under a configured Python interpreter. Before doing that, it compared the interpreter path with sys.executable.
The comparison used the path strings. Two different strings can lead to the same executable through symlinks.
The June 6 startup change compares resolved paths instead. It follows the deployment repair, where I also needed to account for the interpreter the hosting platform already selected.
Decide whether another exec is needed
The helper reads an optional VENV_PATH and constructs its bin/python3 path. The revised guard is:
already_active = os.path.realpath(sys.executable) == os.path.realpath(
python_bin
)
if not already_active and os.path.exists(python_bin):
os.execl(python_bin, python_bin, *sys.argv)
The intended operation is to replace the process only when the configured executable differs from the running one. If the process starts again and still decides it must replace itself, startup can repeat instead of reaching the application import.
Resolving aliases gives that check a more useful basis than comparing their spelling.
A small path example
For an illustrative filesystem layout, suppose these two paths point to the same underlying executable:
/opt/example-env/bin/python
/opt/example-env/bin/python3
The strings differ. Their resolved targets can be equal. A guard based only on string inequality would request another exec even though resolving the paths shows the same executable target.
A separate example of a symbolic link, by ColdShine, released into the public domain.
The helper now returns without re-executing in that case.
It also returns immediately when no virtual-environment path is configured. That is expected for the CloudLinux Python Selector arrangement in this deployment: lswsgi already launches with the selector-managed environment. Requiring another VENV_PATH was an assumption the entry point didn’t need to impose on that host.
Executable identity isn’t the whole environment
The resolved-path check addresses this restart decision. It doesn’t prove that every dependency is installed or that every environment variable is correct.
Two virtual environments can also refer to the same base Python executable while having different prefixes and package locations. Resolving the binary alone isn’t a complete virtual-environment identity test.
Those distinctions are reasons to check application imports and the selected environment separately. They don’t undo the narrower benefit of avoiding a restart based only on two spellings of the same target.
When the configured path resolves to the interpreter already running, the entry point can go ahead and create the WSGI application.