fix(gateway): exit cleanly on replace startup races

This commit is contained in:
giugiu-a11y 2026-04-24 19:13:45 -03:00
parent 4fade39c90
commit ea0f07279e
No known key found for this signature in database
2 changed files with 131 additions and 8 deletions

View file

@ -11169,23 +11169,31 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =
from gateway.status import write_pid_file, remove_pid_file, get_running_pid
_current_pid = get_running_pid()
if _current_pid is not None and _current_pid != os.getpid():
logger.error(
message = (
"Another gateway instance (PID %d) started during our startup. "
"Exiting to avoid double-running.", _current_pid
"Exiting to avoid double-running."
)
if replace:
logger.info(message, _current_pid)
return True
logger.error(message, _current_pid)
return False
if not acquire_gateway_runtime_lock():
logger.error(
"Gateway runtime lock is already held by another instance. Exiting."
)
message = "Gateway runtime lock is already held by another instance. Exiting."
if replace:
logger.info(message)
return True
logger.error(message)
return False
try:
write_pid_file()
except FileExistsError:
release_gateway_runtime_lock()
logger.error(
"PID file race lost to another gateway instance. Exiting."
)
message = "PID file race lost to another gateway instance. Exiting."
if replace:
logger.info(message)
return True
logger.error(message)
return False
atexit.register(remove_pid_file)
atexit.register(release_gateway_runtime_lock)