From 0b8fa2c968abea0cf51f756ade45f63192bf97b8 Mon Sep 17 00:00:00 2001 From: Aurimas Liutikas Date: Wed, 11 Oct 2023 17:46:30 -0700 Subject: [PATCH] Add protection for 0 byte cache entries In androidx we have observed that if Gradle OOMs it sometimes tries to write 0 byte cache entries which corrupt the remote cache for future builds. This change makes sure we do not write store/load such entries Test: None --- .../build/gradle/gcpbuildcache/GcpBuildCacheService.kt | 1 + .../build/gradle/gcpbuildcache/GcpStorageService.kt | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt index 879f0e5..61d058c 100644 --- a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt +++ b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt @@ -64,6 +64,7 @@ internal class GcpBuildCacheService( } override fun store(key: BuildCacheKey, writer: BuildCacheEntryWriter) { + if (writer.size == 0L) return // do not store empty entries into the cache logger.info("Storing ${key.blobKey()}") val cacheKey = key.blobKey() val output = ByteArrayOutputStream() diff --git a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt index cb701d3..d4c2b02 100644 --- a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt +++ b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt @@ -57,7 +57,6 @@ internal class GcpStorageService( } override fun store(cacheKey: String, contents: ByteArray): Boolean { - if (!isEnabled) { logger.info("Not Enabled") return false @@ -123,7 +122,10 @@ internal class GcpStorageService( if (storage == null) return null return try { val blob = storage.service.get(blobId) ?: return null - return if (blob.size > sizeThreshold) { + return if (blob.size == 0L) { + // return empty entries as a cache miss + null + } else if (blob.size > sizeThreshold) { val path = FileHandleInputStream.create() blob.downloadTo(path) path.handleInputStream()