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

Fix memory leak in tail recursion #79

Merged
merged 2 commits into from
Sep 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The `Unreleased` section name is replaced by the expected version of next releas
### Removed
### Fixed

- Memory leak in the tail recursion: disposables within the tail recursion loop do not get disposed until the recursion terminates. Fixed it by replacing the tail recursion with while loop.

<a name="1.5.3"></a>
## [1.5.3] - 2020-09-08

Expand Down Expand Up @@ -438,4 +440,4 @@ _NOTE: not interoperable (i.e., via a binding redirect) with CK 1.1 due to a bre
[1.0.0-rc1]: https://github.com/jet/FsKafka/compare/1.0.0-preview2...1.0.0-rc1
[1.0.0-preview2]: https://github.com/jet/FsKafka/compare/1.0.0-preview1...1.0.0-preview2
[1.0.0-preview1]: https://github.com/jet/FsKafka/compare/1.0.0-bare...1.0.0-preview1
[1.0.0-bare]: https://github.com/jet/FsKafka/compare/e4bc8ff53b4f4400308b09c02fe8da6fc7e61d82...1.0.0-bare
[1.0.0-bare]: https://github.com/jet/FsKafka/compare/e4bc8ff53b4f4400308b09c02fe8da6fc7e61d82...1.0.0-bare
60 changes: 31 additions & 29 deletions src/FsKafka/FsKafka.fs
Original file line number Diff line number Diff line change
Expand Up @@ -466,35 +466,37 @@ module private ConsumerImpl =
Array.Clear(buffer, 0, count)
batch

let rec loop () = async {
if not collection.IsCompleted then
let batchWatch = System.Diagnostics.Stopwatch()
let mutable batchLen, batchSize = 0, 0L
use __ = Serilog.Context.LogContext.PushProperty("partition", Binding.partitionValue key.Partition)
try match nextBatch() with
| [||] -> ()
| batch ->
batchLen <- batch.Length
batchSize <- batch |> Array.sumBy approximateMessageBytes
batchWatch.Start()
log.ForContext("batchSize", batchSize).Debug("Dispatching {batchLen} message(s) to handler", batchLen)
// run the handler function
do! handler batch

// store completed offsets
let lastItem = batch |> Array.maxBy (fun m -> Binding.offsetValue m.Offset)
consumer.StoreOffset(lastItem)

// decrement in-flight message counter
counter.Delta(-batchSize)
with e ->
log.ForContext("batchSize", batchSize).ForContext("batchLen", batchLen).ForContext("handlerDuration", batchWatch.Elapsed)
.Information(e, "Exiting batch processing loop due to handler exception")
tcs.TrySetException e |> ignore
cts.Cancel()
return! loop() }

Async.Start(loop(), cts.Token)
let processBatch = async {
let batchWatch = System.Diagnostics.Stopwatch()
let mutable batchLen, batchSize = 0, 0L
try match nextBatch() with
| [||] -> ()
| batch ->
batchLen <- batch.Length
batchSize <- batch |> Array.sumBy approximateMessageBytes
batchWatch.Start()
log.ForContext("batchSize", batchSize).Debug("Dispatching {batchLen} message(s) to handler", batchLen)
// run the handler function
do! handler batch

// store completed offsets
let lastItem = batch |> Array.maxBy (fun m -> Binding.offsetValue m.Offset)
consumer.StoreOffset(lastItem)

// decrement in-flight message counter
counter.Delta(-batchSize)
with e ->
log.ForContext("batchSize", batchSize).ForContext("batchLen", batchLen).ForContext("handlerDuration", batchWatch.Elapsed)
.Information(e, "Exiting batch processing loop due to handler exception")
tcs.TrySetException e |> ignore
cts.Cancel() }

let loop = async {
use __ = Serilog.Context.LogContext.PushProperty("partition", Binding.partitionValue key.Partition)
while not collection.IsCompleted do
do! processBatch }

Async.Start(loop, cts.Token)

use _ = partitionedCollection.OnPartitionAdded.Subscribe consumePartition

Expand Down
58 changes: 30 additions & 28 deletions src/FsKafka0/FsKafka.fs
Original file line number Diff line number Diff line change
Expand Up @@ -478,34 +478,36 @@ module private ConsumerImpl =
Array.Clear(buffer, 0, count)
batch

let rec loop () = async {
if not collection.IsCompleted then
let batchWatch = System.Diagnostics.Stopwatch()
let mutable batchLen, batchSize = 0, 0L
use __ = Serilog.Context.LogContext.PushProperty("partition", Binding.partitionValue key.Partition)
try match nextBatch() with
| [||] -> ()
| batch ->
batchLen <- batch.Length
batchSize <- batch |> Array.sumBy (fun m -> getMessageSize m)
batchWatch.Start()
log.ForContext("batchSize", batchSize).Debug("Dispatching {batchLen} message(s) to handler", batchLen)
// run the handler function
do! handler batch

// store completed offsets
storeOffset consumer (getBatchOffset batch)

// decrement in-flight message counter
counter.Delta -batchSize
with e ->
log.ForContext("batchSize", batchSize).ForContext("batchLen", batchLen).ForContext("handlerDuration", batchWatch.Elapsed)
.Information(e, "Exiting batch processing loop due to handler exception")
tcs.TrySetException e |> ignore
cts.Cancel()
return! loop() }

Async.Start(loop(), cts.Token)
let processBatch = async {
let batchWatch = System.Diagnostics.Stopwatch()
let mutable batchLen, batchSize = 0, 0L
try match nextBatch() with
| [||] -> ()
| batch ->
batchLen <- batch.Length
batchSize <- batch |> Array.sumBy (fun m -> getMessageSize m)
batchWatch.Start()
log.ForContext("batchSize", batchSize).Debug("Dispatching {batchLen} message(s) to handler", batchLen)
// run the handler function
do! handler batch

// store completed offsets
storeOffset consumer (getBatchOffset batch)

// decrement in-flight message counter
counter.Delta -batchSize
with e ->
log.ForContext("batchSize", batchSize).ForContext("batchLen", batchLen).ForContext("handlerDuration", batchWatch.Elapsed)
.Information(e, "Exiting batch processing loop due to handler exception")
tcs.TrySetException e |> ignore
cts.Cancel() }

let loop = async {
use __ = Serilog.Context.LogContext.PushProperty("partition", Binding.partitionValue key.Partition)
while not collection.IsCompleted do
do! processBatch }

Async.Start(loop, cts.Token)

use _ = partitionedCollection.OnPartitionAdded.Subscribe consumePartition
use _ = consumer.OnPartitionsRevoked.Subscribe (fun ps -> for p in ps do partitionedCollection.Revoke p)
Expand Down