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

The mongod process forks before listening for connections

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 3.4.9
    • Fix Version/s: 3.4.11, 3.6.1, 3.7.1
    • Component/s: None
    • Labels:
      None
    • Backwards Compatibility:
      Fully Compatible
    • Operating System:
      ALL
    • Backport Requested:
      v3.6, v3.4
    • Steps To Reproduce:
      Hide

      Create a systemd service to test if the socket is listening:

      [Unit]
      Type=oneshot
      Description=TestService
      After=mongod.service
      Wants=mongod.service
       
      [Service]
      ExecStart=/usr/bin/testsocket
      Restart=on-abort
       
      [Install]
      WantedBy=multi-user.target
      

      With the following script (/usr/bin/testsocket)

      #!/bin/sh
      /bin/nc localhost 27017 </dev/null >/dev/null && echo "Listening" || echo "Not listening"
      

      Stop mongod, and start the service which should start mongod first:

      [root@localhost ~]# systemctl stop mongod
      [root@localhost ~]# ps aux|egrep '[m]ongo'    
      [root@localhost ~]# systemctl start testservice
      [root@localhost ~]# systemctl status testservice
      ● testservice.service - TestService
       
      Sep 21 19:49:02 localhost.localdomain systemd[1]: Started TestService.
      Sep 21 19:49:02 localhost.localdomain systemd[1]: Starting TestService...
      Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Ncat: Connection refused.
      Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Not listening
      

      Add a 1 second sleep via ExecStartPre in the testservice unit file, or to the beginning of /usr/bin/testsocket and mongod is listening by the time it runs:

      [root@localhost ~]# systemctl stop mongod       
      [root@localhost ~]# ps aux|egrep '[m]ongo'    
      [root@localhost ~]# systemctl start testservice       
      [root@localhost ~]# systemctl status testservice        
      ● testservice.service - TestService
       
      Sep 21 19:52:13 localhost.localdomain systemd[1]: Started TestService.
      Sep 21 19:52:13 localhost.localdomain systemd[1]: Starting TestService...
      Sep 21 19:52:14 localhost.localdomain testsocket[3882]: Listening
      

      Show
      Create a systemd service to test if the socket is listening: [Unit] Type=oneshot Description=TestService After=mongod.service Wants=mongod.service [Service] ExecStart=/usr/bin/testsocket Restart=on-abort [Install] WantedBy=multi-user.target With the following script ( /usr/bin/testsocket ) #!/bin/sh /bin/nc localhost 27017 </dev/null >/dev/null && echo "Listening" || echo "Not listening" Stop mongod, and start the service which should start mongod first: [root@localhost ~]# systemctl stop mongod [root@localhost ~]# ps aux|egrep '[m]ongo' [root@localhost ~]# systemctl start testservice [root@localhost ~]# systemctl status testservice ● testservice.service - TestService   Sep 21 19:49:02 localhost.localdomain systemd[1]: Started TestService. Sep 21 19:49:02 localhost.localdomain systemd[1]: Starting TestService... Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Ncat: Connection refused. Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Not listening Add a 1 second sleep via ExecStartPre in the testservice unit file, or to the beginning of /usr/bin/testsocket and mongod is listening by the time it runs: [root@localhost ~]# systemctl stop mongod [root@localhost ~]# ps aux|egrep '[m]ongo' [root@localhost ~]# systemctl start testservice [root@localhost ~]# systemctl status testservice ● testservice.service - TestService   Sep 21 19:52:13 localhost.localdomain systemd[1]: Started TestService. Sep 21 19:52:13 localhost.localdomain systemd[1]: Starting TestService... Sep 21 19:52:14 localhost.localdomain testsocket[3882]: Listening
    • Sprint:
      Build Ready

      Description

      When starting mongod as a systemd service, the process forks before mongod has opened the socket and starts listening for connections. This can cause dependent services to fail to start, as systemd considers mongod started once the process has forked.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                12 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: