Desmond-Dong commited on
Commit
3115532
·
1 Parent(s): bf2418d

"fix_shutdown_handling"

Browse files
Files changed (1) hide show
  1. reachy_mini_ha_voice/main.py +33 -4
reachy_mini_ha_voice/main.py CHANGED
@@ -9,6 +9,7 @@ import asyncio
9
  import logging
10
  import socket
11
  import threading
 
12
  from typing import Optional
13
 
14
  logger = logging.getLogger(__name__)
@@ -152,17 +153,45 @@ class ReachyMiniHaVoice(ReachyMiniApp):
152
  logger.info(" -> Generic Camera -> http://<ip>:8081/stream")
153
  logger.info("=" * 50)
154
 
155
- # Wait for stop signal
156
  while not stop_event.is_set():
157
- loop.run_until_complete(asyncio.sleep(0.5))
158
 
 
 
159
  except Exception as e:
160
  logger.error(f"Error running voice assistant: {e}")
161
  raise
162
  finally:
163
  logger.info("Shutting down voice assistant...")
164
- loop.run_until_complete(service.stop())
165
- loop.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  logger.info("Voice assistant stopped.")
167
 
168
 
 
9
  import logging
10
  import socket
11
  import threading
12
+ import time
13
  from typing import Optional
14
 
15
  logger = logging.getLogger(__name__)
 
153
  logger.info(" -> Generic Camera -> http://<ip>:8081/stream")
154
  logger.info("=" * 50)
155
 
156
+ # Wait for stop signal - use simple sleep to avoid blocking event loop
157
  while not stop_event.is_set():
158
+ time.sleep(0.1)
159
 
160
+ except KeyboardInterrupt:
161
+ logger.info("Keyboard interruption in main thread... closing server.")
162
  except Exception as e:
163
  logger.error(f"Error running voice assistant: {e}")
164
  raise
165
  finally:
166
  logger.info("Shutting down voice assistant...")
167
+ try:
168
+ loop.run_until_complete(service.stop())
169
+ except Exception as e:
170
+ logger.error(f"Error stopping service: {e}")
171
+
172
+ # Clean up robot connection if available
173
+ if reachy_mini is not None:
174
+ try:
175
+ # Ensure media is explicitly closed before disconnecting
176
+ if hasattr(reachy_mini, 'media'):
177
+ reachy_mini.media.close()
178
+ logger.debug("Robot media closed")
179
+ except Exception as e:
180
+ logger.debug(f"Error closing media during shutdown: {e}")
181
+
182
+ try:
183
+ # Prevent connection from keeping threads alive
184
+ reachy_mini.client.disconnect()
185
+ logger.debug("Robot client disconnected")
186
+ except Exception as e:
187
+ logger.debug(f"Error disconnecting client during shutdown: {e}")
188
+
189
+ # Close event loop
190
+ try:
191
+ loop.close()
192
+ except Exception as e:
193
+ logger.debug(f"Error closing event loop: {e}")
194
+
195
  logger.info("Voice assistant stopped.")
196
 
197