Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-27240

ConnectBG must not close() while the background thread is active

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • None
    • 3.0.15, 3.2.12, 3.4.1, 3.5.1
    • Networking
    • None
    • Fully Compatible
    • ALL
    • v3.4, v3.2, v3.0
    • Platforms 2017-01-23

    Description

      ConnectBG handles asynchronous connects by spinning up a background thread to call ::connect(). We kick that thread out of connect, after a timeout, by calling close. This isn't safe because:

      Thread A: Your call to Socket::connect()
      Thread B: BG connector for A
      Thread C: Unrelated thread calling Socket::connect()
      Thread D: BG connector for C
       
       
       
      Thread A                     Thread B                Thread C           Thread D
      Start Thread B
                                   connect(N)
      Wait 4.9999 seconds
                                   get EINTR
      Trigger timeout
      shutdown(N)
      close(N)
                                                           N = socket()
                                   connect(N)
                                                                              connect(N)
                                                                              errno == EISCONN
      

      Which can lead us to connect a fd to the wrong endpoint.

      We can work around this by only calling shutdown from the parent thread, waiting for the child to join, then calling close.

      Attachments

        Issue Links

          Activity

            People

              jason.carey@mongodb.com Jason Carey
              jason.carey@mongodb.com Jason Carey
              Votes:
              0 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: