@@ -703,6 +703,23 @@ func (p *Brontide) Start() error {
703
703
704
704
p .startTime = time .Now ()
705
705
706
+ // Before launching the writeHandler goroutine, we send any channel
707
+ // sync messages that must be resent for borked channels. We do this to
708
+ // avoid data races with WriteMessage & Flush calls.
709
+ if len (msgs ) > 0 {
710
+ p .log .Infof ("Sending %d channel sync messages to peer after " +
711
+ "loading active channels" , len (msgs ))
712
+
713
+ // Send the messages directly via writeMessage and bypass the
714
+ // writeHandler goroutine.
715
+ for _ , msg := range msgs {
716
+ if err := p .writeMessage (msg ); err != nil {
717
+ return fmt .Errorf ("unable to send " +
718
+ "reestablish msg: %v" , err )
719
+ }
720
+ }
721
+ }
722
+
706
723
err = p .pingManager .Start ()
707
724
if err != nil {
708
725
return fmt .Errorf ("could not start ping manager %w" , err )
@@ -717,23 +734,6 @@ func (p *Brontide) Start() error {
717
734
// Signal to any external processes that the peer is now active.
718
735
close (p .activeSignal )
719
736
720
- // Now that the peer has started up, we send any channel sync messages
721
- // that must be resent for borked channels.
722
- if len (msgs ) > 0 {
723
- p .log .Infof ("Sending %d channel sync messages to peer after " +
724
- "loading active channels" , len (msgs ))
725
-
726
- // Send the messages directly via writeMessage and bypass the
727
- // writeHandler goroutine to avoid cases where writeHandler
728
- // may exit and cause a deadlock.
729
- for _ , msg := range msgs {
730
- if err := p .writeMessage (msg ); err != nil {
731
- return fmt .Errorf ("unable to send reestablish" +
732
- "msg: %v" , err )
733
- }
734
- }
735
- }
736
-
737
737
// Node announcements don't propagate very well throughout the network
738
738
// as there isn't a way to efficiently query for them through their
739
739
// timestamp, mostly affecting nodes that were offline during the time
@@ -2093,6 +2093,12 @@ func (p *Brontide) logWireMessage(msg lnwire.Message, read bool) {
2093
2093
// message buffered on the connection. It is safe to call this method again
2094
2094
// with a nil message iff a timeout error is returned. This will continue to
2095
2095
// flush the pending message to the wire.
2096
+ //
2097
+ // NOTE:
2098
+ // Besides its usage in Start, this function should not be used elsewhere
2099
+ // except in writeHandler. If multiple goroutines call writeMessage at the same
2100
+ // time, panics can occur because WriteMessage and Flush don't use any locking
2101
+ // internally.
2096
2102
func (p * Brontide ) writeMessage (msg lnwire.Message ) error {
2097
2103
// Only log the message on the first attempt.
2098
2104
if msg != nil {
0 commit comments