stt-cli 0.2.1

Speech to text Cli using Groq API and OpenAI API
ALSA lib pcm_dmix.c:973:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dsnoop.c:541:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream
ALSA lib pcm_dsnoop.c:567:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dsnoop.c:541:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream
ALSA lib pcm_dsnoop.c:567:(snd_pcm_dsnoop_open) unable to open slave
2025-04-24T13:41:42.339074Z  INFO stt_cli::audio::device: Please select an audio input device:
2025-04-24T13:41:43.944348Z  INFO stt_cli::audio::device: Selected device: default
2025-04-24T13:41:43.959210Z  INFO stt_cli::audio::stream_manager: Audio stream started successfully
2025-04-24T13:41:43.960157Z  INFO stt_cli::app: Waiting for shutdown signal...
2025-04-24T13:41:43.960187Z  INFO stt_cli::app: Waiting for shutdown signal...
2025-04-24T13:41:43.960322Z  INFO stt_cli::app: Starting audio processing task
2025-04-24T13:41:43.960338Z  INFO stt_cli::hotkey_service: Hotkey service started
2025-04-24T13:41:45.363109Z  INFO stt_cli::hotkey_service: Recording started
2025-04-24T13:41:50.668715Z  INFO transcribe: stt_cli::providers::groq: Starting Groq transcription request
2025-04-24T13:41:51.266698Z  INFO transcribe: stt_cli::providers::groq: Groq transcription successful
2025-04-24T13:41:51.266790Z  INFO transcribe: stt_cli::providers::groq: Received transcription:  can you listen to it, and can you send it over?
2025-04-24T13:41:53.127149Z  INFO stt_cli::hotkey_service: Recording paused
^C2025-04-24T13:41:56.095014Z  INFO stt_cli::shutdown_handler: Received SIGINT (Ctrl+C)
2025-04-24T13:41:56.095111Z  INFO stt_cli::app: Received shutdown signal
2025-04-24T13:41:56.095713Z  INFO stt_cli::audio::stream_manager: Audio stream stopped successfully
2025-04-24T13:41:56.095908Z  INFO stt_cli::app: Audio processing task stopped



but as you can see -- after multiple Ctrl+C -- it does not exit the program.

logs:

```md

2025-04-24T13:41:56.095713Z  INFO stt_cli::audio::stream_manager: Audio stream stopped successfully
2025-04-24T13:41:56.095908Z  INFO stt_cli::app: Audio processing task stopped
2025-04-24T13:42:01.096868Z  WARN stt_cli::app: Task did not complete within timeout: deadline has elapsed
2025-04-24T13:42:01.096967Z  INFO stt_cli::app: Shutdown complete
^C^C^C^C^C^C

```


so, the shutdown handler is not working properly.


ANALYSIS And POTENTIAL DEBUGGING logs:

## Analysis of the Shutdown Issue

Based on the logs and code review, the application is experiencing a "zombie process" issue where the program doesn't fully exit after receiving a shutdown signal. Here's what's happening:

1. **Signal Handling**: The shutdown signal (Ctrl+C/SIGINT) is correctly received and logged by the shutdown handler.
2. **Shutdown Sequence Initiation**: The shutdown sequence is properly initiated, and the app logs "Received shutdown signal".
3. **Component Shutdown**: Audio stream and processing tasks are stopped successfully and logged.
4. **Timeout Warning**: A warning is logged: "Task did not complete within timeout: deadline has elapsed".
5. **Completion Indication**: "Shutdown complete" is logged, but the process doesn't exit.

## Root Cause Analysis

The most likely causes for this behavior are:

1. **Lingering Tasks**: Some Tokio tasks are not being properly awaited or cancelled.
2. **Blocking Receivers**: Channel receivers (particularly from `GlobalHotKeyEvent::receiver()`) might be blocking the shutdown.
3. **Incomplete Resource Cleanup**: Some resources (particularly in the hotkey service) are not being properly cleaned up.
4. **Tokio Runtime Not Shutting Down**: The main Tokio runtime is not shutting down because some tasks are still running.
5. **Missing Task Cancellation**: Tasks spawned with `tokio::spawn` are not being properly cancelled or awaited.

