Commit 8a520b3
AMQP-621: Fix Consumer.basicCancel Race Condition
JIRA: https://jira.spring.io/browse/AMQP-621
The `SimpleMessageListenerContainer` has logic to initiate `BlockingQueueConsumer.basicCancel()` and mark `BlockingQueueConsumer` as inactive immediately.
But the `handleCancelOk` answer can come back from the Broker a bit late, in async manner.
During this time window we can still receive messages via `handleDelivery`.
With `prefetchCount = 1` we may end up with the deadlock on `handleDelivery` thread during that time window, when main consumer loop identifies consumer as inactive and exits for `stop` process
and can't do that because the `BlockingQueueConsumer.this.queue.put(new Delivery(consumerTag, envelope, properties, body))` operation blocks the consumer from any other operations.
* Fix the race condition via additional `BlockingQueueConsumer.cancelled()` state to let main loop to spin until `handleCancelOk` honoring any in-flight messages
* The `cancelled` state is based on the `cancelReceived` flag which is changed from the `handleCancelOk` as a Broker response for the `basicCancel`
and in the `handleCancel` event from the Broker as a reason of some unexpected state on Broker, e.g. consuming queue has been removed
* Protect message lost via `basicReject` in case of late arrival into `handleDelivery`, when the consumer has been cancelled already
Address PR comments
* Rename `cancelReceived` -> `cancelled` with JavaDocs
* Initiate `basicCancel()` from `handleCancel(String consumerTag)` to cancel `Consumer` from all other tags.
The Broker canceling initiative is fully similar to `queueChanged` in the listener container
* Do no mark `cancelled` in the `handleCancelOk` until all the tags are cancelled
Polishing
1. Fix case when basicCancel received with only one queue (set cancelled).
2. Add protection to cancelled() in case all cancelOks are not received within shutdownTimeout
after aborting.
3. If we are aborting, use offer instead of put to insert into the queue to avoid indefinitely
blocking the client thread.
4. Purge the queue at the end of stop() just in case #2 happens and we still have a hung client thread.
Add Test - Abort with Multiple Queues1 parent 0e042b0 commit 8a520b3
File tree
4 files changed
+89
-21
lines changed- spring-rabbit/src
- main/java/org/springframework/amqp/rabbit/listener
- test/java/org/springframework/amqp/rabbit/listener
4 files changed
+89
-21
lines changedLines changed: 43 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
100 | 104 | | |
101 | 105 | | |
102 | | - | |
103 | | - | |
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
| |||
133 | 135 | | |
134 | 136 | | |
135 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
136 | 142 | | |
137 | 143 | | |
138 | 144 | | |
| |||
249 | 255 | | |
250 | 256 | | |
251 | 257 | | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
252 | 262 | | |
253 | 263 | | |
254 | 264 | | |
| |||
318 | 328 | | |
319 | 329 | | |
320 | 330 | | |
321 | | - | |
322 | | - | |
| 331 | + | |
323 | 332 | | |
324 | 333 | | |
325 | 334 | | |
326 | 335 | | |
327 | 336 | | |
328 | 337 | | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
329 | 343 | | |
330 | 344 | | |
331 | 345 | | |
| |||
339 | 353 | | |
340 | 354 | | |
341 | 355 | | |
342 | | - | |
343 | 356 | | |
344 | 357 | | |
345 | 358 | | |
| |||
391 | 404 | | |
392 | 405 | | |
393 | 406 | | |
394 | | - | |
| 407 | + | |
395 | 408 | | |
396 | 409 | | |
397 | 410 | | |
| |||
582 | 595 | | |
583 | 596 | | |
584 | 597 | | |
585 | | - | |
586 | 598 | | |
587 | | - | |
| 599 | + | |
588 | 600 | | |
589 | 601 | | |
590 | 602 | | |
| |||
602 | 614 | | |
603 | 615 | | |
604 | 616 | | |
| 617 | + | |
605 | 618 | | |
606 | 619 | | |
607 | 620 | | |
| |||
738 | 751 | | |
739 | 752 | | |
740 | 753 | | |
741 | | - | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
742 | 757 | | |
743 | 758 | | |
744 | | - | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
745 | 765 | | |
746 | 766 | | |
747 | 767 | | |
748 | 768 | | |
749 | 769 | | |
750 | | - | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
751 | 773 | | |
752 | | - | |
753 | | - | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
754 | 777 | | |
755 | 778 | | |
756 | 779 | | |
| |||
761 | 784 | | |
762 | 785 | | |
763 | 786 | | |
764 | | - | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
765 | 794 | | |
766 | 795 | | |
767 | 796 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1040 | 1040 | | |
1041 | 1041 | | |
1042 | 1042 | | |
| 1043 | + | |
1043 | 1044 | | |
1044 | 1045 | | |
1045 | 1046 | | |
| |||
1306 | 1307 | | |
1307 | 1308 | | |
1308 | 1309 | | |
1309 | | - | |
| 1310 | + | |
1310 | 1311 | | |
1311 | 1312 | | |
1312 | 1313 | | |
| |||
Lines changed: 43 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
49 | 53 | | |
50 | | - | |
| 54 | + | |
51 | 55 | | |
52 | 56 | | |
53 | 57 | | |
| |||
84 | 88 | | |
85 | 89 | | |
86 | 90 | | |
87 | | - | |
88 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
89 | 108 | | |
90 | 109 | | |
91 | 110 | | |
| |||
106 | 125 | | |
107 | 126 | | |
108 | 127 | | |
109 | | - | |
| 128 | + | |
110 | 129 | | |
111 | 130 | | |
112 | 131 | | |
| |||
120 | 139 | | |
121 | 140 | | |
122 | 141 | | |
123 | | - | |
| 142 | + | |
124 | 143 | | |
125 | 144 | | |
126 | 145 | | |
| |||
141 | 160 | | |
142 | 161 | | |
143 | 162 | | |
144 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
145 | 182 | | |
146 | 183 | | |
147 | 184 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
157 | 158 | | |
158 | 159 | | |
159 | 160 | | |
| |||
0 commit comments