@@ -88,6 +88,8 @@ type Whisper struct {
8888 stats Statistics // Statistics of whisper node
8989
9090 mailServer MailServer // MailServer interface
91+
92+ wg sync.WaitGroup
9193}
9294
9395// New creates a Whisper client ready to communicate through the Ethereum P2P network.
@@ -243,8 +245,10 @@ func (whisper *Whisper) SetBloomFilter(bloom []byte) error {
243245 whisper .settings .Store (bloomFilterIdx , b )
244246 whisper .notifyPeersAboutBloomFilterChange (b )
245247
248+ whisper .wg .Add (1 )
246249 go func () {
247250 // allow some time before all the peers have processed the notification
251+ defer whisper .wg .Done ()
248252 time .Sleep (time .Duration (whisper .syncAllowance ) * time .Second )
249253 whisper .settings .Store (bloomFilterToleranceIdx , b )
250254 }()
@@ -261,7 +265,9 @@ func (whisper *Whisper) SetMinimumPoW(val float64) error {
261265 whisper .settings .Store (minPowIdx , val )
262266 whisper .notifyPeersAboutPowRequirementChange (val )
263267
268+ whisper .wg .Add (1 )
264269 go func () {
270+ defer whisper .wg .Done ()
265271 // allow some time before all the peers have processed the notification
266272 time .Sleep (time .Duration (whisper .syncAllowance ) * time .Second )
267273 whisper .settings .Store (minPowToleranceIdx , val )
@@ -626,10 +632,12 @@ func (whisper *Whisper) Send(envelope *Envelope) error {
626632// of the Whisper protocol.
627633func (whisper * Whisper ) Start (* p2p.Server ) error {
628634 log .Info ("started whisper v." + ProtocolVersionStr )
635+ whisper .wg .Add (1 )
629636 go whisper .update ()
630637
631638 numCPU := runtime .NumCPU ()
632639 for i := 0 ; i < numCPU ; i ++ {
640+ whisper .wg .Add (1 )
633641 go whisper .processQueue ()
634642 }
635643
@@ -640,6 +648,7 @@ func (whisper *Whisper) Start(*p2p.Server) error {
640648// of the Whisper protocol.
641649func (whisper * Whisper ) Stop () error {
642650 close (whisper .quit )
651+ whisper .wg .Wait ()
643652 log .Info ("whisper stopped" )
644653 return nil
645654}
@@ -874,6 +883,7 @@ func (whisper *Whisper) checkOverflow() {
874883
875884// processQueue delivers the messages to the watchers during the lifetime of the whisper node.
876885func (whisper * Whisper ) processQueue () {
886+ defer whisper .wg .Done ()
877887 var e * Envelope
878888 for {
879889 select {
@@ -892,6 +902,7 @@ func (whisper *Whisper) processQueue() {
892902// update loops until the lifetime of the whisper node, updating its internal
893903// state by expiring stale messages from the pool.
894904func (whisper * Whisper ) update () {
905+ defer whisper .wg .Done ()
895906 // Start a ticker to check for expirations
896907 expire := time .NewTicker (expirationCycle )
897908
0 commit comments