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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.0.15, 3.2.12, 3.4.1, 3.5.1
    • Component/s: Networking
    • Labels:
      None
    • Backwards Compatibility:
      Fully Compatible
    • Operating System:
      ALL
    • Backport Completed:
    • Backport Requested:
      v3.4, v3.2, v3.0
    • Sprint:
      Platforms 2017-01-23
    • Case:

      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

              Assignee:
              jason.carey Jason Carey
              Reporter:
              jason.carey Jason Carey
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              10 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: