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

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

    • Fully Compatible
    • ALL
    • v3.4, v3.2, v3.0
    • Platforms 2017-01-23

      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.

            Assignee:
            mira.carey@mongodb.com Mira Carey
            Reporter:
            mira.carey@mongodb.com Mira Carey
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: