Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/horizon: Fix memory leak in web.streamHandler #2575

Merged
merged 5 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/horizon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ file. This project adheres to [Semantic Versioning](http://semver.org/).

* Add `last_modified_time` to account responses. `last_modified_time` is the
closing time of the most recent ledger in which the account was modified.
* Fix a memory leak in the code responsible for streaming [#2548](https://github.com/stellar/go/pull/2548).
* Fix a memory leak in the code responsible for streaming [#2548](https://github.com/stellar/go/pull/2548) and [#2575](https://github.com/stellar/go/pull/2575).
* Horizon encodes `fee_charged` and `max_fee` as strings when serializing transaction responses to JSON [#2555](https://github.com/stellar/go/pull/2555).

## v1.2.2
Expand Down
14 changes: 14 additions & 0 deletions services/horizon/internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"net/http"
"strings"
"sync"
"time"

"github.com/stellar/go/services/horizon/internal/actions"
Expand Down Expand Up @@ -139,9 +140,19 @@ func (we *web) streamHandler(jfn interface{}, sfn streamFunc, params interface{}
// will never return if `newLedgers` channel is not read. From Effective Go:
// > If the channel is unbuffered, the sender blocks until the receiver has received the value.
newLedgers := make(chan bool, 1)
var closedLock sync.Mutex
var closed bool
go func() {
for {
time.Sleep(we.sseUpdateFrequency)

closedLock.Lock()
tmpClosed := closed
closedLock.Unlock()
if tmpClosed {
return
}

currentLedgerState := ledger.CurrentState()
if currentLedgerState.HistoryLatest >= lastLedgerState.HistoryLatest+1 {
newLedgers <- true
Expand All @@ -154,6 +165,9 @@ func (we *web) streamHandler(jfn interface{}, sfn streamFunc, params interface{}
case <-newLedgers:
continue
case <-ctx.Done():
closedLock.Lock()
closed = true
closedLock.Unlock()
case <-we.appCtx.Done():
}

Expand Down