keep-network / keep-core

The smart contracts and reference client behind the Keep network
https://keep.network
MIT License
119 stars 74 forks source link

Fix a memory leak in Electrum client #3775

Closed lukasz-zimnoch closed 8 months ago

lukasz-zimnoch commented 8 months ago

Closes: https://github.com/keep-network/keep-core/issues/3739 Depends on: https://github.com/keep-network/go-electrum/pull/5 Depends on: https://github.com/keep-network/keep-core/pull/3774

So far, the GetLatestBlockHeight function of the Electrum client was using go-electrum's SubscribeHeaders under the hood. That caused a memory leak because GetLatestBlockHeight was not interested in reading from the returned headersChan channel. Each call to GetLatestBlockHeight produced a new dangling goroutine blocked on a buffered channel with one item inside.

Here we replace SubscribeHeaders with SubscribeHeadersSingle which does not create a goroutine supposed to handle future headers notifications. The SubscribeHeadersSingle just returns the current chain tip and ignores further notifications coming from the Electrum server.

See https://github.com/keep-network/go-electrum/pull/5 for further reference.