title:	Fix TUI prompt spurious-deny and SIGTTIN daemon hang
state:	OPEN
author:	cuihtlauac (Cuihtlauac Alvarado)
labels:	
assignees:	
reviewers:	
projects:	
milestone:	
number:	22
url:	https://github.com/tarides/sudo-proxy/pull/22
additions:	80
deletions:	2
auto-merge:	disabled
--
## Summary

Three lifecycle bugs surfaced by sustained Claude Code MCP usage:

- **Spurious `Denied` without user input** — `read_key_timeout` switched the TTY to non-canonical mode with `TCSANOW`, which leaves bytes already in the line-discipline queue intact. A leftover Enter, mistyped key, or paste residue from before the prompt rendered was delivered to the first `read` and resolved as `Denied` without the user pressing anything. Switched to `TCSAFLUSH` so the input queue is discarded atomically with the mode change.

- **TUI window appears but never renders the next prompt; Claude Code retries forever** — While a previously-approved privileged child holds the controlling terminal's foreground pgrp (handed off in `executor::run_single_command` so `sudo` can read its password), the daemon itself is in a *background* pgrp on its own tty. A second concurrent prompt's `read(/dev/tty)` delivers `SIGTTIN`, whose default action stops the daemon — and nothing in this codebase ever sends `SIGCONT`, so the daemon hangs until the user kills it. Now ignore `SIGTTIN`/`SIGTTOU` process-wide so background-tty races become `EIO` at the read site instead of stopping the daemon.

- **Live daemon becomes unreachable when a second instance starts** — `bin/sudo-proxy` removed the socket file unconditionally on any `server::run` error, including `AddrInUse`. That deletes the live daemon's socket file out from under it, leaving a running-but-unreachable process. Skip removal on `AddrInUse`.

## Test plan

- [x] `cargo test` — all 31 lib unit tests + every integration suite green
- [x] New regression test `read_key_timeout_discards_pre_prompt_input` pre-fills a pty's input queue and asserts `read_key_timeout` returns `None` (timeout), not the buffered byte
- [ ] Manual: run a Claude Code session with multiple privileged commands fired in quick succession; confirm prompts no longer auto-deny and the daemon no longer freezes

🤖 Generated with [Claude Code](https://claude.com/claude-code)
