Code review comment: [SpotBugs-P1] Coding mistake: sendData is value in the map, it should use key class of map in remove()

Code before review:
private void processSendWork() {
synchronized (this.pendingSendLock) {
if (!this.isSendLoopRunning) {
this.isSendLoopRunning = true;
} else {
return;
}
}
TRACE_LOGGER.debug("Processing pending sends to '{}'. Available link credit '{}'", this.sendPath, this.linkCredit);
try {
if (!this.ensureLinkIsOpen().isDone()) {
return;
}
final Sender sendLinkCurrent = this.sendLink;
while (sendLinkCurrent != null
&& sendLinkCurrent.getLocalState() == EndpointState.ACTIVE && sendLinkCurrent.getRemoteState() == EndpointState.ACTIVE
&& this.linkCredit > 0) {
final WeightedDeliveryTag deliveryTag;
final SendWorkItem<DeliveryState> sendData;
synchronized (this.pendingSendLock) {
deliveryTag = this.pendingSends.poll();
if (deliveryTag == null) {
TRACE_LOGGER.debug("There are no pending sends to '{}'.", this.sendPath);
this.isSendLoopRunning = false;
break;
} else {
sendData = this.pendingSendsData.get(deliveryTag.getDeliveryTag());
if (sendData == null) {
TRACE_LOGGER.debug("SendData not found for this delivery. path:{}, linkName:{}, deliveryTag:{}", this.sendPath, this.sendLink.getName(), deliveryTag);
continue;
}
}
}
if (sendData.getWork() != null && sendData.getWork().isDone()) {
this.pendingSendsData.remove(deliveryTag.getDeliveryTag());
continue;
}
Delivery delivery = null;
boolean linkAdvance = false;
int sentMsgSize = 0;
Exception sendException = null;
try {
delivery = sendLinkCurrent.delivery(deliveryTag.getDeliveryTag().getBytes(UTF_8));
delivery.setMessageFormat(sendData.getMessageFormat());
TransactionContext transaction = sendData.getTransaction();
if (transaction != TransactionContext.NULL_TXN) {
TransactionalState transactionalState = new TransactionalState();
transactionalState.setTxnId(new Binary(transaction.getTransactionId().array()));
delivery.disposition(transactionalState);
}
TRACE_LOGGER.debug("Sending message delivery '{}' to '{}'", deliveryTag.getDeliveryTag(), this.sendPath);
sentMsgSize = sendLinkCurrent.send(sendData.getMessage(), 0, sendData.getEncodedMessageSize());
assert sentMsgSize == sendData.getEncodedMessageSize() : "Contract of the ProtonJ library for Sender.Send API changed";
linkAdvance = sendLinkCurrent.advance();
} catch(Exception exception) {
sendException = exception;
}
if (linkAdvance) {
this.linkCredit--;
sendData.setWaitingForAck();
} else {
TRACE_LOGGER.warn("Sendlink advance failed. path:{}, linkName:{}, deliveryTag:{}, sentMessageSize:{}, payloadActualSiz:{}",
this.sendPath, this.sendLink.getName(), deliveryTag, sentMsgSize, sendData.getEncodedMessageSize());
if (delivery != null) {
delivery.free();
}
Exception completionException = sendException != null ? new OperationCancelledException("Send operation failed. Please see cause for more details", sendException)
: new OperationCancelledException(String.format(Locale.US, "Send operation failed while advancing delivery(tag: %s) on SendLink(path: %s).", this.sendPath, deliveryTag));
AsyncUtil.completeFutureExceptionally(sendData.getWork(), completionException);
}
}
} finally {
synchronized (this.pendingSendLock) {
if (this.isSendLoopRunning) {
this.isSendLoopRunning = false;
}
}
}
}

Line under review: this.pendingSendsData.remove(deliveryTag.getDeliveryTag());

Code after addressing the review:
private void processSendWork() {
synchronized (this.pendingSendLock) {
if (!this.isSendLoopRunning) {
this.isSendLoopRunning = true;
} else {
return;
}
}
TRACE_LOGGER.debug("Processing pending sends to '{}'. Available link credit '{}'", this.sendPath, this.linkCredit);
try {
if (!this.ensureLinkIsOpen().isDone()) {
return;
}
final Sender sendLinkCurrent = this.sendLink;
while (sendLinkCurrent != null
&& sendLinkCurrent.getLocalState() == EndpointState.ACTIVE && sendLinkCurrent.getRemoteState() == EndpointState.ACTIVE
&& this.linkCredit > 0) {
final WeightedDeliveryTag deliveryTag;
final SendWorkItem<DeliveryState> sendData;
synchronized (this.pendingSendLock) {
deliveryTag = this.pendingSends.poll();
if (deliveryTag == null) {
TRACE_LOGGER.debug("There are no pending sends to '{}'.", this.sendPath);
this.isSendLoopRunning = false;
break;
} else {
sendData = this.pendingSendsData.get(deliveryTag.getDeliveryTag());
if (sendData == null) {
TRACE_LOGGER.debug("SendData not found for this delivery. path:{}, linkName:{}, deliveryTag:{}", this.sendPath, this.sendLink.getName(), deliveryTag);
continue;
}
}
}
if (sendData.getWork() != null && sendData.getWork().isDone()) {
this.pendingSendsData.remove(deliveryTag.getDeliveryTag());
continue;
}
Delivery delivery = null;
boolean linkAdvance = false;
int sentMsgSize = 0;
Exception sendException = null;
try {
delivery = sendLinkCurrent.delivery(deliveryTag.getDeliveryTag().getBytes(UTF_8));
delivery.setMessageFormat(sendData.getMessageFormat());
TransactionContext transaction = sendData.getTransaction();
if (transaction != TransactionContext.NULL_TXN) {
TransactionalState transactionalState = new TransactionalState();
transactionalState.setTxnId(new Binary(transaction.getTransactionId().array()));
delivery.disposition(transactionalState);
}
TRACE_LOGGER.debug("Sending message delivery '{}' to '{}'", deliveryTag.getDeliveryTag(), this.sendPath);
sentMsgSize = sendLinkCurrent.send(sendData.getMessage(), 0, sendData.getEncodedMessageSize());
assert sentMsgSize == sendData.getEncodedMessageSize() : "Contract of the ProtonJ library for Sender.Send API changed";
linkAdvance = sendLinkCurrent.advance();
} catch(Exception exception) {
sendException = exception;
}
if (linkAdvance) {
this.linkCredit--;
sendData.setWaitingForAck();
} else {
TRACE_LOGGER.warn("Sendlink advance failed. path:{}, linkName:{}, deliveryTag:{}, sentMessageSize:{}, payloadActualSiz:{}",
this.sendPath, this.sendLink.getName(), deliveryTag, sentMsgSize, sendData.getEncodedMessageSize());
if (delivery != null) {
delivery.free();
}
Exception completionException = sendException != null ? new OperationCancelledException("Send operation failed. Please see cause for more details", sendException)
: new OperationCancelledException(String.format(Locale.US, "Send operation failed while advancing delivery(tag: %s) on SendLink(path: %s).", this.sendPath, deliveryTag));
AsyncUtil.completeFutureExceptionally(sendData.getWork(), completionException);
}
}
} finally {
synchronized (this.pendingSendLock) {
if (this.isSendLoopRunning) {
this.isSendLoopRunning = false;
}
}
}
}