Skip to content
Edit this page

Developer Guide

This section documents the engineering conventions used across the ClashRS workspace (clash-bin, clash-lib, clash-dns, clash-netstack, and clash-ffi). It focuses on the concurrency, lifecycle, and error-handling practices that keep a long-running, multi-protocol async proxy core predictable under high load, hot configuration reloads, and cross-platform shutdowns.

Target Toolchain & Runtime

ClashRS uses Rust edition 2024 with Cargo resolver 3 and Tokio as its asynchronous runtime. Most core concurrency bugs in a proxy daemon stem from subtle interactions between tokio::select!, detached tasks, partial shutdowns during hot reload, and lossy error boundaries.

Core Topics

Topic Primary Primitive / Reference What It Covers
Cancellation Safety tokio::select! Avoiding dropped bytes, lost messages, and deadlocks when futures are cancelled mid-poll
Graceful Shutdown CancellationToken Coordinating process exit, FFI shutdown, and hot reloads without leaking sockets or routes
Structured Concurrency JoinSet Bounding task lifetimes, enforcing backpressure, and preventing orphaned background tasks
Error Handling thiserror, anyhow, std::io::Error Choosing the right error type per layer, preserving error chains, and handling boundaries cleanly

How These Four Practices Fit Together

In ClashRS, a single inbound connection or subsystem lifecycle touches all four areas at once:

  1. Structured concurrency gives every spawned task a clear owner (a JoinSet, an AbortOnDropHandle, a Runner, or an enclosing select!), so background work cannot outlive its subsystem.
  2. Graceful shutdown propagates a CancellationToken hierarchy from start_with_overrides down to ApiRunner, DnsRunner, InboundManager, TunRunner, and provider fetchers so listeners release ports, pipes, and OS routes cleanly before a hot reload or exit completes.
  3. Cancellation safety ensures that when a tokio::select! branch wins—whether it is a cancellation signal, a timer, or a peer message—the losing branches do not silently lose buffered packets, corrupt framing state, or leave locks/permits wedged.
  4. Error handling keeps failures typed at library and data-plane boundaries, attaches context without duplicating logs, and turns transient peer or upstream errors into isolated session teardown instead of crashing the core.

Workspace Map

Crate Role Key Lifecycle / Error Surface
clash-bin CLI executable and service-mode entry point Parses CLI flags, installs SafePaths, returns anyhow::Result<()>
clash-lib Core proxy library (app/, proxy/, config/) Owns Error, Runner, RuntimeComponents, Dispatcher, inbounds, and outbounds
clash-dns Local DNS server listeners (UDP/TCP/DoT/DoH/DoH3) Exposes watfaq_dns::DNSError and listener futures driven by DnsRunner
clash-netstack Userspace TCP/IP stack (smoltcp) for TUN Provides packet streams/sinks consumed by TunRunner
clash-ffi C ABI bindings for mobile clients (Android / iOS) Bridges start_scaffold / shutdown across FFI without unwinding panics

Local Verification Before Opening a PR

Run formatting, lints, and tests from the clash-rs repository root before submitting changes:

cargo +nightly fmt --all --check
cargo clippy --all --all-features
cargo test --all --all-features

Git Workflow

Never commit or push directly to master. Create a feature branch and open a pull request with focused tests covering both the happy path and cancellation/error paths.