Spaces:
Runtime error
Runtime error
da03
commited on
Commit
·
3f1526b
1
Parent(s):
b682e6d
- main.py +26 -7
- static/index.html +15 -9
main.py
CHANGED
|
@@ -132,8 +132,8 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
| 132 |
connection_counter = 0
|
| 133 |
|
| 134 |
# Connection timeout settings
|
| 135 |
-
CONNECTION_TIMEOUT =
|
| 136 |
-
WARNING_TIME =
|
| 137 |
|
| 138 |
# Create a thread pool executor
|
| 139 |
thread_executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
|
@@ -391,7 +391,9 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 391 |
current_time = time.perf_counter()
|
| 392 |
time_since_activity = current_time - last_user_activity_time
|
| 393 |
|
| 394 |
-
|
|
|
|
|
|
|
| 395 |
if time_since_activity >= WARNING_TIME and not timeout_warning_sent:
|
| 396 |
print(f"[{current_time:.3f}] Sending timeout warning to client {client_id}")
|
| 397 |
await websocket.send_json({
|
|
@@ -399,40 +401,56 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 399 |
"timeout_in": CONNECTION_TIMEOUT - WARNING_TIME
|
| 400 |
})
|
| 401 |
timeout_warning_sent = True
|
|
|
|
| 402 |
|
| 403 |
-
# Close connection at
|
| 404 |
if time_since_activity >= CONNECTION_TIMEOUT:
|
| 405 |
-
print(f"[{current_time:.3f}] Closing connection {client_id} due to timeout")
|
|
|
|
| 406 |
|
| 407 |
# Clear the input queue before closing
|
|
|
|
|
|
|
| 408 |
while not input_queue.empty():
|
| 409 |
try:
|
| 410 |
input_queue.get_nowait()
|
| 411 |
input_queue.task_done()
|
| 412 |
except asyncio.QueueEmpty:
|
| 413 |
break
|
|
|
|
| 414 |
|
|
|
|
| 415 |
await websocket.close(code=1000, reason="User inactivity timeout")
|
|
|
|
| 416 |
return
|
| 417 |
|
| 418 |
await asyncio.sleep(1) # Check every second
|
| 419 |
|
| 420 |
except Exception as e:
|
| 421 |
print(f"[{time.perf_counter():.3f}] Error in timeout check for client {client_id}: {e}")
|
|
|
|
|
|
|
| 422 |
break
|
| 423 |
|
| 424 |
# Function to update user activity
|
| 425 |
def update_user_activity():
|
| 426 |
nonlocal last_user_activity_time, timeout_warning_sent
|
|
|
|
| 427 |
last_user_activity_time = time.perf_counter()
|
|
|
|
|
|
|
|
|
|
| 428 |
if timeout_warning_sent:
|
| 429 |
print(f"[{time.perf_counter():.3f}] User activity detected, resetting timeout warning for client {client_id}")
|
| 430 |
timeout_warning_sent = False
|
|
|
|
| 431 |
# Send activity reset notification to client
|
| 432 |
asyncio.create_task(websocket.send_json({"type": "activity_reset"}))
|
|
|
|
| 433 |
|
| 434 |
# Start timeout checking
|
| 435 |
timeout_task = asyncio.create_task(check_timeout())
|
|
|
|
| 436 |
|
| 437 |
async def process_input(data):
|
| 438 |
nonlocal previous_frame, hidden_states, keys_down, frame_num, frame_count, is_processing
|
|
@@ -465,7 +483,7 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 465 |
else:
|
| 466 |
# Update user activity for non-auto inputs
|
| 467 |
update_user_activity()
|
| 468 |
-
print(f'[{time.perf_counter():.3f}] Processing: x: {x}, y: {y}, is_left_click: {is_left_click}, is_right_click: {is_right_click}, keys_down_list: {keys_down_list}, keys_up_list: {keys_up_list}')
|
| 469 |
|
| 470 |
# Update the set based on the received data
|
| 471 |
for key in keys_down_list:
|
|
@@ -653,7 +671,8 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 653 |
except WebSocketDisconnect:
|
| 654 |
# Log final EOS entry
|
| 655 |
log_interaction(client_id, {}, is_end_of_session=True)
|
| 656 |
-
print(f"WebSocket disconnected: {client_id}")
|
|
|
|
| 657 |
break
|
| 658 |
|
| 659 |
except Exception as e:
|
|
|
|
| 132 |
connection_counter = 0
|
| 133 |
|
| 134 |
# Connection timeout settings
|
| 135 |
+
CONNECTION_TIMEOUT = 20 # 20 seconds timeout
|
| 136 |
+
WARNING_TIME = 10 # 10 seconds warning before timeout
|
| 137 |
|
| 138 |
# Create a thread pool executor
|
| 139 |
thread_executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
|
|
|
| 391 |
current_time = time.perf_counter()
|
| 392 |
time_since_activity = current_time - last_user_activity_time
|
| 393 |
|
| 394 |
+
print(f"[{current_time:.3f}] Timeout check - time_since_activity: {time_since_activity:.1f}s, WARNING_TIME: {WARNING_TIME}s, CONNECTION_TIMEOUT: {CONNECTION_TIMEOUT}s")
|
| 395 |
+
|
| 396 |
+
# Send warning at 10 seconds
|
| 397 |
if time_since_activity >= WARNING_TIME and not timeout_warning_sent:
|
| 398 |
print(f"[{current_time:.3f}] Sending timeout warning to client {client_id}")
|
| 399 |
await websocket.send_json({
|
|
|
|
| 401 |
"timeout_in": CONNECTION_TIMEOUT - WARNING_TIME
|
| 402 |
})
|
| 403 |
timeout_warning_sent = True
|
| 404 |
+
print(f"[{current_time:.3f}] Timeout warning sent, timeout_warning_sent: {timeout_warning_sent}")
|
| 405 |
|
| 406 |
+
# Close connection at 20 seconds
|
| 407 |
if time_since_activity >= CONNECTION_TIMEOUT:
|
| 408 |
+
print(f"[{current_time:.3f}] TIMEOUT REACHED! Closing connection {client_id} due to timeout")
|
| 409 |
+
print(f"[{current_time:.3f}] time_since_activity: {time_since_activity:.1f}s >= CONNECTION_TIMEOUT: {CONNECTION_TIMEOUT}s")
|
| 410 |
|
| 411 |
# Clear the input queue before closing
|
| 412 |
+
queue_size_before = input_queue.qsize()
|
| 413 |
+
print(f"[{current_time:.3f}] Clearing input queue, size before: {queue_size_before}")
|
| 414 |
while not input_queue.empty():
|
| 415 |
try:
|
| 416 |
input_queue.get_nowait()
|
| 417 |
input_queue.task_done()
|
| 418 |
except asyncio.QueueEmpty:
|
| 419 |
break
|
| 420 |
+
print(f"[{current_time:.3f}] Input queue cleared, size after: {input_queue.qsize()}")
|
| 421 |
|
| 422 |
+
print(f"[{current_time:.3f}] About to close WebSocket connection...")
|
| 423 |
await websocket.close(code=1000, reason="User inactivity timeout")
|
| 424 |
+
print(f"[{current_time:.3f}] WebSocket.close() called, returning from check_timeout")
|
| 425 |
return
|
| 426 |
|
| 427 |
await asyncio.sleep(1) # Check every second
|
| 428 |
|
| 429 |
except Exception as e:
|
| 430 |
print(f"[{time.perf_counter():.3f}] Error in timeout check for client {client_id}: {e}")
|
| 431 |
+
import traceback
|
| 432 |
+
traceback.print_exc()
|
| 433 |
break
|
| 434 |
|
| 435 |
# Function to update user activity
|
| 436 |
def update_user_activity():
|
| 437 |
nonlocal last_user_activity_time, timeout_warning_sent
|
| 438 |
+
old_time = last_user_activity_time
|
| 439 |
last_user_activity_time = time.perf_counter()
|
| 440 |
+
print(f"[{time.perf_counter():.3f}] User activity detected for client {client_id}")
|
| 441 |
+
print(f"[{time.perf_counter():.3f}] last_user_activity_time updated: {old_time:.3f} -> {last_user_activity_time:.3f}")
|
| 442 |
+
|
| 443 |
if timeout_warning_sent:
|
| 444 |
print(f"[{time.perf_counter():.3f}] User activity detected, resetting timeout warning for client {client_id}")
|
| 445 |
timeout_warning_sent = False
|
| 446 |
+
print(f"[{time.perf_counter():.3f}] timeout_warning_sent reset to: {timeout_warning_sent}")
|
| 447 |
# Send activity reset notification to client
|
| 448 |
asyncio.create_task(websocket.send_json({"type": "activity_reset"}))
|
| 449 |
+
print(f"[{time.perf_counter():.3f}] Activity reset message sent to client")
|
| 450 |
|
| 451 |
# Start timeout checking
|
| 452 |
timeout_task = asyncio.create_task(check_timeout())
|
| 453 |
+
print(f"[{time.perf_counter():.3f}] Timeout task started for client {client_id}")
|
| 454 |
|
| 455 |
async def process_input(data):
|
| 456 |
nonlocal previous_frame, hidden_states, keys_down, frame_num, frame_count, is_processing
|
|
|
|
| 483 |
else:
|
| 484 |
# Update user activity for non-auto inputs
|
| 485 |
update_user_activity()
|
| 486 |
+
print(f'[{time.perf_counter():.3f}] Processing: x: {x}, y: {y}, is_left_click: {is_left_click}, is_right_click: {is_right_click}, keys_down_list: {keys_down_list}, keys_up_list: {keys_up_list}, time_since_activity: {time.perf_counter() - last_user_activity_time:.3f}')
|
| 487 |
|
| 488 |
# Update the set based on the received data
|
| 489 |
for key in keys_down_list:
|
|
|
|
| 671 |
except WebSocketDisconnect:
|
| 672 |
# Log final EOS entry
|
| 673 |
log_interaction(client_id, {}, is_end_of_session=True)
|
| 674 |
+
print(f"[{time.perf_counter():.3f}] WebSocket disconnected: {client_id}")
|
| 675 |
+
print(f"[{time.perf_counter():.3f}] WebSocketDisconnect exception caught")
|
| 676 |
break
|
| 677 |
|
| 678 |
except Exception as e:
|
static/index.html
CHANGED
|
@@ -91,6 +91,12 @@
|
|
| 91 |
</div>
|
| 92 |
|
| 93 |
<div class="canvas-container">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
<div class="canvas-wrapper">
|
| 95 |
<div class="instruction-box">Move your mouse inside to interact</div>
|
| 96 |
<canvas id="displayCanvas" width="512" height="384"></canvas>
|
|
@@ -114,12 +120,6 @@
|
|
| 114 |
<input class="form-check-input" type="checkbox" role="switch" id="autoInputToggle" checked>
|
| 115 |
<label class="form-check-label" for="autoInputToggle" id="autoInputLabel">Auto Input</label>
|
| 116 |
</div>
|
| 117 |
-
|
| 118 |
-
<div id="timeoutWarning" class="alert alert-warning" style="display: none; margin-top: 10px;">
|
| 119 |
-
<strong>Connection Timeout Warning:</strong>
|
| 120 |
-
No user activity detected. Connection will be dropped in <span id="timeoutCountdown">15</span> seconds.
|
| 121 |
-
<button type="button" class="btn btn-sm btn-primary ms-2" onclick="resetTimeout()">Stay Connected</button>
|
| 122 |
-
</div>
|
| 123 |
</div>
|
| 124 |
</div>
|
| 125 |
|
|
@@ -279,7 +279,7 @@
|
|
| 279 |
|
| 280 |
// Timeout countdown mechanism
|
| 281 |
let timeoutCountdownInterval = null;
|
| 282 |
-
let timeoutCountdown =
|
| 283 |
let timeoutWarningActive = false;
|
| 284 |
|
| 285 |
// Track currently pressed keys
|
|
@@ -369,7 +369,7 @@
|
|
| 369 |
clearInterval(timeoutCountdownInterval);
|
| 370 |
}
|
| 371 |
|
| 372 |
-
timeoutCountdown =
|
| 373 |
timeoutWarningActive = true;
|
| 374 |
|
| 375 |
// Show warning
|
|
@@ -400,7 +400,13 @@
|
|
| 400 |
}
|
| 401 |
|
| 402 |
timeoutWarningActive = false;
|
| 403 |
-
timeoutCountdown =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
// Hide warning
|
| 406 |
const warning = document.getElementById('timeoutWarning');
|
|
|
|
| 91 |
</div>
|
| 92 |
|
| 93 |
<div class="canvas-container">
|
| 94 |
+
<div id="timeoutWarning" class="alert alert-warning" style="display: none; margin-bottom: 15px;">
|
| 95 |
+
<strong>Connection Timeout Warning:</strong>
|
| 96 |
+
No user activity detected. Connection will be dropped in <span id="timeoutCountdown">10</span> seconds.
|
| 97 |
+
<button type="button" class="btn btn-sm btn-primary ms-2" onclick="resetTimeout()">Stay Connected</button>
|
| 98 |
+
</div>
|
| 99 |
+
|
| 100 |
<div class="canvas-wrapper">
|
| 101 |
<div class="instruction-box">Move your mouse inside to interact</div>
|
| 102 |
<canvas id="displayCanvas" width="512" height="384"></canvas>
|
|
|
|
| 120 |
<input class="form-check-input" type="checkbox" role="switch" id="autoInputToggle" checked>
|
| 121 |
<label class="form-check-label" for="autoInputToggle" id="autoInputLabel">Auto Input</label>
|
| 122 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
</div>
|
| 124 |
</div>
|
| 125 |
|
|
|
|
| 279 |
|
| 280 |
// Timeout countdown mechanism
|
| 281 |
let timeoutCountdownInterval = null;
|
| 282 |
+
let timeoutCountdown = 10;
|
| 283 |
let timeoutWarningActive = false;
|
| 284 |
|
| 285 |
// Track currently pressed keys
|
|
|
|
| 369 |
clearInterval(timeoutCountdownInterval);
|
| 370 |
}
|
| 371 |
|
| 372 |
+
timeoutCountdown = 10;
|
| 373 |
timeoutWarningActive = true;
|
| 374 |
|
| 375 |
// Show warning
|
|
|
|
| 400 |
}
|
| 401 |
|
| 402 |
timeoutWarningActive = false;
|
| 403 |
+
timeoutCountdown = 10;
|
| 404 |
+
|
| 405 |
+
// Reset the countdown display to 10 seconds
|
| 406 |
+
const countdownElement = document.getElementById('timeoutCountdown');
|
| 407 |
+
if (countdownElement) {
|
| 408 |
+
countdownElement.textContent = timeoutCountdown;
|
| 409 |
+
}
|
| 410 |
|
| 411 |
// Hide warning
|
| 412 |
const warning = document.getElementById('timeoutWarning');
|