## Key Issues Identified

1. **Hotkey Service Cleanup**: The hotkey unregistration code in `HotkeyService::register_shutdown_handler` is commented out:
   ```rust
   // if let Err(e) = service.manager.unregister_all(&service.registered_hotkeys) {
   //     debug!("Failed to unregister hotkeys during shutdown: {}", e);
   // }
   ```

2. **Blocking Task Handling**: The `spawn_blocking` task that forwards hotkey events is not being properly terminated.

3. **Shutdown Signal Propagation**: The shutdown signal might not be reaching all components.

4. **Task Awaiting**: In `App::wait_for_shutdown`, tasks are awaited with a timeout, but there's no mechanism to force-terminate tasks that don't complete.

## Debugging Plan

1. **Add Detailed Logging**:
   - Add more detailed logging in shutdown paths
   - Log when each component starts and completes shutdown
   - Add logging to track task creation and completion

2. **Fix Hotkey Service Cleanup**:
   - Uncomment and fix the hotkey unregistration code
   - Ensure the blocking task that forwards hotkey events is properly terminated

3. **Improve Shutdown Signal Propagation**:
   - Ensure all components receive and properly handle the shutdown signal
   - Add explicit cancellation for all spawned tasks

4. **Implement Forceful Termination**:
   - Add a mechanism to forcefully terminate tasks that don't complete within the timeout
   - Consider using `tokio::task::AbortHandle` for critical tasks

5. **Check for Resource Leaks**:
   - Verify that all channels, mutexes, and other resources are properly dropped
   - Add explicit drop implementations for critical components

6. **Investigate Tokio Runtime Behavior**:
   - Add a debug build with Tokio tracing enabled to see what tasks are still running
   - Check if the main Tokio runtime is being properly shut down

7. **Test Incremental Fixes**:
   - Implement fixes one at a time and test after each change
   - Focus first on the hotkey service, as it's the most likely culprit

## Specific Code Areas to Focus On

1. **HotkeyService::run** - The blocking task that forwards hotkey events needs proper cancellation
2. **HotkeyService::register_shutdown_handler** - Uncomment and fix the hotkey unregistration code
3. **App::wait_for_shutdown** - Improve task awaiting and add forceful termination
4. **main.rs** - Ensure the main function properly awaits all tasks and exits cleanly

## Next Steps

1. Implement detailed logging to identify which components are not shutting down
2. Fix the hotkey service cleanup
3. Improve shutdown signal propagation
4. Test with incremental fixes

---

## Debugging Checklist for Shutdown Issue

- [ ] **Add Logging to All Task Start/Stop**
    - Log when every background task (Tokio or blocking) is started and when it exits.
    - Log in the hotkey service's blocking thread both at start and before exit.

- [ ] **Instrument HotkeyService Blocking Thread**
    - Add a shutdown signal check in the blocking loop.
    - Log when the blocking loop breaks due to shutdown.
    - Ensure the thread can exit when shutdown is requested.

- [ ] **Track All JoinHandles**
    - Ensure every spawned task (Tokio or blocking) returns a JoinHandle and is stored in a list.
    - Log and abort any handle that does not complete within the timeout during shutdown.

- [ ] **Check for Resource Leaks**
    - Confirm all channels (mpsc, broadcast, etc.) are closed or dropped at shutdown.
    - Confirm all Arc/Mutex/shared resources are dropped.

- [ ] **Use Tokio Console or Tracing**
    - Run the app with `tokio-console` or enable Tokio tracing to see what tasks are alive after shutdown.

- [ ] **Uncomment and Fix Hotkey Unregistration**
    - Ensure hotkeys are unregistered in the shutdown handler.
    - Log the result of unregistration.

