{"record":{"id":"f6886475e69a27cc","repo":"oraios/serena","slug":"invalid-content-length-header-value-r","errorCode":null,"errorMessage":"Invalid Content-Length header: {value!r}","messagePattern":"Invalid Content-Length header: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/solidlsp/lsp_protocol_handler/server.py","lineNumber":160,"sourceCode":"        body,\n    )\n\n\nclass MessageType:\n    error = 1\n    warning = 2\n    info = 3\n    log = 4\n\n\ndef content_length(line: bytes) -> int | None:\n    if line.startswith(b\"Content-Length: \"):\n        _, value = line.split(b\"Content-Length: \")\n        value = value.strip()\n        try:\n            return int(value)\n        except ValueError:\n            raise ValueError(f\"Invalid Content-Length header: {value!r}\")\n    return None\n","sourceCodeStart":142,"sourceCodeEnd":162,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/solidlsp/lsp_protocol_handler/server.py#L142-L162","documentation":"This ValueError is raised by the LSP protocol handler when parsing the stdout stream of a language server process. The handler reads HTTP-style headers and expects a 'Content-Length: <int>' header; if the numeric part cannot be converted to an integer, the stream is considered corrupt and this error is thrown.","triggerScenarios":"The language server process writes a line starting with 'Content-Length: ' whose value is not a valid integer (e.g. 'Content-Length: abc', 'Content-Length: ??', or an empty/whitespace-only value). This happens in content_length(), called from _read_ls_process_stdout/_read_loop while reading the server's stdout.","commonSituations":"A misbehaving or non-LSP-compliant language server binary is on PATH (wrong version, wrong executable selected); the server emits diagnostics/log lines that accidentally match the header format; stdout is contaminated by shell wrappers, scripts, or proxy processes; or the binary prints localized/garbage output on startup due to environment/locale issues.","solutions":["Inspect the language server's stdout directly (run the server binary manually) to see what malformed Content-Length line it emits","Verify the correct, LSP-compliant server binary is installed and on PATH (check version; reinstall if corrupt or mismatched)","Check for wrapper scripts or environment hooks (shell profiles, proxies, logging wrappers) that pollute the server's stdout","If the server is known to emit stray output, patch or upgrade solidlsp's handler to skip non-conforming header lines instead of raising","Ensure the server process is launched with stdout used exclusively for LSP protocol (disable any verbose/log-to-stdout options)"],"exampleFix":"// before: handler raises on any non-integer Content-Length value\nvalue = value.strip()\ntry:\n    return int(value)\nexcept ValueError:\n    raise ValueError(f\"Invalid Content-Length header: {value!r}\")\n\n// after (library-side hardening): ignore malformed header lines and keep reading\nvalue = value.strip()\ntry:\n    return int(value)\nexcept ValueError:\n    log.warning(f\"Ignoring invalid Content-Length header: {value!r}\")\n    return None","handlingStrategy":"validation","validationCode":"import re\n\ndef has_valid_content_length(line: bytes) -> bool:\n    if line.startswith(b\"Content-Length: \"):\n        value = line.split(b\"Content-Length: \", 1)[1].strip()\n        return value.isdigit() and int(value) >= 0\n    return True\n\n# Before feeding header lines to the parser / launching the server:\n# assert all(has_valid_content_length(l) for l in header_lines)","typeGuard":"def parse_content_length(line: bytes) -> int | None:\n    if not line.startswith(b\"Content-Length: \"):\n        return None\n    value = line.split(b\"Content-Length: \", 1)[1].strip()\n    if not value.isdigit():\n        return None\n    return int(value)","tryCatchPattern":"try:\n    length = content_length(line)\nexcept ValueError as e:\n    logging.warning(\"Malformed LSP header from server: %s\", e)\n    length = None  # skip line and continue reading the stream","preventionTips":["Validate that the chosen language server binary speaks the LSP wire protocol before wiring it up (run it and inspect stdout)","Keep the server's stdout reserved for LSP protocol; disable verbose/log-to-stdout server options","Pin and test known-good server versions; avoid wrapper scripts that print to stdout","Monitor server startup output in logs to catch non-protocol contamination early"],"tags":["lsp","protocol-parsing","valueerror","language-server"],"backgroundTag":"invalid-content-length-header","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}