Skip to content

fix: reduce detach latency and stabilize detach path#324

Open
tobwen wants to merge 2 commits intoshell-pool:masterfrom
tobwen:fix/detach-latency
Open

fix: reduce detach latency and stabilize detach path#324
tobwen wants to merge 2 commits intoshell-pool:masterfrom
tobwen:fix/detach-latency

Conversation

@tobwen
Copy link
Contributor

@tobwen tobwen commented Mar 2, 2026

Summary

Fixes #323, detach latency by making detach path responsive with faster polling and adaptive backoff in client protocol loop.

Changes

  • Reduced JOIN_POLL_DURATION from 100ms to 10ms in libshpool/src/consts.rs for faster thread join polling
  • Reduced SHELL_TO_CLIENT_POLL_MS from 100 to 10 in libshpool/src/daemon/shell.rs for faster detach/reattach detection
  • Added adaptive backoff with exponential step (1ms initial, capped at 25ms) in client protocol loop
  • Added fast path for TTY detach (10ms) vs. slow path fallback (300ms max)
  • Exit client->shell thread immediately after detach action in libshpool/src/daemon/shell.rs
  • Refactored polling loop in libshpool/src/protocol.rs to check stop flag during sleep intervals

Behavior

Before this fix

  • Detach was slow (100 to 300ms polling intervals)
  • Threads could stay blocked after detach waiting on poll cycles
  • Users experienced noticeable delay when detaching

After this fix

  • Responsive detach with 10ms fast-path for TTY sessions
  • Immediate exit after detach (no more waiting for poll cycles)
  • Adaptive backoff ensures compatibility with existing shutdown paths
  • CPU usage controlled through bounded backoff (max 25ms step)

Rationale

The previous polling intervals (100ms) were too conservative, causing noticeable latency in the detach operation. By reducing polling to 10ms and implementing adaptive backoff, detach becomes responsive while maintaining compatibility with existing shutdown flows and avoiding busy waits.

Tests

No tests have been added or modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HAS FIX] Improve detach latency and responsivenes

1 participant