Commit 55b4bc9
authored
[fix] Fix memory leak caused by incorrect close and destruction (apache#54)
Fixes apache#55
### Motivation
1. When a producer or consumer is closed, the reference is still stored
in `ClientImpl`. If a client kept creating producers or consumers,
the memory usage would not reduce.
2. When the `HandlerBase::connection_` field is modified, the
`removeProducer` or `removeConsumer` method is not called. Then these
producers and consumers will be cached in the connection until the
connection is closed.
3. The `PartitionedProducerImpl` and `MultiTopicsConsumerImpl` have
cyclic references, when a `Producer` or `Consumer` instance goes out
of the scope, the destructors are not called. When I used GDB to
debug them, I found the reference counts were both greater than 1.
### Modifications
Let's use "handlers" to represent "producers and consumers".
1. In `ClientImpl`, use `SynchronizedHashMap` to store references of
handlers, as well as the `cleanupXXX` methods to remove a handler.
2. Add `HandlerBase::beforeConnectionChange` method, which is called
before `connection_` is modified. Disallow the access to
`connection_` from derived classes.
3. Avoid `shared_from_this()` being passed into callbacks in ASIO
executors for `PartitionedProducerImpl` and
`MultiTopicsConsumerImpl`.
This PR also unifies the `shutdown` implementations for handlers and
call `shutdown` in the destructors.
1. Cancel the timers
2. Unregister itself from `ClientImpl` and `ClientConnection`
3. Set the create future with `ResultAlreadyClosed`
4. Set the state to `Closed`
It's called when:
- the destructor is called
- `closeAsync` is completed
- `unsubscribeAsync` is completed with ResultOk
### Verifications
`ShutdownTest` is added to verify the following cases:
- a single topic
- a partitioned topic (multiple topics)
- a partitioned topic with regex subscription
`testClose` verifies `shutdown` when `closeAsync` and `unsubscribeAsync`
are called. `testDestructor` verifies `shutdown` when handlers go out of
the scope and the destructors are called.1 parent 7f7653b commit 55b4bc9
File tree
24 files changed
+610
-357
lines changed- .github/workflows
- lib
- tests
24 files changed
+610
-357
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
317 | | - | |
| 317 | + | |
318 | 318 | | |
319 | 319 | | |
320 | 320 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
192 | | - | |
193 | | - | |
194 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
195 | 201 | | |
196 | 202 | | |
197 | 203 | | |
| |||
241 | 247 | | |
242 | 248 | | |
243 | 249 | | |
244 | | - | |
245 | | - | |
246 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
247 | 262 | | |
248 | 263 | | |
249 | 264 | | |
| |||
397 | 412 | | |
398 | 413 | | |
399 | 414 | | |
400 | | - | |
401 | | - | |
402 | | - | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
403 | 424 | | |
404 | 425 | | |
405 | 426 | | |
| |||
477 | 498 | | |
478 | 499 | | |
479 | 500 | | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
487 | 505 | | |
488 | 506 | | |
489 | 507 | | |
490 | 508 | | |
491 | | - | |
492 | 509 | | |
493 | 510 | | |
494 | 511 | | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
495 | 515 | | |
496 | 516 | | |
497 | 517 | | |
498 | 518 | | |
499 | | - | |
500 | | - | |
| 519 | + | |
| 520 | + | |
501 | 521 | | |
502 | 522 | | |
503 | 523 | | |
| |||
507 | 527 | | |
508 | 528 | | |
509 | 529 | | |
510 | | - | |
511 | | - | |
| 530 | + | |
| 531 | + | |
512 | 532 | | |
513 | 533 | | |
514 | 534 | | |
| |||
562 | 582 | | |
563 | 583 | | |
564 | 584 | | |
565 | | - | |
566 | | - | |
567 | | - | |
| 585 | + | |
| 586 | + | |
568 | 587 | | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
| 588 | + | |
| 589 | + | |
575 | 590 | | |
576 | 591 | | |
577 | 592 | | |
578 | 593 | | |
579 | 594 | | |
580 | | - | |
581 | | - | |
| 595 | + | |
| 596 | + | |
582 | 597 | | |
583 | 598 | | |
584 | 599 | | |
| |||
631 | 646 | | |
632 | 647 | | |
633 | 648 | | |
634 | | - | |
635 | 649 | | |
636 | | - | |
| 650 | + | |
637 | 651 | | |
638 | 652 | | |
639 | 653 | | |
640 | 654 | | |
641 | | - | |
| 655 | + | |
642 | 656 | | |
643 | 657 | | |
644 | 658 | | |
645 | 659 | | |
646 | | - | |
647 | 660 | | |
648 | | - | |
| 661 | + | |
649 | 662 | | |
650 | 663 | | |
651 | 664 | | |
652 | 665 | | |
653 | | - | |
| 666 | + | |
654 | 667 | | |
655 | 668 | | |
656 | 669 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
94 | 100 | | |
95 | 101 | | |
96 | 102 | | |
| |||
147 | 153 | | |
148 | 154 | | |
149 | 155 | | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
| 156 | + | |
| 157 | + | |
155 | 158 | | |
156 | 159 | | |
157 | 160 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
0 commit comments