Skip to content

Commit f182602

Browse files
authored
Porting the memory leak fix from dev to master (#183)
* Fixing a memory leak in RequestResponseLink (#180) # Conflicts: # azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/MessagingFactory.java # azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/RequestResponseLink.java * Changing version number.
1 parent 009adf5 commit f182602

File tree

7 files changed

+46
-58
lines changed

7 files changed

+46
-58
lines changed

azure-servicebus/azure-servicebus.pom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.microsoft.azure</groupId>
66
<artifactId>azure-servicebus</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.1.1</version>
88
<licenses>
99
<license>
1010
<name>The MIT License (MIT)</name>

azure-servicebus/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>com.microsoft.azure</groupId>
1111
<artifactId>azure-servicebus-parent</artifactId>
12-
<version>1.1.0</version>
12+
<version>1.1.1</version>
1313
</parent>
1414

1515
<build>

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/CoreMessageReceiver.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,7 @@ private void createReceiveLink()
381381

382382
final ReceiveLinkHandler handler = new ReceiveLinkHandler(this);
383383
BaseHandler.setHandler(receiver, handler);
384-
this.underlyingFactory.registerForConnectionError(receiver);
385-
386384
receiver.open();
387-
388-
if (this.receiveLink != null)
389-
{
390-
this.underlyingFactory.deregisterForConnectionError(this.receiveLink);
391-
}
392-
393385
this.receiveLink = receiver;
394386
}
395387

@@ -587,7 +579,9 @@ public void onOpenComplete(Exception exception)
587579
}
588580

589581
if (exception == null)
590-
{
582+
{
583+
this.underlyingFactory.registerForConnectionError(this.receiveLink);
584+
591585
if (this.linkOpen != null && !this.linkOpen.getWork().isDone())
592586
{
593587
AsyncUtil.completeFuture(this.linkOpen.getWork(), this);
@@ -631,7 +625,7 @@ public void onOpenComplete(Exception exception)
631625
this.closeAsync();
632626
}
633627
}
634-
628+
635629
this.lastKnownLinkError = exception;
636630
}
637631
}
@@ -800,6 +794,8 @@ public void onError(Exception exception)
800794
}
801795
else
802796
{
797+
this.underlyingFactory.deregisterForConnectionError(this.receiveLink);
798+
803799
if (exception != null &&
804800
(!(exception instanceof ServiceBusException) || !((ServiceBusException) exception).getIsTransient()))
805801
{

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/CoreMessageSender.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ public void onOpenComplete(Exception completionException)
364364
{
365365
if (completionException == null)
366366
{
367+
this.underlyingFactory.registerForConnectionError(this.sendLink);
367368
this.lastKnownLinkError = null;
368369
this.retryPolicy.resetRetryCount(this.getClientId());
369370

@@ -450,6 +451,7 @@ public void onError(Exception completionException)
450451
}
451452
else
452453
{
454+
this.underlyingFactory.deregisterForConnectionError(this.sendLink);
453455
this.lastKnownLinkError = completionException;
454456
this.lastKnownErrorReportedAt = Instant.now();
455457

@@ -593,16 +595,7 @@ private void createSendLink()
593595

594596
SendLinkHandler handler = new SendLinkHandler(CoreMessageSender.this);
595597
BaseHandler.setHandler(sender, handler);
596-
597-
this.underlyingFactory.registerForConnectionError(sender);
598-
sender.open();
599-
600-
if (this.sendLink != null)
601-
{
602-
final Sender oldSender = this.sendLink;
603-
this.underlyingFactory.deregisterForConnectionError(oldSender);
604-
}
605-
598+
sender.open();
606599
this.sendLink = sender;
607600
}
608601

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/MessagingFactory.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private void closeConnection(ErrorCondition error, Exception cause)
311311
TRACE_LOGGER.info("Closing connection to host");
312312
// Important to copy the reference of the connection as a call to getConnection might create a new connection while we are still in this method
313313
Connection currentConnection = this.connection;
314-
if(connection != null)
314+
if(currentConnection != null)
315315
{
316316
Link[] links = this.registeredLinks.toArray(new Link[0]);
317317

@@ -392,27 +392,28 @@ public void onEvent()
392392
} catch (IOException e) {
393393
AsyncUtil.completeFutureExceptionally(this.connetionCloseFuture, e);
394394
}
395+
396+
Timer.schedule(new Runnable()
397+
{
398+
@Override
399+
public void run()
400+
{
401+
if (!MessagingFactory.this.connetionCloseFuture.isDone())
402+
{
403+
String errorMessage = "Closing MessagingFactory timed out.";
404+
TRACE_LOGGER.warn(errorMessage);
405+
MessagingFactory.this.connetionCloseFuture.completeExceptionally(new TimeoutException(errorMessage));
406+
}
407+
}
408+
},
409+
this.operationTimeout, TimerType.OneTimeRun);
395410
}
396-
else if(this.connection == null || this.connection.getRemoteState() == EndpointState.CLOSED)
411+
else
397412
{
398413
this.connetionCloseFuture.complete(null);
414+
Timer.unregister(this.getClientId());
399415
}
400416
});
401-
402-
Timer.schedule(new Runnable()
403-
{
404-
@Override
405-
public void run()
406-
{
407-
if (!MessagingFactory.this.connetionCloseFuture.isDone())
408-
{
409-
String errorMessage = "Closing MessagingFactory timed out.";
410-
TRACE_LOGGER.warn(errorMessage);
411-
MessagingFactory.this.connetionCloseFuture.completeExceptionally(new TimeoutException(errorMessage));
412-
}
413-
}
414-
},
415-
this.operationTimeout, TimerType.OneTimeRun);
416417