- [ ] **Check Main Function Completion**
    - Log at the very end of `main` to confirm it returns.
    - If using `#[tokio::main]`, ensure no tasks are left running when main returns.

- [ ] **Test Incrementally**
    - After each change, test shutdown and confirm if the process exits cleanly.
    - If not, check which checklist item is not satisfied and investigate further.

---

This checklist will help you systematically identify and fix the root cause of the lingering process during shutdown. Mark off each step as you verify or complete it.

------

Attempt 2 at shutdown debug handle.r 


ALSA lib pcm_dmix.c:973:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:973:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dsnoop.c:541:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream
ALSA lib pcm_dsnoop.c:567:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dsnoop.c:541:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream
ALSA lib pcm_dsnoop.c:567:(snd_pcm_dsnoop_open) unable to open slave
2025-04-24T13:50:00.471249Z  INFO stt_cli::audio::device: Please select an audio input device:
2025-04-24T13:50:01.276662Z  INFO stt_cli::audio::device: Selected device: default
2025-04-24T13:50:01.288289Z  INFO stt_cli::audio::stream_manager: Audio stream started successfully
2025-04-24T13:50:01.289141Z  INFO stt_cli::app: Waiting for shutdown signal...
2025-04-24T13:50:01.289179Z  INFO stt_cli::app: Waiting for shutdown signal...
2025-04-24T13:50:01.289169Z  INFO stt_cli::hotkey_service: Hotkey service started
2025-04-24T13:50:01.289190Z  INFO stt_cli::app: Starting audio processing task
2025-04-24T13:50:02.591265Z  INFO stt_cli::hotkey_service: Recording started
2025-04-24T13:50:07.892639Z  INFO transcribe: stt_cli::providers::groq: Starting Groq transcription request
2025-04-24T13:50:08.385591Z  INFO transcribe: stt_cli::providers::groq: Groq transcription successful
2025-04-24T13:50:08.385702Z  INFO transcribe: stt_cli::providers::groq: Received transcription:  Alright, so does it work or does it not work at all? Let me know.
2025-04-24T13:50:10.204749Z  INFO stt_cli::hotkey_service: Recording paused
^C2025-04-24T13:50:17.150287Z  INFO stt_cli::app: Received Ctrl+C signal
2025-04-24T13:50:17.150450Z  INFO stt_cli::shutdown_handler: [tts-groq] Starting graceful shutdown sequence
2025-04-24T13:50:17.150500Z  INFO stt_cli::shutdown_handler: [tts-groq] Executing shutdown handler: Stop audio stream manager (priority: Normal)
2025-04-24T13:50:17.151241Z  INFO stt_cli::hotkey_service: Hotkey service received shutdown signal
2025-04-24T13:50:17.151288Z  INFO stt_cli::hotkey_service: Hotkey service stopped
2025-04-24T13:50:17.151824Z  INFO stt_cli::audio::stream_manager: Audio stream stopped successfully
2025-04-24T13:50:17.152119Z  INFO stt_cli::shutdown_handler: [tts-groq] Shutdown handler completed: Stop audio stream manager
2025-04-24T13:50:17.152140Z  INFO stt_cli::shutdown_handler: [tts-groq] Executing shutdown handler: Stop hotkey service (priority: Normal)
2025-04-24T13:50:17.152156Z  INFO stt_cli::hotkey_service: Hotkey service shutdown handler completed
2025-04-24T13:50:17.152165Z  INFO stt_cli::shutdown_handler: [tts-groq] Shutdown handler completed: Stop hotkey service
2025-04-24T13:50:17.152174Z  INFO stt_cli::shutdown_handler: [tts-groq] Graceful shutdown sequence completed: ShutdownResult { completed: 2, timed_out: 0, forced: 0, failed: 0 }
2025-04-24T13:50:17.152204Z  INFO stt_cli::app: Audio processing task stopped
2025-04-24T13:50:17.152510Z  INFO stt_cli::app: Shutdown complete
^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C