Skip to content

Commit d5ef39d

Browse files
committed
De-duping restarts so /web-vnc waits for a booting emulator.
1 parent c869c63 commit d5ef39d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

server/utils/server_startup_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ def _restart_emulators():
8181
try:
8282
logger.info(f" ---> Auto-restarting emulator for {email}...")
8383

84+
# Use RequestManager to prevent contention with other requests
85+
from server.core.request_manager import RequestManager
86+
87+
# Create a request manager for the restart operation
88+
# Using a special path to identify restart operations
89+
restart_manager = RequestManager(email, "/internal/restart-emulator", "POST")
90+
91+
# Try to claim this restart operation
92+
if not restart_manager.claim_request():
93+
# Another process is already restarting this emulator
94+
logger.info(f"Another process is already restarting emulator for {email}, skipping")
95+
continue
96+
8497
# Check if this profile's emulator is already in use (local development only)
8598
if platform.system() == "Darwin":
8699
(
@@ -95,6 +108,9 @@ def _restart_emulators():
95108
)
96109
logger.info(f"On local development, only one profile per emulator is allowed")
97110
failed_restarts.append(email)
111+
restart_manager.store_response(
112+
{"success": False, "error": f"Emulator already in use by {other_email}"}, 409
113+
)
98114
continue
99115

100116
# Use email override context to ensure proper email routing
@@ -116,15 +132,27 @@ def _restart_emulators():
116132
logger.debug(
117133
f"Marked emulator {automator.device_id} as in use by {email}"
118134
)
135+
136+
# Mark the restart as complete
137+
restart_manager.store_response(
138+
{"success": True, "message": f"Emulator restarted for {email}"}, 200
139+
)
119140
else:
120141
logger.error(f"✗ Failed to initialize driver for {email}", exc_info=True)
121142
failed_restarts.append(email)
143+
restart_manager.store_response(
144+
{"success": False, "error": "Failed to initialize driver"}, 500
145+
)
122146
else:
123147
logger.error(f"✗ Failed to start emulator for {email}: {message}", exc_info=True)
124148
failed_restarts.append(email)
149+
restart_manager.store_response({"success": False, "error": message}, 500)
125150

126151
except Exception as e:
127152
logger.error(f"✗ Error restarting emulator for {email}: {e}", exc_info=True)
153+
# Complete the request with error if we had registered it
154+
if "restart_manager" in locals():
155+
restart_manager.store_response({"success": False, "error": str(e)}, 500)
128156
logger.debug(
129157
f"Backtrace for error restarting emulator for {email}: {traceback.format_exc()}"
130158
)

0 commit comments

Comments
 (0)