Commit 03bb6b7
fix(pulse-core): reference-count shared subscriptions (#1000)
`subscribe()` and `subscribeContract()` memoise by key, so concurrent callers
asking for the same address or contract receive the *same* Watcher object.
`unsubscribe()` called `stop()` on it unconditionally. `Watcher.stop()` sets
`_stopped`, calls `removeAllListeners()`, and makes `emit()` return false
without dispatching — so the first caller to leave silently killed every other
caller's event flow.
The failure is invisible from the outside. In apps/web both SSE routes key on
data that is not per-connection (`address`, `contract:${contractId}`) and
unsubscribe on teardown, so when one visitor closed their tab, every other
visitor watching the same contract kept an open connection, kept receiving
heartbeats, and never received another event — indistinguishable from a quiet
contract.
Adds a namespaced refcount map (`addr:` / `contract:` / `config:`, since the
three registries have independent key spaces). `subscribe*()` retains,
`unsubscribe*()` releases, and the watcher stops only on the last release.
Details worth noting:
- Each watcher's stop handler clears its own refcount entry, so a consumer
calling `watcher.stop()` directly cannot strand a count and leave the next
subscription for that key permanently unstoppable.
- `release()` on an unknown key returns true (stop). An already-torn-down
entry should not keep a watcher alive.
- `unsubscribeAll()` / `unsubscribeAllContracts()` are teardown and
deliberately ignore counts; `unsubscribeAll()` now iterates a snapshot
because the stop handlers mutate the registry it was iterating.
Single-subscriber behaviour is unchanged: one subscribe, one unsubscribe,
stopped immediately.
Verified end to end against a live server: two clients streaming mainnet USDC
(CCW67TSZ...), first disconnected, second went on receiving real
`contract.emitted` events rather than going silent.
12 new tests; all 603 existing pulse-core tests still pass.
Co-authored-by: Salmatcre8 <118213044+Salmatcre8@users.noreply.github.com>1 parent 3ce8c59 commit 03bb6b7
2 files changed
Lines changed: 253 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
146 | 153 | | |
147 | 154 | | |
148 | 155 | | |
| |||
170 | 177 | | |
171 | 178 | | |
172 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
173 | 194 | | |
174 | 195 | | |
175 | 196 | | |
| |||
511 | 532 | | |
512 | 533 | | |
513 | 534 | | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
514 | 557 | | |
515 | 558 | | |
516 | 559 | | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
517 | 564 | | |
518 | 565 | | |
519 | 566 | | |
520 | 567 | | |
521 | 568 | | |
522 | 569 | | |
523 | 570 | | |
| 571 | + | |
524 | 572 | | |
525 | 573 | | |
526 | 574 | | |
| |||
539 | 587 | | |
540 | 588 | | |
541 | 589 | | |
| 590 | + | |
542 | 591 | | |
543 | 592 | | |
544 | 593 | | |
| |||
554 | 603 | | |
555 | 604 | | |
556 | 605 | | |
| 606 | + | |
557 | 607 | | |
558 | 608 | | |
559 | 609 | | |
| |||
566 | 616 | | |
567 | 617 | | |
568 | 618 | | |
| 619 | + | |
569 | 620 | | |
570 | 621 | | |
571 | 622 | | |
| |||
574 | 625 | | |
575 | 626 | | |
576 | 627 | | |
577 | | - | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
578 | 634 | | |
579 | 635 | | |
580 | 636 | | |
581 | | - | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
582 | 642 | | |
583 | 643 | | |
584 | 644 | | |
585 | 645 | | |
586 | 646 | | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
587 | 651 | | |
588 | 652 | | |
589 | | - | |
| 653 | + | |
590 | 654 | | |
591 | 655 | | |
592 | 656 | | |
| |||
625 | 689 | | |
626 | 690 | | |
627 | 691 | | |
628 | | - | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
629 | 696 | | |
630 | 697 | | |
| 698 | + | |
631 | 699 | | |
632 | 700 | | |
633 | 701 | | |
| |||
638 | 706 | | |
639 | 707 | | |
640 | 708 | | |
| 709 | + | |
641 | 710 | | |
642 | 711 | | |
643 | 712 | | |
644 | 713 | | |
645 | 714 | | |
646 | 715 | | |
647 | | - | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
648 | 720 | | |
649 | 721 | | |
650 | 722 | | |
| |||
653 | 725 | | |
654 | 726 | | |
655 | 727 | | |
| 728 | + | |
656 | 729 | | |
657 | 730 | | |
658 | 731 | | |
| |||
663 | 736 | | |
664 | 737 | | |
665 | 738 | | |
| 739 | + | |
666 | 740 | | |
667 | 741 | | |
668 | 742 | | |
| |||
677 | 751 | | |
678 | 752 | | |
679 | 753 | | |
| 754 | + | |
680 | 755 | | |
681 | 756 | | |
682 | 757 | | |
| |||
689 | 764 | | |
690 | 765 | | |
691 | 766 | | |
| 767 | + | |
692 | 768 | | |
693 | 769 | | |
694 | 770 | | |
| |||
717 | 793 | | |
718 | 794 | | |
719 | 795 | | |
| 796 | + | |
| 797 | + | |
720 | 798 | | |
721 | | - | |
| 799 | + | |
| 800 | + | |
722 | 801 | | |
723 | 802 | | |
724 | | - | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
725 | 806 | | |
726 | 807 | | |
727 | 808 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
0 commit comments