Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(redis): declare the content size in zstd cache frames (#103969)

James Gill3ef706070281
diff --git a/rust/common/redis/src/client.rs b/rust/common/redis/src/client.rs
index f9aadc19396d..acbb2510ce90 100644
--- a/rust/common/redis/src/client.rs
+++ b/rust/common/redis/src/client.rs
@@ -1,3 +1,4 @@
+use std::io::Write;
use std::sync::Arc;
use std::time::{Duration, Instant};
@@ -258,11 +259,18 @@ impl RedisClient {
data: Vec<u8>,
config: &CompressionConfig,
) -> Result<Vec<u8>, CustomRedisError> {
- if config.enabled && data.len() > config.threshold {
- zstd::encode_all(&data[..], config.level).map_err(|e| e.into())
- } else {
- Ok(data)
- }
+ if !(config.enabled && data.len() > config.threshold) {
+ return Ok(data);
+ }
+
+ // The frame has to declare the decompressed size. Django reads these values through
+ // python-zstd, which sizes its output buffer from that header and fails on anything
+ // past one 128 KiB block without it. `zstd::encode_all` never pledges the size, so it
+ // writes entries Django can only read while they stay small.
+ let mut encoder = zstd::Encoder::new(Vec::new(), config.level)?;
+ encoder.set_pledged_src_size(Some(data.len() as u64))?;
+ encoder.write_all(&data)?;
+ Ok(encoder.finish()?)
}
/// Serialize a string value according to the format and apply compression if configured
@@ -1076,6 +1084,29 @@ mod tests {
assert_eq!(deserialized, test_value);
}
+ #[test]
+ fn test_compressed_frame_declares_content_size() {
+ // Django reads these values through python-zstd, which sizes its output buffer
+ // from the frame header and fails past one 128 KiB block when the size is absent.
+ // A frame without it is readable by this crate and unreadable by Django, so no
+ // round-trip through `try_decompress` can catch the regression.
+ let test_value = "x".repeat(200_000);
+ let config = CompressionConfig::default();
+
+ let serialized = helpers::serialize_value(&test_value, RedisValueFormat::Pickle);
+ assert!(
+ serialized.len() > 128 * 1024,
+ "payload must exceed one zstd block for this to be the production shape"
+ );
+
+ let processed = RedisClient::maybe_compress(serialized.clone(), &config).unwrap();
+
+ assert_eq!(
+ zstd::zstd_safe::get_frame_content_size(&processed).unwrap(),
+ Some(serialized.len() as u64)
+ );
+ }
+
#[test]
fn test_utf8_without_compression() {
let test_value = "test_string";