From 70ebf59642dbc100f5d4ed82ba4cce46bb4eaacc Mon Sep 17 00:00:00 2001
From: ledgerwatch <akhounov@gmail.com>
Date: Tue, 6 Oct 2020 22:07:57 +0100
Subject: [PATCH] Context cleanup and make context independent (#1193)

---
 ethdb/kv_lmdb.go   | 18 +++++++-----------
 ethdb/kv_remote.go |  6 +++---
 2 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/ethdb/kv_lmdb.go b/ethdb/kv_lmdb.go
index 9188909b2c..3bd2b0bb9d 100644
--- a/ethdb/kv_lmdb.go
+++ b/ethdb/kv_lmdb.go
@@ -203,12 +203,11 @@ func (opts LmdbOpts) MustOpen() KV {
 }
 
 type LmdbKV struct {
-	opts                LmdbOpts
-	env                 *lmdb.Env
-	log                 log.Logger
-	buckets             dbutils.BucketsCfg
-	stopStaleReadsCheck context.CancelFunc
-	wg                  *sync.WaitGroup
+	opts    LmdbOpts
+	env     *lmdb.Env
+	log     log.Logger
+	buckets dbutils.BucketsCfg
+	wg      *sync.WaitGroup
 }
 
 func NewLMDB() LmdbOpts {
@@ -248,7 +247,7 @@ func (db *LmdbKV) DiskSize(_ context.Context) (uint64, error) {
 	return uint64(stats.PSize) * (stats.LeafPages + stats.BranchPages + stats.OverflowPages), nil
 }
 
-func (db *LmdbKV) Begin(ctx context.Context, parent Tx, writable bool) (Tx, error) {
+func (db *LmdbKV) Begin(_ context.Context, parent Tx, writable bool) (Tx, error) {
 	if db.env == nil {
 		return nil, fmt.Errorf("db closed")
 	}
@@ -276,7 +275,6 @@ func (db *LmdbKV) Begin(ctx context.Context, parent Tx, writable bool) (Tx, erro
 	tx.RawRead = true
 	return &lmdbTx{
 		db:      db,
-		ctx:     ctx,
 		tx:      tx,
 		isSubTx: isSubTx,
 	}, nil
@@ -285,13 +283,11 @@ func (db *LmdbKV) Begin(ctx context.Context, parent Tx, writable bool) (Tx, erro
 type lmdbTx struct {
 	isSubTx bool
 	tx      *lmdb.Txn
-	ctx     context.Context
 	db      *LmdbKV
 	cursors []*lmdb.Cursor
 }
 
 type LmdbCursor struct {
-	ctx        context.Context
 	tx         *lmdbTx
 	bucketName string
 	dbi        lmdb.DBI
@@ -626,7 +622,7 @@ func (tx *lmdbTx) Cursor(bucket string) Cursor {
 
 func (tx *lmdbTx) stdCursor(bucket string) Cursor {
 	b := tx.db.buckets[bucket]
-	return &LmdbCursor{bucketName: bucket, ctx: tx.ctx, tx: tx, bucketCfg: b, dbi: tx.db.buckets[bucket].DBI}
+	return &LmdbCursor{bucketName: bucket, tx: tx, bucketCfg: b, dbi: tx.db.buckets[bucket].DBI}
 }
 
 func (tx *lmdbTx) CursorDupSort(bucket string) CursorDupSort {
diff --git a/ethdb/kv_remote.go b/ethdb/kv_remote.go
index a0ff56de12..02cff8ecc1 100644
--- a/ethdb/kv_remote.go
+++ b/ethdb/kv_remote.go
@@ -336,7 +336,7 @@ func (c *remoteCursor) Seek(seek []byte) ([]byte, []byte, error) {
 	var err error
 	if c.stream == nil {
 		var streamCtx context.Context
-		streamCtx, c.streamCancelFn = context.WithCancel(c.ctx) // We create child context for the stream so we can cancel it to prevent leak
+		streamCtx, c.streamCancelFn = context.WithCancel(context.Background()) // We create child context for the stream so we can cancel it to prevent leak
 		c.stream, err = c.tx.db.remoteKV.Seek(streamCtx)
 	}
 
@@ -384,7 +384,7 @@ func (c *remoteCursor) Last() ([]byte, []byte, error) {
 
 func (c *remoteCursor) Close() {
 	if c.stream != nil {
-		//c.streamCancelFn() // Commented out because this causes "context cancelled issue" on RPC daemon
+		c.streamCancelFn()
 		c.stream = nil
 		c.streamingRequested = false
 	}
@@ -430,7 +430,7 @@ func (c *remoteCursorDupSort) SeekBothRange(key, value []byte) ([]byte, []byte,
 	var err error
 	if c.stream == nil {
 		var streamCtx context.Context
-		streamCtx, c.streamCancelFn = context.WithCancel(c.ctx) // We create child context for the stream so we can cancel it to prevent leak
+		streamCtx, c.streamCancelFn = context.WithCancel(context.Background()) // We create child context for the stream so we can cancel it to prevent leak
 		c.stream, err = c.tx.db.remoteKV.Seek(streamCtx)
 		if err != nil {
 			return []byte{}, nil, err
-- 
GitLab