5748: Fix leaks in GetBlock error handling and PutBlock use of GetBlock where
authorPeter Amstutz <peter.amstutz@curoverse.com>
Thu, 14 May 2015 20:02:43 +0000 (16:02 -0400)
committerPeter Amstutz <peter.amstutz@curoverse.com>
Thu, 14 May 2015 20:02:43 +0000 (16:02 -0400)
buffers were not being returned to the buffer pool.

services/keepstore/bufferpool.go
services/keepstore/handlers.go

index 373bfc75a1ca251b0bf851bfe45dfe78fc635ae7..0b216d76bff893fa30379a4003963431a9d309e9 100644 (file)
@@ -39,6 +39,8 @@ func (p *bufferPool) Get(size int) []byte {
 }
 
 func (p *bufferPool) Put(buf []byte) {
-       p.Pool.Put(buf)
-       <-p.limiter
+       if buf != nil {
+               p.Pool.Put(buf)
+               <-p.limiter
+       }
 }
index cf5dfcac57a2e93b1bde9fa8a2bfcc60d01a186c..7c9ae6427d6cd992131c468276ad9b0ea3dfc313 100644 (file)
@@ -514,6 +514,7 @@ func GetBlock(hash string, update_timestamp bool) ([]byte, error) {
                        if !os.IsNotExist(err) {
                                log.Printf("GetBlock: reading %s: %s\n", hash, err)
                        }
+                       bufs.Put(buf)
                        continue
                }
                // Check the file checksum.
@@ -525,6 +526,7 @@ func GetBlock(hash string, update_timestamp bool) ([]byte, error) {
                        log.Printf("%s: checksum mismatch for request %s (actual %s)\n",
                                vol, hash, filehash)
                        error_to_caller = DiskHashError
+                       bufs.Put(buf)
                        continue
                }
                if error_to_caller == DiskHashError {
@@ -536,6 +538,7 @@ func GetBlock(hash string, update_timestamp bool) ([]byte, error) {
                                error_to_caller = GenericError
                                log.Printf("%s: Touch %s failed: %s",
                                        vol, hash, error_to_caller)
+                               bufs.Put(buf)
                                continue
                        }
                }
@@ -586,6 +589,7 @@ func PutBlock(block []byte, hash string) error {
        // so there is nothing special to do if err != nil.
        //
        if oldblock, err := GetBlock(hash, true); err == nil {
+               defer bufs.Put(oldblock)
                if bytes.Compare(block, oldblock) == 0 {
                        // The block already exists; return success.
                        return nil