shadowsocks/shadowsocks-rust · error
invalid `udp_weight`, must be in [0, 1]
Error message
invalid `udp_weight`, must be in [0, 1]
What it means
Thrown when a server entry specifies `udp_weight` outside [0.0, 1.0]. Like tcp_weight, this feeds ServerWeight for traffic balancing; the parser validates it immediately after tcp_weight and rejects values below 0 or above 1 with ErrorKind::Invalid. Defaults to 1.0 when omitted.
Source
Thrown at crates/shadowsocks-service/src/config.rs:2386
}
if let Some(remarks) = svr.remarks {
nsvr.set_remarks(remarks);
}
if let Some(id) = svr.id {
nsvr.set_id(id);
}
if svr.tcp_weight.is_some() || svr.udp_weight.is_some() {
let tcp_weight = svr.tcp_weight.unwrap_or(1.0);
if !(0.0..=1.0).contains(&tcp_weight) {
let err = Error::new(ErrorKind::Invalid, "invalid `tcp_weight`, must be in [0, 1]", None);
return Err(err);
}
let udp_weight = svr.udp_weight.unwrap_or(1.0);
if !(0.0..=1.0).contains(&udp_weight) {
let err = Error::new(ErrorKind::Invalid, "invalid `udp_weight`, must be in [0, 1]", None);
return Err(err);
}
let mut weight = ServerWeight::new();
weight.set_tcp_weight(tcp_weight);
weight.set_udp_weight(udp_weight);
nsvr.set_weight(weight);
}
let mut server_instance = ServerInstanceConfig::with_server_config(nsvr);
if let Some(acl_path) = svr.acl {
let acl = match AccessControl::load_from_file(&acl_path) {
Ok(acl) => acl,
Err(err) => {
let err = Error::new(
ErrorKind::Invalid,
"acl loading failed",
Some(format!("file {acl_path}, error: {err}")),View on GitHub (pinned to 8eb0f0a65b)
Solutions
- Set udp_weight within [0.0, 1.0], e.g. 0.5 for half weight
- Use 0.0 to disable UDP traffic for that server
- Remove the field to accept the default of 1.0
Example fix
// before
{"server": "1.2.3.4", "server_port": 8388, "method": "aes-256-gcm", "password": "pw", "udp_weight": 80}
// after
{"server": "1.2.3.4", "server_port": 8388, "method": "aes-256-gcm", "password": "pw", "udp_weight": 0.8} Defensive patterns
Strategy: validation
Validate before calling
if let Some(w) = svr.get("udp_weight").and_then(|w| w.as_f64()) {
assert!((0.0..=1.0).contains(&w), "udp_weight {w} out of [0,1]");
} Type guard
fn valid_udp_weight(w: f64) -> bool { (0.0..=1.0).contains(&w) } Try / catch
let udp_weight = svr.udp_weight.unwrap_or(1.0);
if !(0.0..=1.0).contains(&udp_weight) {
eprintln!("clamping udp_weight {udp_weight} to [0,1]");
} Prevention
- Validate tcp_weight and udp_weight together — either being present triggers both checks
- Use a shared helper to normalize weights before generating configs
- Default to omitting weights unless load-balancing is intentional
When it happens
Trigger: Setting `"udp_weight": 2.0`, `"udp_weight": -1`, etc. in a server entry while weight fields are present (the check runs whenever either tcp_weight or udp_weight is set).
Common situations: Percentage-scale confusion (using 0-100 numbers); typo'd decimals; assuming UDP weight is validated only when udp_weight itself is present.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- invalid `tcp_weight`, must be in [0, 1]
- `protocol` invalid
- invalid `mode`
- `server`, `server_port`, `method`, `password` must be provid
- `users[].password` should be base64 encoded
AI-assisted analysis of shadowsocks/shadowsocks-rust@8eb0f0a65b (2026-09-09).
Data as JSON: /api/errors/f4140a6bbedfe65a.
Report an issue: GitHub.