417418
return this.connetionCloseFuture;
418419
}
@@ -494,7 +495,10 @@ public void run()
494495
@Override
495496
public void registerForConnectionError(Link link)
496497
{
497-
this.registeredLinks.add(link);
498+
if(link != null)
499+
{
500+
this.registeredLinks.add(link);
501+
}
498502
}
499503

500504
/**
@@ -503,7 +507,10 @@ public void registerForConnectionError(Link link)
503507
@Override
504508
public void deregisterForConnectionError(Link link)
505509
{
506-
this.registeredLinks.remove(link);
510+
if(link != null)
511+
{
512+
this.registeredLinks.remove(link);
513+
}
507514
}
508515

509516
void scheduleOnReactorThread(final DispatchHandler handler) throws IOException

azure-servicebus/src/main/java/com/microsoft/azure/servicebus/primitives/RequestResponseLink.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ public void onOpenComplete(Exception completionException) {
480480
if(completionException == null)
481481
{
482482
TRACE_LOGGER.debug("Opened internal receive link of requestresponselink to {}", parent.linkPath);
483+
this.parent.underlyingFactory.registerForConnectionError(this.receiveLink);
483484
this.openFuture.complete(null);
484485

485486
// Send unlimited credit
@@ -511,6 +512,7 @@ public void onError(Exception exception) {
511512
}
512513

513514
TRACE_LOGGER.error("Internal receive link of requestresponselink to '{}' encountered error.", this.parent.linkPath, exception);
515+
this.parent.underlyingFactory.deregisterForConnectionError(this.receiveLink);
514516
this.parent.amqpSender.closeInternals(false);
515517
this.parent.amqpSender.setClosed();
516518
this.parent.completeAllPendingRequestsWithException(exception);
@@ -547,6 +549,7 @@ public void onClose(ErrorCondition condition) {
547549
else
548550
{
549551
TRACE_LOGGER.error("Internal receive link of requestresponselink to '{}' closed with error.", this.parent.linkPath, exception);
552+
this.parent.underlyingFactory.deregisterForConnectionError(this.receiveLink);
550553
this.parent.amqpSender.closeInternals(false);
551554
this.parent.amqpSender.setClosed();
552555
this.parent.completeAllPendingRequestsWithException(exception);
@@ -592,13 +595,6 @@ public void onReceiveComplete(Delivery delivery)
592595
}
593596

594597
public void setReceiveLink(Receiver receiveLink) {
595-
if (this.receiveLink != null)
596-
{
597-
Receiver oldReceiver = this.receiveLink;
598-
this.parent.underlyingFactory.deregisterForConnectionError(oldReceiver);
599-
}
600-
601-
this.parent.underlyingFactory.registerForConnectionError(receiveLink);
602598
this.receiveLink = receiveLink;
603599
}
604600
}
@@ -685,6 +681,7 @@ public void onOpenComplete(Exception completionException) {
685681
if(completionException == null)
686682
{
687683
TRACE_LOGGER.debug("Opened internal send link of requestresponselink to {}", parent.linkPath);
684+
this.parent.underlyingFactory.registerForConnectionError(this.sendLink);
688685
this.openFuture.complete(null);
689686
this.runSendLoop();
690687
}
@@ -714,6 +711,7 @@ public void onError(Exception exception) {
714711
}
715712

716713
TRACE_LOGGER.error("Internal send link of requestresponselink to '{}' encountered error.", this.parent.linkPath, exception);
714+
this.parent.underlyingFactory.deregisterForConnectionError(this.sendLink);
717715
this.parent.amqpReceiver.closeInternals(false);
718716
this.parent.amqpReceiver.setClosed();
719717
this.parent.completeAllPendingRequestsWithException(exception);
@@ -750,6 +748,7 @@ public void onClose(ErrorCondition condition) {
750748
else
751749
{
752750
TRACE_LOGGER.error("Internal send link of requestresponselink to '{}' closed with error.", this.parent.linkPath, exception);
751+
this.parent.underlyingFactory.deregisterForConnectionError(this.sendLink);
753752
this.parent.amqpReceiver.closeInternals(false);
754753
this.parent.amqpReceiver.setClosed();
755754
this.parent.completeAllPendingRequestsWithException(exception);
@@ -819,14 +818,7 @@ public void onSendComplete(Delivery delivery) {
819818
// Doesn't happen as sends are settled on send
820819
}
821820

822-
public void setSendLink(Sender sendLink) {
823-
if (this.sendLink != null)
824-
{
825-
Sender oldSender = this.sendLink;
826-
this.parent.underlyingFactory.deregisterForConnectionError(oldSender);
827-
}
828-
829-
this.parent.underlyingFactory.registerForConnectionError(sendLink);
821+
public void setSendLink(Sender sendLink) {
830822
this.sendLink = sendLink;
831823
this.availableCredit = new AtomicInteger(0);
832824
}

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.microsoft.azure</groupId>
66
<artifactId>azure-servicebus-parent</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.1.1</version>
88
<packaging>pom</packaging>
99

1010
<url>https://github.com/Azure/azure-service-bus-java</url>
@@ -13,7 +13,7 @@
1313
<proton-j-version>0.22.0</proton-j-version>
1414
<junit-version>4.12</junit-version>
1515
<slf4j-version>1.8.0-alpha2</slf4j-version>
16-
<client-current-version>1.1.0</client-current-version>
16+
<client-current-version>1.1.1</client-current-version>
1717
</properties>
1818

1919
<modules>

0 commit comments

Comments
 (0)