{"record":{"id":"17238cc8fd1b7db5","repo":"jstedfast/MailKit","slug":"error-inflating","errorCode":null,"errorMessage":"Error inflating: ","messagePattern":"Error inflating: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"MailKit/CompressedStream.cs","lineNumber":213,"sourceCode":"\n\t\t\tdo {\n\t\t\t\tif (zIn.avail_in == 0 && !eos) {\n\t\t\t\t\tzIn.avail_in = InnerStream.Read (zIn.next_in, 0, zIn.next_in.Length);\n\n\t\t\t\t\teos = zIn.avail_in == 0;\n\t\t\t\t\tzIn.next_in_index = 0;\n\t\t\t\t}\n\n\t\t\t\tint retval = zIn.inflate (JZlib.Z_FULL_FLUSH);\n\n\t\t\t\tif (retval == JZlib.Z_STREAM_END)\n\t\t\t\t\tbreak;\n\n\t\t\t\tif (eos && retval == JZlib.Z_BUF_ERROR)\n\t\t\t\t\treturn 0;\n\n\t\t\t\tif (retval != JZlib.Z_OK)\n\t\t\t\t\tthrow new IOException (\"Error inflating: \" + zIn.msg);\n\t\t\t} while (zIn.avail_out == count);\n\n\t\t\treturn count - zIn.avail_out;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Reads a sequence of bytes from the stream and advances the position\n\t\t/// within the stream by the number of bytes read.\n\t\t/// </summary>\n\t\t/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many\n\t\t/// bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>\n\t\t/// <param name=\"buffer\">The buffer.</param>\n\t\t/// <param name=\"offset\">The buffer offset.</param>\n\t\t/// <param name=\"count\">The number of bytes to read.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"buffer\"/> is <see langword=\"null\" />.\n\t\t/// </exception>","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/CompressedStream.cs#L195-L231","documentation":"Read() throws IOException(\"Error inflating: \" + zIn.msg) when JZlib's inflate() returns a status other than Z_OK (and the special end-of-stream Z_BUF_ERROR case is excluded). zIn.msg carries zlib's description of the corrupted or unexpected compressed data.","triggerScenarios":"Calling CompressedStream.Read when the underlying stream yields bytes that fail zlib inflation: corrupt/truncated compressed data, wrong compression window size, reading a stream not actually zlib-compressed, or protocol desync (e.g. an IMAP server response that is not compressed payload).","commonSituations":"IMAP COMPRESS sessions where the server sent a literal or untagged response that was not compressed, network truncation mid-stream, feeding a raw (uncompressed) stream into CompressedStream, zlib version/format mismatch.","solutions":["Inspect zIn.msg / the IOException message to identify the zlib failure (data error, need dictionary, etc.).","Verify the inner stream actually contains zlib data and was not corrupted or truncated; reconnect and reissue the COMPRESS command.","Ensure you did not interleave raw writes/reads on the same socket outside CompressedStream, which desynchronizes the deflate/inflate state.","Regenerate or re-fetch the compressed data; if the source data is bad, the stream cannot recover."],"exampleFix":"// before\nint n = compressedStream.Read(buffer, 0, buffer.Length); // IOException: Error inflating\n// after\ntry {\n    int n = compressedStream.Read(buffer, 0, buffer.Length);\n} catch (IOException ex) when (ex.Message.StartsWith(\"Error inflating\")) {\n    // inner stream data is corrupt/not zlib - reconnect and restart COMPRESS\n    ReconnectAndRecompress();\n}","handlingStrategy":"try-catch","validationCode":"// before reading, sanity-check the source is zlib data (RFC1950 header 0x78)\nint first = innerStream.ReadByte();\nif (first != 0x78) throw new InvalidDataException(\"Inner stream is not zlib-compressed.\");\n// (also keep a copy so the byte can be pushed back / stream recreated)","typeGuard":"static bool IsZlibHeader(Stream s) {\n    int b = s.ReadByte();\n    if (b < 0) return false;\n    s.Position = s.CanSeek ? 0 : 0; // rewind only if seekable\n    return (b & 0x0F) == 8; // CMF: deflate method\n}","tryCatchPattern":"try {\n    int n = compressedStream.Read(buffer, 0, buffer.Length);\n} catch (IOException ex) when (ex.Message.StartsWith(\"Error inflating\")) {\n    // corrupt or non-zlib data: cannot recover inflate state; reconnect/restart session\n    logger.LogError(ex, \"Inflate failure: {Msg}\", ex.Message);\n    await ReconnectAsync();\n}","preventionTips":["Verify COMPRESS (or zlib framing) is active before sending/receiving payload","Never write raw/uncompressed bytes into a compressed session","Handle network truncation promptly - treat partial frames as fatal to the stream","Route ALL traffic through CompressedStream to avoid protocol desync","Keep a reference to zlib msg details from the exception message when logging"],"tags":["io","zlib","compression","corrupt-data","imap"],"backgroundTag":"zlib-inflate-failed","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}