Skip to content

Refactor ws_service_exec.rs:handle_ws to reduce cognitive_complexity and add test coverage #216

@mukeshblackhat

Description

@mukeshblackhat

ws_service_exec.rs:handle_ws (line 239) has #[allow(clippy::cognitive_complexity)]. The function is ~140 lines handling a PTY-backed exec session for running commands inside a service container.

What it does

handle_ws(WebSocket, AppState, project, name, service, container_id, session_id) — WebSocket handler for interactive shell/exec sessions into a service container. If a session_id is provided and active, reconnects to that session. Otherwise creates a new session: resolves the inner container, opens a PTY via docker exec, stores a PtySession in state, spawns a reader task that pushes PTY output into a scrollback ring buffer + broadcast, sends an init message with the new session_id, and runs the bridge loop.

What to do

1. Extract session creation

Lines 260–332 set up a brand-new session: generate UUID, resolve inner container, open PTY via spawn_blocking, dup the fd, build the PtySession, insert into state. Extract:

async fn create_pty_session(
    state: &AppState,
    docker: &bollard::Docker,
    container_id: &str,
    project: &str,
    service: &str,
    composite_key: String,
    socket: &mut WebSocket,
) -> Option<(String, Arc<Mutex<VecDeque<u8>>>, broadcast::Sender<Vec<u8>>, i32, i32)>
// returns (session_id, scrollback, output_tx, read_fd, write_fd) on success

2. Extract PTY reader task spawn

Lines 334–366 spawn the background task that reads from the PTY master fd into the scrollback ring + broadcast. Extract:

fn spawn_pty_reader_task(
    state: Arc<AppState>,
    sid: String,
    read_fd: i32,
    scrollback: Arc<Mutex<VecDeque<u8>>>,
    output_tx: broadcast::Sender<Vec<u8>>,
)

3. Extract error-and-return helper

The pattern let _ = socket.send(Message::Text(...)).await; return; appears 5+ times for different error paths. Extract:

async fn send_error_and_return(
    socket: &mut WebSocket,
    message: impl Into<String>,
)

4. Add unit tests

Unit testing this handler directly is hard (requires Docker + PTY). Focus on:

  • send_error_and_return — called via a mock socket to verify message format (if feasible)
  • Any pure helper functions that come out of the extraction

Important

The workspace Cargo.toml sets cognitive_complexity, too_many_lines, too_many_arguments, and several other clippy lints to warn globally. Run make lint after every change and make sure the output is clean. Don't fix a suppression here only to create new warnings elsewhere.

Acceptance criteria

  • create_pty_session extracted
  • spawn_pty_reader_task extracted
  • send_error_and_return extracted to deduplicate error paths
  • handle_ws uses the new helpers
  • All tests pass (make test)
  • make lint passes with zero new warnings
  • Remove #[allow(clippy::cognitive_complexity)] if make lint passes without it, keep if not (note why)

Related

Tracking issue pattern from previous PRs: #122, #133, #157, #158, #171, #172, #173, #174, #175

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions