Skip to content

Commit

Permalink
Don't capture entire message in ACK timeout task.
Browse files Browse the repository at this point in the history
The old code caused memory leaks.
  • Loading branch information
JoshRosen committed Nov 14, 2014
1 parent a0fa1ba commit f847dd4
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,14 @@ private[nio] class ConnectionManager(
: Future[Message] = {
val promise = Promise[Message]()

// It's important that the TimerTask doesn't capture a reference to `message`, which can cause
// memory leaks since cancelled TimerTasks won't necessarily be garbage collected until they are
// scheduled to run. Therefore, extract the message id from outside of the task:
val messageId = message.id
val timeoutTask = new TimerTask {
override def run(): Unit = {
messageStatuses.synchronized {
messageStatuses.remove(message.id).foreach ( s => {
messageStatuses.remove(messageId).foreach ( s => {
val e = new IOException("sendMessageReliably failed because ack " +
s"was not received within $ackTimeout sec")
if (!promise.tryFailure(e)) {
Expand Down

0 comments on commit f847dd4

Please sign in to comment.